1 /** 2 * The Render Engine 3 * BoxBodyComponent 4 * 5 * @fileoverview A physical rectangular body component for use in a {@link Simulation}. 6 * 7 * @author: Brett Fattori (brettf@renderengine.com) 8 * 9 * @author: $Author: bfattori $ 10 * @version: $Revision: 1555 $ 11 * 12 * Copyright (c) 2011 Brett Fattori (brettf@renderengine.com) 13 * 14 * Permission is hereby granted, free of charge, to any person obtaining a copy 15 * of this software and associated documentation files (the "Software"), to deal 16 * in the Software without restriction, including without limitation the rights 17 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 18 * copies of the Software, and to permit persons to whom the Software is 19 * furnished to do so, subject to the following conditions: 20 * 21 * The above copyright notice and this permission notice shall be included in 22 * all copies or substantial portions of the Software. 23 * 24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 25 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 26 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 27 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 28 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 29 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 30 * THE SOFTWARE. 31 * 32 */ 33 34 // The class this file defines and its required classes 35 R.Engine.define({ 36 "class":"R.components.physics.BoxBody", 37 "requires":[ 38 "R.components.physics.BaseBody", 39 "R.math.Math2D" 40 ] 41 }); 42 43 /** 44 * @class An extension of the {@link R.components.BaseBody} which creates a rectangular 45 * rigid body. 46 * 47 * @param name {String} Name of the component 48 * @param extents {R.math.Vector2D} The full extents of the body along X and Y 49 * 50 * @extends R.components.physics.BaseBody 51 * @constructor 52 * @description A rectangular rigid body component. 53 */ 54 R.components.physics.BoxBody = function () { 55 return R.components.physics.BaseBody.extend(/** @scope R.components.physics.BoxBody.prototype */{ 56 57 extents:null, 58 59 /** 60 * @private 61 */ 62 constructor:function (name, extents) { 63 var fixDef = new Box2D.Dynamics.b2FixtureDef(); 64 fixDef.shape = new Box2D.Collision.Shapes.b2PolygonShape(); 65 66 this.base(name, fixDef); 67 this.extents = R.clone(extents); 68 this.extents.div(R.physics.Simulation.WORLD_SIZE); 69 this.setLocalOrigin(extents.x / 2, extents.y / 2); 70 }, 71 72 /** 73 * Destroy the object 74 */ 75 destroy:function () { 76 this.extents.destroy(); 77 this.base(); 78 }, 79 80 /** 81 * Return the object to the pool. 82 */ 83 release:function () { 84 this.extents = null; 85 this.base(); 86 }, 87 88 /** 89 * Deprecated in favor of {@link #setGameObject} 90 * @deprecated 91 */ 92 setHostObject:function (hostObj) { 93 this.setGameObject(hostObj); 94 }, 95 96 setGameObject:function (gameObject) { 97 this.base(gameObject); 98 99 this.getFixtureDef().shape.SetAsBox(this.extents.x / 2, this.extents.y / 2); // Half width and height 100 }, 101 102 /** 103 * Get a box which bounds the body, local to the body. 104 * @return {R.math.Rectangle2D} 105 */ 106 getBoundingBox:function () { 107 var box = this.base(); 108 var e = R.clone(this.getExtents()).mul(R.physics.Simulation.WORLD_SIZE); 109 box.set(0, 0, e.x, e.y); 110 e.destroy(); 111 return box; 112 }, 113 114 /** 115 * Set the extents of the box's body. Calling this method after the 116 * simulation of the body has started has no effect. 117 * 118 * @param extents {R.math.Point2D} The extents of the body along X and Y 119 */ 120 setExtents:function (extents) { 121 this.extents = R.clone(extents).div(R.physics.Simulation.WORLD_SIZE); 122 this.getFixtureDef().SetAsBox(this.extents.x / 2, this.extents.y / 2); 123 if (this.simulation) { 124 this.updateFixture(); 125 } 126 }, 127 128 /** 129 * Get the extents of the box's body. 130 * @return {R.math.Point2D} 131 */ 132 getExtents:function () { 133 return this.extents; 134 } 135 136 /* pragma:DEBUG_START */ 137 /** 138 * Adds shape debugging 139 * @private 140 */, execute:function (renderContext, time, dt) { 141 this.base(renderContext, time, dt); 142 if (R.Engine.getDebugMode()) { 143 renderContext.pushTransform(); 144 renderContext.setLineStyle("blue"); 145 var ext = R.clone(this.extents).mul(R.physics.Simulation.WORLD_SIZE); 146 var hx = ext.x / 2; 147 var hy = ext.y / 2; 148 var rect = R.math.Rectangle2D.create(-hx, -hy, hx * 2, hy * 2); 149 renderContext.drawRectangle(rect); 150 rect.destroy(); 151 ext.destroy(); 152 renderContext.popTransform(); 153 } 154 } 155 /* pragma:DEBUG_END */ 156 157 158 }, { /** @scope R.components.physics.BoxBody.prototype */ 159 160 /** 161 * Get the class name of this object 162 * 163 * @return {String} "R.components.physics.BoxBody" 164 */ 165 getClassName:function () { 166 return "R.components.physics.BoxBody"; 167 } 168 169 }); 170 };