Quick answer: local y axis vector?


I’m trying to find the 3d vector that is parallel to the local y-axis of a python controlled game object (relative to the global axis). If that isn’t easy, the alternative is to find the equation of the line of the local y-axis in global units OR to get a point that is like [0,1,0] local from the center of the object, but expressed in the global coordinates. From this, the vector could also be calculated.

in case that’s a bit confusing, let me clarify:
if an object has y axis the same as the global y axis, python would return [0,1,0], a point/position vector that goes just along the y axis. But if the object were rotated 90 degrees and the local y axis were in line with the global x axis, it would return [1,0,0] as the position vector/point, which would tell me “the local y axis of the object goes directly along the global x-axis”

I’ve seen somewhere the use of “object.y” for dealing with aspects of a local axis of an object, but I just don’t know how it works. It’s also possible this is incredibly simple and I’m overlooking an easy function, but I just don’t know :wink: Any help would be appreciated!

Could you not use a vector of the objects position, add orientation to get local vector?
In this example, change the keyword “axis” to x,y or z

import bge

cont = bge.logic.getCurrentController()
own = cont.owner

pos = own.worldPosition
ori = own.worldOrientation
axis = "x"

if axis == "y":
    vec2 = ori[0]
elif axis == "y":
    vec2 = ori[1]    
    vec2 = ori[2]
vec = pos+vec2
vec.magnitude = 5

hey, thanks for the reply :slight_smile:
I actually figured it out like an hour after posting, and yes it was simple, just using the orientation like above, except:
orientation_of_y_axis = own.worldOrientation[1]
and then the vector is as you suggested above, the world position +/- this unit vector above. I guess I just didn’t quite understand what worldOrientation returned :wink:

Pleasure to be of service, and well done for figuring it out!

There is also KX_GameObject.getAxisVect(). To get a world space vector that represents an objects local space y axis:

forward_vec = myObj.getAxisVect((0, 1, 0))

Here is the API entry for it: http://www.blender.org/documentation/blender_python_api_2_57_release/bge.types.html#bge.types.KX_GameObject.getAxisVect

Oh, well there you go!

just for understanding:

worldOrientation returns the rotation matrix of the object from its local space into scene space (world space).

This means

worldVector = worldOrientationMatrix * localVector
90° around Z

( 0 -1  0 )   ( 0 )
( 1  0  0 ) * ( 1 )
( 0  0  1 )   ( 0 )
  (-1 ) 
= ( 0 )
  ( 0 )

as your localVector is the vector along +y with a length of one (0,1,0) you can reduce the formula to:

world Vector = worldOrientationMatrix(YPart)

in the example:

( # -1  # )   ( 0 ) <- it is zero wich makes the X-part zero (#)
( #  0  # ) * ( 1 ) <- it is one wich leaves the Y-part unchanged
( #  0  # )   ( 0 ) <- it is zero wich makes the Z-part zero (#)
  (-1 ) 
= ( 0 )
  ( 0 )

For details you can look at the wiki for transformation matrix.