### Author Topic: set z axis to the same as another object  (Read 2163 times)

#### slenkar

• byte
• Posts: 20
##### set z axis to the same as another object
« on: May 30, 2009, 04:03:04 am »
'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

• Posts: 11777
##### Re: set z axis to the same as another object
« Reply #1 on: May 30, 2009, 08:42:01 am »
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

• double
• Posts: 863
##### Re: set z axis to the same as another object
« Reply #2 on: May 30, 2009, 01:08:22 pm »
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:

Code: [Select]
`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:

Code: [Select]
`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:
Code: [Select]
`SimpleVector lookAtPoint = myCamera.getPosition();lookAtPoint.add( targetObject.getZAxis() );myCamera.lookAt( lookAtPoint );`
« Last Edit: May 30, 2009, 01:11:41 pm by paulscode »

#### slenkar

• byte
• Posts: 20
##### Re: set z axis to the same as another object
« Reply #3 on: May 30, 2009, 05:14:00 pm »
thanks for 'the codez' they work very well
« Last Edit: May 30, 2009, 05:38:05 pm by slenkar »