This content has been marked as final. Show 4 replies
I solved it partly, by setting the .x and .y parameters of object.transform.rotation to 0.
But it still doesn't work just right... When I drive past the object it gets rotated 180 degrees in z, resulting in the back pointing at the target instead of the front.
Anyone experienced the same?
Have you looked at the pointAtOrientation property of 3D objects?
Alternatively you can do some 3D mathematics, and rotate the model by hand. To rotate your model, you can use the syntax:
myModel.rotate(vPosition, vAxis, vAngle, #world)
You'll need to work out vAxis and vAngle from the current orientation of the model:
vPosition = myModel.worldPosition
vZAxis = myModel.getWorldTransform().zAxis -- current
vPointToPointAt = <some vector>
vDirection - vPointToPointAt - vPosition -- desired
vAxis = vZAxis.cross(vDirection)
vAngle = angleBetween(vZAxis, vDirection)
If vAxis has a magnitude of 0, but vAngle is not 0, then your model is pointing in diametrically the opposite direction: vAngle will be 180. Rotating your model about any axis of rotation will make it point in the right direction, but it will probably be tilted or upside down. You will need to choose your rotatition vector with care to make sure that your model ends up the right way up.
I usually use a utility node (in this case, a SW3D group) for something like this:
-- limit pointAt() to y-axis rotation
-- +y is Up
-- pointAt() orients nodes along local negative z-axis
gUtilGroup.transform.position = pEntityModel.transform.position
gUtilGroup.transform.position.y = pTargetPosition.y
pEntityModel.transform.rotation = gUtilGroup.transform.rotation
-- global dependencies (need to be defined during scene setup):
gUtilGroup = gSW3D.newGroup("UtilGroup")
gUp = vector(0,1,0)
Another thing to watch out for, pointAt() will generate an "Invalid Point" error if the node's position coincides with the target position.
Thank you guys, you really helped me out.
Edit: To Openspark: Yes, I checked the pointAtOrientation but to no avail. I experimented with it but couldn't get it to work.