UNPKG

odin

Version:

Node.js Canvas/WebGL Javascript Game Framework

353 lines (282 loc) 9.13 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JSDoc: Source: math/aabb2.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/aabb2.js</h1> <section> <article> <pre class="prettyprint source"><code>if (typeof(define) !== "function") { var define = require("amdefine")(module); } define([ "odin/math/mathf", "odin/math/vec2" ], function(Mathf, Vec2) { "use strict"; /** * @class AABB2 * 2d axis aligned bounding box * @param Vec2 min * @param Vec2 max */ function AABB2(min, max) { /** * @property Vec2 min * @memberof Odin.AABB2 */ this.min = min || new Vec2(Infinity, Infinity); /** * @property Vec2 max * @memberof Odin.AABB2 */ this.max = max || new Vec2(-Infinity, -Infinity); } /** * @method clone * @memberof Odin.AABB2 * returns new copy of this * @return AABB2 */ AABB2.prototype.clone = function() { return new AABB2(this.min.clone(), this.max.clone()); }; /** * @method copy * @memberof Odin.AABB2 * copies other AABB * @param AABB2 other * @return this */ AABB2.prototype.copy = function(other) { this.min.copy(other.min); this.max.copy(other.max); return this; }; /** * @method set * @memberof Odin.AABB2 * set min and max vectors * @param Vec2 min * @param Vec2 max * @return this */ AABB2.prototype.set = function(min, max) { this.min.copy(min); this.max.copy(max); return this; }; /** * @method expandPoint * @memberof Odin.AABB2 * @param Vec2 v * @return this */ AABB2.prototype.expandPoint = function(v) { this.min.min(v); this.max.max(v); return this; }; /** * @method expandVec * @memberof Odin.AABB2 * @param Vec2 v * @return this */ AABB2.prototype.expandVec = function(v) { this.min.sub(v); this.max.add(v); return this; }; /** * @method expandScalar * @memberof Odin.AABB2 * @param Number s * @return this */ AABB2.prototype.expandScalar = function(s) { this.min.ssub(s); this.max.sadd(s); return this; }; /** * @method union * @memberof Odin.AABB2 * joins this and another aabb * @param AABB2 aabb * @return this */ AABB2.prototype.union = function(other) { this.min.min(other.min); this.max.max(other.max); return this; }; /** * @method clear * @memberof Odin.AABB2 * clears aabb * @return this */ AABB2.prototype.clear = function() { this.min.set(Infinity, Infinity); this.max.set(-Infinity, -Infinity); return this; }; /** * @method contains * @memberof Odin.AABB2 * checks if AABB contains point * @param Vec2 point * @return Boolean */ AABB2.prototype.contains = function(point) { var min = this.min, max = this.max, px = point.x, py = point.y; return !( px &lt; min.x || px > max.x || py &lt; min.y || py > max.y ); }; /** * @method intersects * @memberof Odin.AABB2 * checks if AABB intersects AABB * @param AABB2 other * @return Boolean */ AABB2.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 ); }; /** * @method fromPoints * @memberof Odin.AABB2 * set min and max from array of vectors * @param Array points * @return this */ AABB2.prototype.fromPoints = function(points) { var v, i = points.length, minx = Infinity, miny = Infinity, maxx = -Infinity, maxy = -Infinity, min = this.min, max = this.max, x, y; while (i--) { v = points[i]; x = v.x; y = v.y; minx = minx > x ? x : minx; miny = miny > y ? y : miny; maxx = maxx &lt; x ? x : maxx; maxy = maxy &lt; y ? y : maxy; } min.x = minx; min.y = miny; max.x = maxx; max.y = maxy; return this; }; /** * @method fromCenterSize * @memberof Odin.AABB2 * sets this from a center point and a size vector * @param Vec2 center * @param Vec2 size * @return this */ AABB2.prototype.fromCenterSize = function(center, size) { var min = this.min, max = this.max, x = center.x, y = center.y, hx = size.x * 0.5, hy = size.y * 0.5; min.x = x - hx; min.y = y - hy; max.x = x + hx; max.y = y + hy; return this; }; /** * @memberof Odin.AABB2 * @param Odin.AABB2 other * @return this */ AABB2.prototype.equals = function(other) { return !(!this.min.equals(other.min) || !this.max.equals(other.max)); }; /** * @method fromJSON * @memberof Odin.AABB2 * sets values from json object * @param Object json * @return this */ AABB2.prototype.fromJSON = function(json) { this.min.fromJSON(json.min); this.max.fromJSON(json.max); return this; }; /** * @method toJSON * @memberof Odin.AABB2 * returns json object * @return Object */ AABB2.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.AABB2 * converts AABB to string "AABB2( min: Vec2( -1, -1 ), max: Vec2( 1, 1 ) )" * @return String */ AABB2.prototype.toString = function() { var min = this.min, max = this.max; return "AABB2( min: " + min + ", max: " + max + " )"; }; return AABB2; } ); </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>