 # Generate a point X units along object facing direction?

Hi All,

Say I have an object and it is randomly located and oriented. So my start point is the loc/rot of the object. I am looking for a way to calculate a second point that is X distance away from the start point, along the direction the object is facing.

Here is kind of what I have.

``````
# Fetch the spline that makes up this weapons path.
spline = temp_ob.data.splines
p1 = spline.bezier_points
p2 = spline.bezier_points
point_on_curve = temp_mt.matrix_world * temp_mt.location
locator_point = temp_loc.matrix_world * temp_loc.location
start_point = point_on_curve + locator_point
p1.co = start_point

<b>axis = Vector((1,0,0))</b>

end_point = point_on_curve +(axis*100.0)        # Length of laser..?
p2.co = end_point

``````

I think what I have could work, but I don’t know how to generate a valid axis = Vector((1,0,0))

normalize the direction vector and multiply it by the distance you wanna get (in your example 100)

Hmm…I really am math challenged. How do I make a direction vector from LOC/ROT? Is Normalize part of mathutils?

Let’s say you have a cube and you consider the face pointing towards its positive Y (local axis) the “facing direction”:

``````ob = bpy.context.object # the cube
mat = ob.matrix_world

rot = mat.to_3x3()
loc = mat.to_translation() # our origin, could also use ob.location

direction_unit_vector = rot.col # +Y direction
# Note: as this is taken from matrix_world, it's already normalized -&gt; length = ~1

point_a = loc
point_b = loc + direction_unit_vector * 100 # length of 100 units
``````

If the direction vector isn’t normalized, you can use Vector.normalized() from mathutils. But it wouldn’t be hard to normalize manually either:

``````
v = Vector((2, 3, 5))

# calculate length, remember Pythagoras' theorem!
length = sqrt(v*v + v*v + v*v) # square and add together the vector components, then take the square root

# divide the vector components by the vector length to get the unit vector (vector with length of 1)
v_unit = Vector((v / length, v / length, v / length))

# Or make use of the mathutils.Vector methods:
v_unit = v / v.length

# Or simply use the provided normalized() method:
v_unit = v.normalized()
``````

Thanks for the math lesson.

In my case, I can use the origin as the location, but I will have to factor in all 3 axis for rotation.

Can I just add them together?

``````
<b>direction_unit_vector = rot.col + rot.col + rot.col</b>
``````

I guess I still need to know how to construct a directional vector from a XYZ rotation.
.
.
.
or would it be?

``````direction_unit_vector = Vector((rot.col,rot.col,rot.col))
``````

you can’t construct a direction from a rotation. It’s like finding the facing direction of a sphere - there is simply none (or every normal is). You need to decide what “facing” means, e.g. “+Y axis is the looking direction”.

[ATTACH=CONFIG]216369[/ATTACH]

A rotation matrix (3x3) is basically 3 unit vectors pointing in three different directions, towards local X, Y and Z. If you decide the facing direction is +Y, it would be the green arrow.

If you combined all 3 vectors, you would probably get a vector pointing towards the viewer in above illustration

``````you can't construct a direction from a rotation
``````

Ok, it seems like this should still be solvable.

What I am trying to do is cast a ray X units in the direction the object is facing. Basically I need a point returned that is X units away from the casting point.

Is that possible?

Yes! But you must know the facing vector at start point (p0 on your code)
Assuming that your object in p0 is facing it’s path parametrized by 0<t<1

``````
laser_length = 100 #or what you want
laser_vector = (p(t) - p0).normalized() #p(t) is the first next point on path when t=0
#for every point on path
laser_beam = p(t) + (laser_vector * matrix_rotation) * laser_length #p(t) active point on path at time t
#matrix_rotation = matrix that rotate your object

``````

you can turn any two points or a line into a direction and place something x units away from one of these points or any other start point