0

I want to gradually apply a matrix4 on an object, in a function that updates every frame.

If i have two matrices, what is the way to know the difference between them. let say i would like to know the matrix that represent the first one + 0.2 of the difference between the two.

WestLangley
  • 102,557
  • 10
  • 276
  • 276
Normal
  • 528
  • 4
  • 19
  • 1
    You can't directly interpolated between matrices. [This question](https://stackoverflow.com/questions/3093455/3d-geometry-how-to-interpolate-a-matrix) might give you more insights. – BDL Nov 19 '17 at 17:30
  • And if it is a matrix that has only rotation data? – Normal Nov 19 '17 at 21:54
  • 1
    No. Especially with rotations it is not possible. What you usually want is a linear interpolation of the rotation angle, but the matrix does only contain the sin and cos of the angle. You'd be interpolating that which gives weird looking results. Decompose the matrix, interpolate the angle and then compose a new matrix. – BDL Nov 19 '17 at 21:59

1 Answers1

2

You should look into THREE.Quaternion.slerp and THREE.Vector3.lerp methods.

Slerp stands for "spherical linear interpolation" while lerp stands for "linear interpolation".

A matrix then has to be constructed by three based on these two, but three handles this internally if you set myObject3D.position and myObject3D.quaternion.

If your starting point is a matrix4, you can decompose it into quaternions and vectors, and then compose one from the new results. If you want just the end result in a matrix you can use makeRotationFromQuaternion( quaternion ) followed by setPosition( position ).

pailhead
  • 5,162
  • 2
  • 25
  • 46
  • 2
    Should probably add that you can use `matrix.decompose(position, quaternion, scale)` to get those values from a matrix and `matrix.compose(position, quaternion, scale)` to get the values back into matrix-form. – Martin Schuhfuß Nov 20 '17 at 14:11