Public Group

# Rotating to a specific orientation

This topic is 4000 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi guys, I've been working on a top-down 3d shooter game where I'd like to rotate my avatar toward a specific orientation, ie. the mouse pointer on the screen. I've gotten the screen coord transformed into world coordinates, but I'm having a bit of difficulty matching the correct orientation so it doesn't oscilate back and forth. The way I've coded it so far is roughly:
void Avatar::Update(float deltaTime)
{
// First get direction facing
Vec3 d = mRotationMatrix*Vec3(1,0,0);

// Which way to turn?
Vec3 u = CrossProduct(d, mTarget);

if(!IsCloseToZero(u.z))
{
bool clockwise = (u.z<0.0f);
Matrix3 rotate;
rotate.RotateZ(PI*0.25f*deltaTime*(clockwise?-1.0f:1.0f));
mRotationMatrix = mRotationMatrix*rotate;
}
}


The rotation works so and so. It rotates toward the desired target rotation, but once it hits it, it flickers back and forth. I'm coding it in OpenGL, and I'm starting to get the feel of how matrices and vectors work, but I cannot get my head around this specific problem... I'm sure there's a pretty easy solution that I haven't thought of :) Any help is much appreciated.

##### Share on other sites
The reason it shifts back and forth is that you rotate by a specific amount, which in some cases is too much, and then you rotate back the other way... back and forth.

One possible way to fix this is to replace IsCloseToZero() so that it detects whether the specified rotation would overshoot (hint: the magnitude of the cross product is the sin of the angle, so take the arcsin to get an angle), and if it would, replace the rotation with a forcible setting to face the target (hint: you could find the angle between the current facing and the location-to-target vector, and then rotate, but it's probably much easier to just normalize the location vector and directly set the velocity accordingly :) ).

##### Share on other sites

I solved the problem now with your approach, and coupled with a heading variable for my avatar. Then instead of post-multiplying a delta rotation to my rotation matrix, I just set the rotation matrix directly to what the heading is updated to. Works flawlessly :)

1. 1
2. 2
Rutin
23
3. 3
JoeJ
20
4. 4
5. 5

• 27
• 40
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631737
• Total Posts
3001944
×