r3-legacy/src/game-lib-api-matrix4.js

147 lines
4.2 KiB
JavaScript
Raw Normal View History

/**
* Api Matrix 4
2016-12-09 20:32:09 +01:00
* @param row0 GameLib.D3.API.Quaternion
* @param row1 GameLib.D3.API.Quaternion
* @param row2 GameLib.D3.API.Quaternion
* @param row3 GameLib.D3.API.Quaternion
* @constructor
*/
GameLib.D3.API.Matrix4 = function ApiMatrix4(
row0,
row1,
row2,
row3
) {
this.rows = [];
if (GameLib.D3.Utils.UndefinedOrNull(row0)) {
2016-12-09 20:32:09 +01:00
row0 = new GameLib.D3.API.Quaternion(1, 0, 0, 0);
}
this.rows[0] = row0;
if (GameLib.D3.Utils.UndefinedOrNull(row1)) {
2016-12-09 20:32:09 +01:00
row1 = new GameLib.D3.API.Quaternion(0, 1, 0, 0);
}
this.rows[1] = row1;
if (GameLib.D3.Utils.UndefinedOrNull(row2)) {
2016-12-09 20:32:09 +01:00
row2 = new GameLib.D3.API.Quaternion(0, 0, 1, 0);
}
this.rows[2] = row2;
if (GameLib.D3.Utils.UndefinedOrNull(row3)) {
2016-12-09 20:32:09 +01:00
row3 = new GameLib.D3.API.Quaternion(0, 0, 0, 1);
}
this.rows[3] = row3;
};
GameLib.D3.API.Matrix4.prototype.rotationMatrixX = function (radians) {
this.identity();
2016-12-09 20:32:09 +01:00
this.rows[1] = new GameLib.D3.API.Quaternion(0, Math.cos(radians), -1 * Math.sin(radians), 0);
this.rows[2] = new GameLib.D3.API.Quaternion(0, Math.sin(radians), Math.cos(radians), 0);
return this;
};
GameLib.D3.API.Matrix4.prototype.rotationMatrixY = function (radians) {
this.identity();
2016-12-09 20:32:09 +01:00
this.rows[0] = new GameLib.D3.API.Quaternion(
Math.cos(radians),
0,
Math.sin(radians),
0
);
2016-12-09 20:32:09 +01:00
this.rows[2] = new GameLib.D3.API.Quaternion(
-1 * Math.sin(radians),
0,
Math.cos(radians),
0
);
return this;
};
GameLib.D3.API.Matrix4.prototype.rotationMatrixZ = function (radians) {
this.identity();
2016-12-09 20:32:09 +01:00
this.rows[0] = new GameLib.D3.API.Quaternion(Math.cos(radians), -1 * Math.sin(radians), 0, 0);
this.rows[1] = new GameLib.D3.API.Quaternion(Math.sin(radians), Math.cos(radians), 0, 0);
return this;
};
GameLib.D3.API.Matrix4.prototype.rotateX = function (radians, point) {
this.identity();
this.rotationMatrixX(radians);
return this.multiply(point);
};
GameLib.D3.API.Matrix4.prototype.rotateY = function (radians, point) {
this.identity();
this.rotationMatrixY(radians);
return this.multiply(point);
};
GameLib.D3.API.Matrix4.prototype.rotateZ = function (radians, point) {
this.identity();
this.rotationMatrixZ(radians);
return this.multiply(point);
};
GameLib.D3.API.Matrix4.prototype.multiply = function (mvp) {
2016-12-09 20:32:09 +01:00
if (mvp instanceof GameLib.D3.API.Quaternion) {
return new GameLib.D3.API.Quaternion(
this.rows[0].x * mvp.x + this.rows[0].y * mvp.y + this.rows[0].z * mvp.z + this.rows[0].w * mvp.w,
this.rows[1].x * mvp.x + this.rows[1].y * mvp.y + this.rows[1].z * mvp.z + this.rows[1].w * mvp.w,
this.rows[2].x * mvp.x + this.rows[2].y * mvp.y + this.rows[2].z * mvp.z + this.rows[2].w * mvp.w,
this.rows[3].x * mvp.x + this.rows[3].y * mvp.y + this.rows[3].z * mvp.z + this.rows[3].w * mvp.w
);
} else if (mvp instanceof GameLib.D3.API.Vector3) {
return new GameLib.D3.API.Vector3(
this.rows[0].x * mvp.x + this.rows[0].y * mvp.y + this.rows[0].z * mvp.z,
this.rows[1].x * mvp.x + this.rows[1].y * mvp.y + this.rows[1].z * mvp.z,
this.rows[2].x * mvp.x + this.rows[2].y * mvp.y + this.rows[2].z * mvp.z
);
}
};
GameLib.D3.API.Matrix4.prototype.identity = function () {
this.rows = [
2016-12-09 20:32:09 +01:00
new GameLib.D3.API.Quaternion(1, 0, 0, 0),
new GameLib.D3.API.Quaternion(0, 1, 0, 0),
new GameLib.D3.API.Quaternion(0, 0, 1, 0),
new GameLib.D3.API.Quaternion(0, 0, 0, 1)
];
};
GameLib.D3.API.Matrix4.prototype.lookAt = function (position, target, up) {
var pv = new GameLib.D3.API.Vector3(position.x, position.y, position.z);
var z = pv.subtract(target).normalize();
if (z.squared() === 0) {
z.z = 1;
}
var x = up.cross(z).normalize();
if (x.squared() === 0) {
z.x += 0.0001;
x = up.cross(z).normalize();
}
var y = z.cross(x);
this.rows[0].x = x.x;
this.rows[0].y = x.y;
this.rows[0].z = x.z;
this.rows[1].x = y.x;
this.rows[1].y = y.y;
this.rows[1].z = y.z;
this.rows[2].x = z.x;
this.rows[2].y = z.y;
this.rows[2].z = z.z;
return this;
};