UNPKG

odin

Version:

Node.js Canvas/WebGL Javascript Game Framework

367 lines (296 loc) 9.66 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JSDoc: Source: math/aabb3.js</title> <script src="scripts/prettify/prettify.js"> </script> <script src="scripts/prettify/lang-css.js"> </script> <!--[if lt IE 9]> <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css"> <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css"> </head> <body> <div id="main"> <h1 class="page-title">Source: math/aabb3.js</h1> <section> <article> <pre class="prettyprint source"><code>if (typeof(define) !== "function") { var define = require("amdefine")(module); } define([ "odin/math/mathf", "odin/math/vec3" ], function(Mathf, Vec3) { "use strict"; /** * @class AABB3 * 2d axis aligned bounding box * @param Vec3 min * @param Vec3 max */ function AABB3(min, max) { /** * @property Vec3 min * @memberof Odin.AABB3 */ this.min = min || new Vec3(Infinity, Infinity, Infinity); /** * @property Vec3 max * @memberof Odin.AABB3 */ this.max = max || new Vec3(-Infinity, -Infinity, -Infinity); } /** * @method clone * @memberof Odin.AABB3 * returns new copy of this * @return AABB3 */ AABB3.prototype.clone = function() { return new AABB3(this.min.clone(), this.max.clone()); }; /** * @method copy * @memberof Odin.AABB3 * copies other AABB * @param AABB3 other * @return this */ AABB3.prototype.copy = function(other) { this.min.copy(other.min); this.max.copy(other.max); return this; }; /** * @method set * @memberof Odin.AABB3 * set min and max vectors * @param Vec3 min * @param Vec3 max * @return this */ AABB3.prototype.set = function(min, max) { this.min.copy(min); this.max.copy(max); return this; }; /** * @method expandPoint * @memberof Odin.AABB3 * @param Vec3 v * @return this */ AABB3.prototype.expandPoint = function(v) { this.min.min(v); this.max.max(v); return this; }; /** * @method expandVec * @memberof Odin.AABB3 * @param Vec3 v * @return this */ AABB3.prototype.expandVec = function(v) { this.min.sub(v); this.max.add(v); return this; }; /** * @method expandScalar * @memberof Odin.AABB3 * @param Number s * @return this */ AABB3.prototype.expandScalar = function(s) { this.min.ssub(s); this.max.sadd(s); return this; }; /** * @method union * @memberof Odin.AABB3 * joins this and another aabb * @param AABB3 aabb * @return this */ AABB3.prototype.union = function(other) { this.min.min(other.min); this.max.max(other.max); return this; }; /** * @method clear * @memberof Odin.AABB3 * clears aabb * @return this */ AABB3.prototype.clear = function() { this.min.set(Infinity, Infinity, Infinity); this.max.set(-Infinity, -Infinity, -Infinity); return this; }; /** * @method contains * @memberof Odin.AABB3 * checks if AABB contains point * @param Vec3 point * @return Boolean */ AABB3.prototype.contains = function(point) { var min = this.min, max = this.max, px = point.x, py = point.y, pz = point.z; return !( px &lt; min.x || px > max.x || py &lt; min.y || py > max.y || pz &lt; min.z || pz > max.z ); }; /** * @method intersects * @memberof Odin.AABB3 * checks if AABB intersects AABB * @param AABB3 other * @return Boolean */ AABB3.prototype.intersects = function(other) { var aMin = this.min, aMax = this.max, bMin = other.min, bMax = other.max; return !( bMax.x &lt; aMin.x || bMin.x > aMax.x || bMax.y &lt; aMin.y || bMin.y > aMax.y || bMax.z &lt; aMin.z || bMin.z > aMax.z ); }; /** * @method fromPoints * @memberof Odin.AABB3 * set min and max from array of vectors * @param Array points * @return this */ AABB3.prototype.fromPoints = function(points) { var v, i = points.length, minx = Infinity, miny = Infinity, minz = Infinity, maxx = -Infinity, maxy = -Infinity, maxz = -Infinity, min = this.min, max = this.max, x, y, z; while (i--) { v = points[i]; x = v.x; y = v.y; z = v.z; minx = minx > x ? x : minx; miny = miny > y ? y : miny; minz = minz > z ? z : minz; maxx = maxx &lt; x ? x : maxx; maxy = maxy &lt; y ? y : maxy; maxz = maxz &lt; z ? z : maxz; } min.x = minx; min.y = miny; min.z = minz; max.x = maxx; max.y = maxy; max.z = maxz; return this; }; /** * @method fromCenterSize * @memberof Odin.AABB3 * sets this from a center point and a size vector * @param Vec3 center * @param Vec3 size * @return this */ AABB3.prototype.fromCenterSize = function(center, size) { var min = this.min, max = this.max, x = center.x, y = center.y, z = center.z, hx = size.x * 0.5, hy = size.y * 0.5, hz = size.z * 0.5; min.x = x - hx; min.y = y - hy; min.z = z - hz; max.x = x + hx; max.y = y + hy; max.z = z + hz; return this; }; /** * @memberof Odin.AABB3 * @param Odin.AABB3 other * @return this */ AABB3.prototype.equals = function(other) { return !(!this.min.equals(other.min) || !this.max.equals(other.max)); }; /** * @method fromJSON * @memberof Odin.AABB3 * sets values from json object * @param Object json * @return this */ AABB3.prototype.fromJSON = function(json) { this.min.fromJSON(json.min); this.max.fromJSON(json.max); return this; }; /** * @method toJSON * @memberof Odin.AABB3 * returns json object * @return Object */ AABB3.prototype.toJSON = function(json) { json || (json = {}); json.min = this.min.toJSON(json.min); json.max = this.max.toJSON(json.max); return json; }; /** * @method toString * @memberof Odin.AABB3 * converts AABB to string "AABB3( min: Vec3( -1, -1 ), max: Vec3( 1, 1 ) )" * @return String */ AABB3.prototype.toString = function() { var min = this.min, max = this.max; return "AABB3( min: " + min + ", max: " + max + " )"; }; return AABB3; } ); </code></pre> </article> </section> </div> <nav> <h2><a href="index.html">Index</a></h2><h3>Classes</h3><ul><li><a href="Canvas.html">Canvas</a></li><li><a href="GUIObject.html">GUIObject</a></li><li><a href="MeshFilter.html">MeshFilter</a></li><li><a href="Odin.html">Odin</a></li><li><a href="Odin.Class.html">Class</a></li><li><a href="Odin.EventEmitter.html">EventEmitter</a></li><li><a href="Odin.GameObject.html">GameObject</a></li><li><a href="Odin.Scene.html">Scene</a></li><li><a href="P2Contact.html">P2Contact</a></li><li><a href="P2Equation.html">P2Equation</a></li><li><a href="P2Friction.html">P2Friction</a></li><li><a href="P2Solver.html">P2Solver</a></li><li><a href="ParticleSystem.html">ParticleSystem</a></li><li><a href="Renderer.html">Renderer</a></li><li><a href="SpriteAnimation.html">SpriteAnimation</a></li><li><a href="WebGLRenderer.html">WebGLRenderer</a></li><li><a href="WebGLRenderer2D.html">WebGLRenderer2D</a></li></ul> </nav> <br clear="both"> <footer> Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.2.2</a> on Mon Feb 24 2014 16:15:46 GMT-0600 (CST) </footer> <script> prettyPrint(); </script> <script src="scripts/linenumber.js"> </script> </body> </html>