开发者

OpenGL: glRotate: determining the 'angle' component

I need some help with math and OpenGL.

I want to make some object follow another one, that is, turn its face to another object every time the former moves. So I learned about glRotate.

I thought I would obtain the (x, y, z) of the former everytime it moves and then send its coordinates开发者_JS百科 to another function which should update the 'facing to' property of the latter. But, how do I find the 'angle' parameter to glRotate based on both new and old direction of the follower object?

Any help is highly appreciated. Thanks for you time.


Let's assume that your observer is at 0,0,0 and your have an old target position O and a new target position N.

Then the angle difference around the Y axis in radians is:

float angle = atan2(N.z,N.x) - atan2(O.z,O.x);

Since glRotate accepts degrees, you want convert that to degrees:

angle = (angle * 180.f)/M_PI;

How does this work? You project both vectors to the XZ plane. I just ignore the x component for that. Then you take their angle respective the X axis (That arctan does that) and take the difference.

Edit: Fixed the angle computation, thanks to ja72 for pointing out that it was catastrophically broken.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜