# BGE: How to set the Constraint Axis to an object coordinate

I need to constrain an object using the axis of another object (my problem is related to the axis direction: there is no "distance between objects" constraint; you have to use the local or global axis to check). So, I made this parented object that tracks the whole time to another one, and from it I want the Y axis.

Using this tutorial for reference, I did the following:

#CONSTRAINT
direction = cont.actuators['Direction'] #the constraint actuator
face = sce.objects['Facer'] #the facing object
faco = face.localOrientation[1] #here we are (this gives me a 3x3 matrix)
#but i only want to use the Y value
#I’ve tried out even 0, and 2 but wont work

def constra():
direction.direction = faco #this isn't running as it should
cont.activate(direction)


There are no errors in the terminal. It's just not doing what it should do. I think I'm wrong, and he didn't want the orientation of the object, but some other stuff maybe...

• Not certain if it's available within BGE but in the 3DView you can create new Transform Orientations. These orientations are based on the xyz orientation of the selected object at the time of creation. It may be possible to create a new one at the time you want to match the object movement and use that transform to move. OR... create empty, copy target orientation to empty, make empty the parent of object to move, move empty, unparent, delete empty(or reuse it next time). – sambler Aug 14 '13 at 6:40

Does the object need to be parented? I will make an example that presumes it doesn't. If you explicitly need parented objects, this script will not work as it is.

If you transform the orientation matrix of the source object into an euler and then pass it on to the target object's world orientation as a matrix, you should get what you are looking for.

In a nutshell: get matrix -> change to euler -> modify euler -> change to matrix -> give to the object you want

Hook this up to a TRUE level pulse triggering sensor with a frequency of your choice.

import bge
import mathutils

sce = bge.logic.getCurrentScene()
face = sce.objects['Facer']
facY = face.worldOrientation.to_euler()[1] #Now we have the Y-axis rotation
facTar = sce.objects['Facingtarget']       #This is the target object we modify
newRotList = [facTar.worldOrientation.to_euler()[0],facY,facTar.worldOrientation.to_euler()[2]] #Now we have a list
newRot = mathutils.Euler(newRotList).to_matrix()   #Now we have the matrix we need
facTar.worldOrientation = newRot    #The orientation has been modified

• Solved another way, unfortunately I was already using the orientation system on the same object, so I was needing the precise matrix to give the actuator, but now works, so I don`t worry anymore, thanks. – Pivetta Aug 18 '13 at 18:17