简体   繁体   中英

Using aTan2 to face an object to its destination

I've created an object that moves towards its destination with inertia. I am having alot of trouble getting the object to face its destination. My code is simple, it calculates the angle, converts it to degrees and passes that angle to the Matrix4 Rotate function, which adjusts the localTransform (scenegraph).

The problem is that the object spawns, and then just rotates endlessly. It slowly progresses towards its target, but just keeps spinning. I've tested it without translation, it spins regardless on the spot. All I need is for the object to face its destination. My Translate/Rotate functions work correctly, I've used it to rotate an object, have an object spawn with its parent's rotation and head in that direction. They provide 1:1 results with the GLM library.

I've tried swapping the order in aTan2, removing the degrees conversion, (though that does nothing, the Rotate function takes degrees) and swapping translation/rotation order.

localTransform is the combined rotation/scale/translation matrix. row[3]column[1] is Y. [3][0] is X.

float fAngle = atan2(v3Destination[1] - localTransform.data[3][1] , v3Destination[0] - localTransform.data[3][0]);
float fAngleDegrees = fAngle * 180 / PI;
localTransform = Matrix4::Rotate(localTransform, fAngleDegrees, Vector3(0.0f, 0.0f, 1.0f));

Vector3 Movement;
Movement[0] = v3Destination[0] - localTransform.data[3][0];
Movement[1] = v3Destination[1] - localTransform.data[3][1];

Movement = Movement * fSpeed * Application.GetTimeStep();
localTransform = Matrix4::Translate(localTransform, Movement);

Any advice on how to handle this? This is all in 2D coordinates, however the rotation is done on the Z-Axis.

Just a hunch, but is the localTransform matrix completely recomputed each time step?

Or could you be adding a rotation to a matrix that's already been rotated in the previous iteration.

This could explain the continuous rotation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM