setRotation() rolls object

Hi… I am using the setRotation method to point an object to a certain point. The problem I am having is that the method not only points the object to the point, but also rolls the object somewhat so that its local XY plane is now at an angle to the global XY plane. Can anyone tell me how to prevent this?:confused:

Use setOrientation, for some reason setRotation doesn’t work properly (?). Or you could use logic bricks for ease.

Tutorial by Social on Orientation: http://socialstorage.googlepages.com/orientationmatrix-basics

As far as I know, there is no such thing as a setRotation() method.

Do you mean alignAxisToVect()?

You’re right. But there’s an applyRotation() method.

 <b>applyRotation</b>


  <b>applyRotation(rotation, local):</b>

  <b>Note:</b>
  Object type: Occluder, No collision, Static, Dynamic and Rigid body
 
Rotate the game object a set rotation (works like Motion Actuator Rot).
       
rotation:
Type:  3D Vector

 local
Type:  Bool
  1 = True
Apply force using local (game object) axis

 
   0 = False
Apply force using world axis
 
 
 

sorry, meant setOrientation() :o

is this new in 2.49?

applyRotation is new- as far as I can tell, it just allows you to access dRot without a logic brick.

When using alignAxisToVect, I find that I always have to do one align to point the object, and then a second using the vector [0,0,1] and the Z axis to get teh object upright again (that is, if you want a 2d track, where the object and global Z axis are always aligned- if you want the object to be able to tilt, as is useful for a camera, do the [0,0,1] align before the other one)

In case anyone is interested, the following code will set an object’s orientation while keeping it upright (this code is from a tutorial, the author of which I cannot remember, unfortunately):

def euclidSize(vec):
“”"
Calculates the Euclidean size of a 3D vector.
“”"
return sqrt(vec[0]*vec[0]
+ vec[1]*vec[1]
+ vec[2]*vec[2])

def cross(vec1, vec2):
“”"
Calculates the crossproduct of two 3D vectors.
“”"
return [vec1[1]*vec2[2]-vec1[2]*vec2[1],
vec1[2]*vec2[0]-vec1[0]*vec2[2],
vec1[0]*vec2[1]-vec1[1]*vec2[0]]

def pointToUpright (pos0 , pos1 ):
“”"
Calculates the orientation of
an object sothat its local y- axia
can point to another object . The
arguments are the positions of the
two objects , with pos1 the position
of the target object
“”"

y = [ pos1 [0] - pos0 [0] ,
      pos1 [1] - pos0 [1] ,
       pos1 [2] - pos0 [2]]

size = euclidSize (y)

for i in range (0 , 3):
    y[i ] = y[i ] / size # nomalise size

x1 = sqrt (1 - y [1]* y[1] - y [2]* y [2])

#z = cross ( cross (y , [0 , 0 , 1]) , y)

z = [ -y [0]* y[2] ,
      -y [1]* y[2] ,
       y [0]* y [0] + y [1]* y [1]]

size = euclidSize (z)

for i in range (0 , 3):
    z[i ] = z[i ] / size # nomalise size

x = cross (y , z)

res = [[] , [] , []]

for i in range (0 , 3):
    res[i ] = ( x[i], y[i], z[i])

return res

Just tested it and it works well…is this what you were having trouble with?

For the fun of it, I decided to rewrite it with the latest in BGE technology:

def pointToUpright(object, target, up=(0,0,1)):
    """
    Object is always a game object. Target can be a
    point. I think you could change up, but I'm not
    sure.
    """
    from Mathutils import Vector as v
    
    y = v(object.getVectTo(target)[1])
    
    z = y.cross(v(up)).cross(y)
    z.normalize()
    
    x = y.cross(z)
    
    res = [[],[],[]]
    for i in range(3):
        res[i]=(x[i],y[i],z[i])
    
    return res

This is the solution to what I was having trouble with.