Python: Getting a point in space along local axis?


How do I get a point (3-tuple [x,y,z]) that is ‘in-front’ of the object? Say I want to get a point that is a + 20 units along the gameobjects local y-axis.

I think I might have to a vector to get this, but I just can’t figure out how to put a Vector and a 3-tuple together to get a position along an objects local axis. I was going
to use a second object, align it to the first objects position and orientation, then move the second object along it’s local y-axis, then use the second object to get the position. but this was going to get messy quickly as I am needing to get quick a few positions that are offset from an object.

I hope this makes sense, I’m finding it difficult to describe my problem, but hopefully I got enough in there :slight_smile:

worldVector * worldOrientation = localVector

Let’s say your object is at position [x,y,z] and you want a point three units in front of this (+y-axis).

If we simply add [0,3,0] to [x,y,z] the point will always be three units towards the world +y-axis. This only works if the game object is always pointing towards the world +y-axis. What if our object is pointing towards the +x-axis? Or the +z-axis? Or a combination?
This is you’re question right? haha

Anyways, we know now that the point in front of the object on the +y-axis relies on the orientation of the object.
So we can conclude that if we multiply the vector (point in front of object) by the orientation of the object, this must give us the local vector.
Here’s how you would do it in python.

from mathutils import Vector
object.worldPosition + object.worldOrientation*Vector([0,3,0])

Amazing reply Linkxgl. Thank you for explaining that so well.

Its about time I polished up my knowledge of Vectors and Matrices, I keep getting stumped by similar problems, just because of my short knowledge of them. But your solution makes complete sense to me and has helped me understand them a bit.

Thanks :slight_smile:

At the above solution the scale is missing.

I recommend

gameObject.worldTransform * Vector([0,3,0])

since 2.62 there is no need to reconstruct the transformation matrix manually.
I do not know why the it is called transform rather then transformation. Regardless of that this attribute comes pretty handy ;).

Just wondering Monster,

What’s the different between

gameObject.worldOrientation * Vector([0,3,0])


gameObject.worldTransform * Vector([0,3,0])

What happens in-game if I use the first solution instead of the second? Or what are the possible issues?

worldTransform includes scale and position as well as orientation. Thus there is support for scaled objects

Don’t forget that worldTransform holds position data. This means you don’t need to add the result of multiplying worldTransform and your vector with worldPosition.


I’m just used to doing this the 2.59 way. On top of that, I’ve never had problems with the scale, so I never had problems with the old setup.