100 lines
2.1 KiB
JavaScript
100 lines
2.1 KiB
JavaScript
|
GameLib.D3.Vector4 = function(x, y, z, w) {
|
||
|
|
||
|
this.x = 0;
|
||
|
this.y = 0;
|
||
|
this.z = 0;
|
||
|
this.w = 0;
|
||
|
|
||
|
if (x) {
|
||
|
this.x = x;
|
||
|
}
|
||
|
|
||
|
if (y) {
|
||
|
this.y = y;
|
||
|
}
|
||
|
|
||
|
if (z) {
|
||
|
this.z = z;
|
||
|
}
|
||
|
|
||
|
if (w) {
|
||
|
this.w = w;
|
||
|
}
|
||
|
};
|
||
|
|
||
|
GameLib.D3.Vector4.prototype.translate = function (v) {
|
||
|
this.x += v.x;
|
||
|
this.y += v.y;
|
||
|
this.z += v.z;
|
||
|
return this;
|
||
|
};
|
||
|
|
||
|
GameLib.D3.Vector4.prototype.copy = function () {
|
||
|
return new GameLib.D3.Vector4(
|
||
|
this.x,
|
||
|
this.y,
|
||
|
this.z,
|
||
|
this.w
|
||
|
);
|
||
|
};
|
||
|
|
||
|
GameLib.D3.Vector4.prototype.multiply = function (s) {
|
||
|
if (s instanceof GameLib.D3.Vector3) {
|
||
|
this.x *= s.x;
|
||
|
this.y *= s.y;
|
||
|
this.z *= s.z;
|
||
|
} else if (s instanceof GameLib.D3.Matrix4) {
|
||
|
var x = s.rows[0].x * this.x + s.rows[0].y * this.y + s.rows[0].z * this.z + s.rows[0].w * this.w;
|
||
|
var y = s.rows[1].x * this.x + s.rows[1].y * this.y + s.rows[1].z * this.z + s.rows[1].w * this.w;
|
||
|
var z = s.rows[2].x * this.x + s.rows[2].y * this.y + s.rows[2].z * this.z + s.rows[2].w * this.w;
|
||
|
var w = s.rows[3].x * this.x + s.rows[3].y * this.y + s.rows[3].z * this.z + s.rows[3].w * this.w;
|
||
|
this.x = x;
|
||
|
this.y = y;
|
||
|
this.z = z;
|
||
|
this.w = w;
|
||
|
} else {
|
||
|
console.log("functionality not implemented - please do this");
|
||
|
throw new Error("not implemented");
|
||
|
}
|
||
|
};
|
||
|
|
||
|
|
||
|
GameLib.D3.Vector4.prototype.normalize = function () {
|
||
|
|
||
|
// note - leave w untouched
|
||
|
var EPSILON = 0.000001;
|
||
|
|
||
|
var v2 = this.x * this.x + this.y * this.y + this.z * this.z;
|
||
|
|
||
|
if (v2 < EPSILON) {
|
||
|
return this; //do nothing for zero vector
|
||
|
}
|
||
|
|
||
|
var invLength = 1 / Math.sqrt(v2);
|
||
|
|
||
|
this.x *= invLength;
|
||
|
this.y *= invLength;
|
||
|
this.z *= invLength;
|
||
|
|
||
|
return this;
|
||
|
};
|
||
|
|
||
|
GameLib.D3.Vector4.prototype.subtract = function (v) {
|
||
|
|
||
|
if (v instanceof GameLib.D3.Vector3) {
|
||
|
this.x -= v.x;
|
||
|
this.y -= v.y;
|
||
|
this.z -= v.z;
|
||
|
}
|
||
|
|
||
|
if (v instanceof GameLib.D3.Vector4) {
|
||
|
this.x -= v.x;
|
||
|
this.y -= v.y;
|
||
|
this.z -= v.z;
|
||
|
this.w -= v.w;
|
||
|
}
|
||
|
|
||
|
return this;
|
||
|
};
|
||
|
|