1
$\begingroup$

I want to draw a circle into the 3d view in the direction of the current view rotation.

I have 2 vectors (center, mouse_pos), so I have two points in 3d space, so I have the radius - fine.

This is my code so far and the painted result:

enter image description here

segments = 32
mul = (1.0 / (segments - 1)) * (pi * 2)
self._vertices = [(sin(i * mul) + self._center[0], 
cos(i * mul) + self._center[1], 0) for i in range(segments)]

With this the circle is painted like I have a "top perspective". I also have a method to get the current view direction:

def get_view_direction(context):
    rv3d      = context.space_data.region_3d
    view_rot  = rv3d.view_rotation

    dir = view_rot @ mathutils.Vector((0,0,-1))
    return dir.normalized()

My question is now how to let the segments of the circle take the rotation of the view into account? Any ideas?

EDIT: First part solved:

rv3d      = context.space_data.region_3d
view_rot  = rv3d.view_rotation

segments = 32
mul = (1.0 / (segments - 1)) * (pi * 2)
points = [(sin(i * mul), cos(i * mul), 0) for i in range(segments)]

for point in points:
    vertex = view_rot @ mathutils.Vector(point)
    self._vertices.append(vertex)

What's left now is to take the radius and center into acount, and I want to move it along the view direction, but I already have a method for this:

def get_view_direction(context):
    rv3d      = context.space_data.region_3d
    view_rot  = rv3d.view_rotation

    dir = view_rot @ mathutils.Vector((0,0,-1))
    return dir.normalized()

So I guess I can add the direction multiplied by a scalar to each vector?

$\endgroup$
1
$\begingroup$

Okay guys got it, sorry to again answer my own question:-)

rv3d      = context.space_data.region_3d
view_rot  = rv3d.view_rotation

segments = 32
mul = (1.0 / (segments - 1)) * (pi * 2)
points = [(sin(i * mul) * self._radius, cos(i * mul) * self._radius, 0) 
for i in range(segments)]

self._vertices = [view_rot @ mathutils.Vector(point) + 
                  self._center for point in points]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.