# How can I get a 4x4 matrix from a bullet quaternion?

I'm trying to make a game using JavaScript with Ammo.js ( an implementation of bullet ) and Sylvester to do my matrix maths. The problem is I don't know how to turn the WXYZ that bullet gives into the 4x4 matrix Sylvester needs.

If there is a way to just get Bullet Physics to give me the 4x4 matrix please tell me! I am completely lost here.

Three.JS has a function for this based upon this principle. BTW Matrix4x4 are represented internally as Column-Major, not sure what bullet uses.

Three.js Matrix4x4.makeRotationFromQuaternion(Quaternion)

makeRotationFromQuaternion: function ( q ) {

var te = this.elements;

var x = q.x, y = q.y, z = q.z, w = q.w;
var x2 = x + x, y2 = y + y, z2 = z + z;
var xx = x * x2, xy = x * y2, xz = x * z2;
var yy = y * y2, yz = y * z2, zz = z * z2;
var wx = w * x2, wy = w * y2, wz = w * z2;

te[ 0 ] = 1 - ( yy + zz );
te[ 4 ] = xy - wz;
te[ 8 ] = xz + wy;

te[ 1 ] = xy + wz;
te[ 5 ] = 1 - ( xx + zz );
te[ 9 ] = yz - wx;

te[ 2 ] = xz - wy;
te[ 6 ] = yz + wx;
te[ 10 ] = 1 - ( xx + yy );

// last column
te[ 3 ] = 0;
te[ 7 ] = 0;
te[ 11 ] = 0;

// bottom row
te[ 12 ] = 0;
te[ 13 ] = 0;
te[ 14 ] = 0;
te[ 15 ] = 1;

return this;

},

• I will try this again tomorrow - put it in my code with an error 'TypeError: Cannot set property '0' of undefinedâ†µ â€¦//blockhaven.net/dev/BHJSP/blockhaven.js:1054:24)", message: "Cannot set property '0' of undefined' – MineMan287 Oct 13 '14 at 22:04
• You obviously can't just copy and paste it in... this.elements wouldn't exist if you did so. Try instantiating an array of size 16 for the variable te. – ClassicThunder Oct 13 '14 at 22:07
• facepalm I edited it but forgot to initialise the array I just set it to return te and I just set the line to 'var te' – MineMan287 Oct 14 '14 at 19:58