Three.js - Convert mesh velocity to mesh rotation

后端 未结 1 1267
臣服心动
臣服心动 2021-01-24 20:31

Suppose I have a three.js mesh, and a velocity vector3.

The velocity is being altered elsewhere, and then added to the mesh.position every frame.

What I want is

相关标签:
1条回答
  • 2021-01-24 20:57

    You can construct a rotation matrix from the normalized velocity vector. The vector, normalized, is 1 axis. Then you pick a 2nd axis somehow. Then you do a cross product to find the 3rd axis. Finally do a cross product between the first and 3rd to get the final 2nd axis.

    Then you construct a 3x3 matrix from the 3 vectors (i can't remember their order at the moment, but something like [x1,y1,z1,x2,y2,z2,x3,y3,z3]).

    Another method is the Quaternion class:

    .setFromUnitVectors ( vFrom, vTo )

    Sets this quaternion to the rotation required to rotate direction vector vFrom to direction vector vTo. Adapted from http://lolengine.net/blog/2013/09/18/beautiful-maths-quaternion-from-vectors. vFrom and vTo are assumed to be normalized.

    http://threejs.org/docs/#Reference/Math/Quaternion

    Since internally three.js likes to use quaternions, this may be a good method. If your arrow is an upward pointing arrow, you probably want vFrom=[0,1,0], vTo=velocityVector.normalize();

    0 讨论(0)
提交回复
热议问题