Finding the angles for the X, Y and Z axis in 3D - OpenGL/C++

 ̄綄美尐妖づ 提交于 2019-12-04 05:52:17

If your cube is at A = (x0,y0,z0)

If your cube is currently looking at B=(x1,y1,z1)

and if you want it to look at C=(x2,y2,z2) then;

let v1 be the vector from A to B

v1 = B - A

and v2 be the one from A to C

v2 = C - A

First normalize them.

v1 = v1 / |v1|
v2 = v2 / |v2|

then calculate the rotation angle and the rotation axis as

angle = acos(v1*v2) //dot product
axis = v1 X v2 //cross product

You can call glRotate with

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