minecraft.js
Version:
Minecraft data serialization/deserialization and networking
111 lines (94 loc) • 2.39 kB
JavaScript
var Vector2 = require(__dirname + '/vector2.js');
/** @constructor */
var Vector3 = module.exports = function(x, y, z) {
this.x = x;
this.y = y;
this.z = z;
};
Vector3.prototype.toString = function() {
return "(" + this.x + ", " + this.y + ", " + this.z + ")";
};
Vector3.prototype.add = function(v2) {
if(typeof v2 == 'number') {
v2 = new Vector3(v2, v2, v2);
}
return new Vector3(
this.x + v2.x,
this.y + v2.y,
this.z + v2.z
);
};
Vector3.prototype.subtract = function(v2) {
if(typeof v2 == 'number') {
v2 = new Vector3(v2, v2, v2);
}
return new Vector3(
this.x - v2.x,
this.y - v2.y,
this.z - v2.z
);
};
Vector3.prototype.multiply = function(v2) {
if(typeof v2 == 'number') {
v2 = new Vector3(v2, v2, v2);
}
return new Vector3(
this.x * v2.x,
this.y * v2.y,
this.z * v2.z
);
};
Vector3.prototype.divide = function(v2) {
if(typeof v2 == 'number') {
v2 = new Vector3(v2, v2, v2);
}
return new Vector3(
this.x / v2.x,
this.y / v2.y,
this.z / v2.z
);
};
Vector3.prototype.floor = function() {
return new Vector3(
Math.floor(this.x),
Math.floor(this.y),
Math.floor(this.z)
);
};
Vector3.prototype.ceil = function() {
return new Vector3(
Math.ceil(this.x),
Math.ceil(this.y),
Math.ceil(this.z)
);
};
Vector3.prototype.round = function(v2) {
return new Vector3(
Math.round(this.x),
Math.round(this.y),
Math.round(this.z)
);
};
Vector3.prototype.angleTo = function(v2) {
var diff = v2.subtract(this);
var groundDistance = Math.sqrt(Math.pow(diff.x, 2) + Math.pow(diff.z, 2));
var yaw = 360 - ((Math.atan2(diff.x, diff.z) * (180 / Math.PI)) % 360),
pitch = (Math.atan2(groundDistance, diff.y) * (180 / Math.PI) - 90) % 360;
return new Vector2(yaw, pitch);
};
Vector3.prototype.distanceTo = function(v2) {
var diff = v2.subtract(this);
var groundDistance = Math.sqrt(Math.pow(diff.x, 2) + Math.pow(diff.z, 2));
return Math.sqrt(Math.pow(groundDistance, 2) + Math.pow(diff.y, 2));
};
Vector3.prototype.groundDistanceTo = function(v2) {
var diff = v2.subtract(this);
return Math.sqrt(Math.pow(diff.x, 2) + Math.pow(diff.z, 2));
};
Vector3.prototype.getChunk = function(v2) {
var floor = this.floor();
return new Vector2(
floor.x >> 4,
floor.z >> 4
);
};