Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get translation from matrix4()

Tags:

three.js

It may look like a dummy question for most of the three.js developers but, how can a translation be extracted from the transformation matrix?

Actually I am extracting it manually pointing to the matrix array positions (12, 13, 14).

Thanks in advance.

like image 406
juagicre Avatar asked Jan 28 '16 13:01

juagicre


2 Answers

If you want to extract the translation component from the matrix, then use this pattern:

var vec = new THREE.Vector3();
vec.setFromMatrixPosition( matrix4 );

Use Matrix4.decompose() only if you need the translation, quaternion and scale components.

three.js r.73

like image 186
WestLangley Avatar answered Nov 09 '22 23:11

WestLangley


Matrix4 has a decompose method that is capable of exactly that.

var mat = /* ... */;
var translation = new THREE.Vector3(),
  rotation = new THREE.Quaternion(),
  scale = new THREE.Vector3();
mat.decompose(translation, rotation, scale);

Of course, if you are only interested in the translation then you are best off doing what you are currently doing. Behind the scenes, decompose simply extracts elements 12, 13, and 14 from mat.elements.

like image 40
Brandon Avatar answered Nov 09 '22 23:11

Brandon