set z axis to the same as another object

Started by slenkar, May 30, 2009, 04:03:04 AM

Previous topic - Next topic

slenkar

'align' copies the x axis and the y axis

How do I get the zaxis of an object and copy it to another objects zaxis (e.g. the camera)

EgonOlsen

I don't get the question. align() uses the rotations matrices to align one object to another (or the camera). It doesn't exclude any axis. And even if it would, if x and y are set, z is implicitly set too!?

paulscode

#2
First note that there are an infinite number of possible orientations that an object may be in if you only define the z-axis.  If you imagine the z-axis as a pole sticking out from the front of an object, that object could freely rotate around it and could be upside down, sideways, etc.

Therefore, there are several ways to match the z-axis of one object to that of another, depending on how you want to handle the remaining two axis'.  Here is one possible way (not yet tested) using a method I wrote a while back which works like the Camera's lookAt method, except it's for Object3Ds:

public void lookAt( Object3D object, SimpleVector target )
{
    float initialScale = object.getScale();
    object.setScale( 1.0f );
    SimpleVector direction = new SimpleVector(
                  target.calcSub( object.getTransformedCenter() ) ).normalize();
    Matrix rotationMatrix = new Matrix( direction.getRotationMatrix() );
    object.setRotationMatrix( rotationMatrix );
    object.setScale( initialScale );
}


So what you would do is add the target object's z-axis to the position of the source object, then have the source object "look at" that point:

SimpleVector lookAtPoint = sourceObject.getTransformedCenter();
lookAtPoint.add( targetObject.getZAxis() );
lookAt( sourceObject, lookAtPoint );


--EDIT--
Oh, and of course if the Camera is what you are moving, you'd just use its getPosition() and lookAt() methods:
SimpleVector lookAtPoint = myCamera.getPosition();
lookAtPoint.add( targetObject.getZAxis() );
myCamera.lookAt( lookAtPoint );


slenkar

#3
thanks for 'the codez' they work very well