Rotating Towards Path in OpenGL

你离开我真会死。 提交于 2020-01-03 03:42:08

问题


I am having trouble on how to rotate an object in 3D space, based on the OpenGL rotate function glRotatef(..).

glRotatef ( angle , x , y , z )

My object is traveling from one point to another in 3D space. I want my object to rotate in the direction it is traveling.

How would I find the angle, x, y, z that is needed for the glRotatef(...) function if I know the point where I am starting and the point where I am finishing.


回答1:


The gluLookAt function is exactly what you need here. It will save you from calculating the axis and angle manually.




回答2:


Very crudely, what you need is to get the axis of rotation, which will be normal to both the direction of travel and the surface normal, which would mean a cross product of the direction of travel (T) and surface normal (n) would give you an axis for your rotation. Thus axis(A) would be:

A = norm(T x n) or maybe A = norm(n x T), one of which will let you use a positive angle to "rotate" in the right direction.

However, the positive or negative nature of the angle of rotation needs to be analyzed depending on which one of the above you use, but this should get you started with the axis of rotation :)



来源:https://stackoverflow.com/questions/4384155/rotating-towards-path-in-opengl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!