Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add standard mathematical extensions to goog.

  • Loading branch information...
commit 097e1916340fd72899595611cc9c6917276f8296 1 parent 7ba398e
Jeff Verkoeyen authored
2  dev/core/namespace.breeze.js
View
@@ -16,6 +16,8 @@
goog.provide('Breeze');
+goog.require('goog.math.Matrix');
+
/**
* Welcome to Breeze.
*
1  dev/engine/class.director.js
View
@@ -19,7 +19,6 @@ goog.provide('Breeze.Engine.Director');
goog.require('Breeze.Engine');
goog.require('Breeze.Engine.Scene');
-goog.require('goog.style');
goog.require('goog.math.Vec2');
goog.require('goog.object');
85 dev/math/namespace.math.js
View
@@ -18,6 +18,8 @@ goog.provide('Breeze.Math');
goog.require('Breeze');
+goog.require('goog.math.Matrix');
+
Breeze.Math = {};
/**
@@ -28,3 +30,86 @@ Breeze.Math = {};
Breeze.Math.chopRemainder = function(x) {
return parseInt(x, 10);
};
+
+/**
+ * Create a 4x4 rotation matrix on the given axis.
+ * @param {!number} degreesCCW
+ * @param {!string} axis
+ * @return {goog.math.Matrix}
+ */
+goog.math.Matrix.createRotation = function(degreesCCW, axis) {
+ var ret = goog.math.Matrix.createIdentityMatrix(4);
+
+ var radiansCCW = -goog.math.toRadians(degreesCCW);
+
+ switch( axis )
+ {
+ case 'X':
+ case 'x':
+ ret.setValueAt(1, 1, Math.cos(radiansCCW));
+ ret.setValueAt(1, 2, Math.sin(radiansCCW));
+ ret.setValueAt(2, 1, -Math.sin(radiansCCW));
+ ret.setValueAt(2, 2, Math.cos(radiansCCW));
+ break;
+
+ case 'Y':
+ case 'y':
+ ret.setValueAt(0, 0, Math.cos(radiansCCW));
+ ret.setValueAt(0, 2, -Math.sin(radiansCCW));
+ ret.setValueAt(2, 0, Math.sin(radiansCCW));
+ ret.setValueAt(2, 2, Math.cos(radiansCCW));
+ break;
+
+ case 'Z':
+ case 'z':
+ ret.setValueAt(0, 0, Math.cos(radiansCCW));
+ ret.setValueAt(0, 1, Math.sin(radiansCCW));
+ ret.setValueAt(1, 0, -Math.sin(radiansCCW));
+ ret.setValueAt(1, 1, Math.cos(radiansCCW));
+ break;
+ }
+
+ return ret;
+};
+
+/**
+ * Clamp a coordinate within this box.
+ *
+ * @param {!goog.math.Vec2} coord The coordinates to clamp within this box.
+ * @return {!goog.math.Vec2} The clamped coordinates.
+ */
+goog.math.Box.prototype.clamped = function(coord) {
+ return new goog.math.Vec2(goog.math.clamp(coord.x, this.left, this.right),
+ goog.math.clamp(coord.y, this.top, this.bottom));
+};
+
+/**
+ * Multiply this vector by a scalar.
+ *
+ * @param {!number} b The scalar to multiply by.
+ * @return {!goog.math.Vec2} This vector with {@code b} multiplied.
+ */
+goog.math.Vec2.prototype.times = function(b) {
+ return new goog.math.Vec2(this.x * b, this.y * b);
+};
+
+/**
+ * Subract a vector from this and return the result.
+ *
+ * @param {!goog.math.Vec2} b The vector to subtract.
+ * @return {!goog.math.Vec2} This vector with {@code b} subtracted.
+ */
+goog.math.Vec2.prototype.minus = function(b) {
+ return new goog.math.Vec2(this.x - b.x, this.y - b.y);
+};
+
+/**
+ * Returns the cross-product of two vectors.
+ *
+ * @param {!goog.math.Vec2} a The first vector.
+ * @param {!goog.math.Vec2} b The second vector.
+ * @return {number} The cross-product of the two vectors.
+ */
+goog.math.Vec2.cross = function(a, b) {
+ return a.x * b.y - a.y * b.x;
+};
Please sign in to comment.
Something went wrong with that request. Please try again.