WebGL - 沿行进方向旋转对象
在此示例中: http://deeplogic.info/project/webGL/
如何旋转对象朝它行进的方向?
In this example: http://deeplogic.info/project/webGL/
How can I rotate the object in the direction in which it is travelling?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设你的物体正在向 D 方向移动,那么你需要做的就是找到垂直于该方向的向量。如果您的运动仅在一个平面上,您可以通过 D 与平面法线 N 的叉积来找到该向量 E。
这会产生 3 个向量 D、E 和 N。归一化后,它们形成旋转坐标的基础系统。您可以将它们放入 3×3 矩阵的列中,
将该矩阵扩展为 4×4 同质矩阵
,然后可以使用 glMultMatrix 将其传递给 OpenGL,以将其应用到矩阵堆栈上。
Say your object is moving into direction D, then all you need to do is finding a vector perpendicular to that direction. In case your movement is in only one plane you can find this vector E by taking the cross product of D with the plane normal N.
This yields you with 3 vectors D, E and N. After normalization those form the base of a rotated coordinate system. You can put them into the columns of a 3×3 matrix
extend this matrix into a 4×4 homogenous one
and you can pass it to OpenGL with glMultMatrix to apply it on the matrix stack.
要使沿 (x, y) 方向移动的对象绕 z 旋转,您可以使用
Math.atan2(y,x)
方法。它将返回一个以弧度为单位的角度。 ES 2 之前的 OpenGL 约定以度为单位,但现在它取决于您必须将变换推送到顶点着色器的任何代码。不过,您可能需要进行转换,在这种情况下,只需将结果乘以 180(以度为单位的半圆),然后除以 pi(以弧度为单位的半圆)。To get rotation around z for an object moving in the direction (x, y) you can use the
Math.atan2(y,x)
method. It'll return an angle in radians. The OpenGL convention prior to ES 2 was to work in degrees, but nowadays it's up to whatever code you have to push transformations to the vertex shader. You'll probably want to convert though, in which case just multiply the result by 180 (half a circle in degrees) and divide by pi (half a circle in radians).