1 /** 2 * The Render Engine 3 * CircleBodyComponent 4 * 5 * @fileoverview A physical circular 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.CircleBody", 37 "requires":[ 38 "R.components.physics.BaseBody", 39 "R.math.Point2D", 40 "R.math.Rectangle2D" 41 ] 42 }); 43 44 /** 45 * @class An extension of the {@link R.components.physics.BaseBody} which creates a circular 46 * rigid body. 47 * 48 * @param name {String} Name of the component 49 * @param radius {Number} The radius of the circle 50 * 51 * @extends R.components.physics.BaseBody 52 * @constructor 53 * @description A circular rigid body component. 54 */ 55 R.components.physics.CircleBody = function () { 56 return R.components.physics.BaseBody.extend(/** @scope R.components.physics.CircleBody.prototype */{ 57 58 radius:0, 59 60 /** 61 * @private 62 */ 63 constructor:function (name, radius) { 64 var fixDef = new Box2D.Dynamics.b2FixtureDef(); 65 fixDef.shape = new Box2D.Collision.Shapes.b2CircleShape(radius / R.physics.Simulation.WORLD_SIZE); 66 67 this.base(name, fixDef); 68 this.radius = radius / R.physics.Simulation.WORLD_SIZE; 69 this.setLocalOrigin(radius, radius); 70 }, 71 72 /** 73 * Releases the component back into the object pool. 74 */ 75 release:function () { 76 this.base(); 77 this.radius = 0; 78 }, 79 80 /** 81 * Set the radius of the circle's body. Calling this method after 82 * simulation has started on the body has no effect. 83 * 84 * @param radius {Number} The radius of the body 85 */ 86 setRadius:function (radius) { 87 this.radius = radius; 88 89 var scaled = radius / R.physics.Simulation.WORLD_SIZE; 90 this.getFixtureDef().shape.SetRadius(scaled); 91 if (this.simulation) { 92 this.updateFixture(); 93 } 94 }, 95 96 /** 97 * Get the radius of the circle's body. 98 * @return {Number} 99 */ 100 getRadius:function () { 101 return this.radius; 102 }, 103 104 /** 105 * Get a box which bounds the body. 106 * @return {R.math.Rectangle2D} 107 */ 108 getBoundingBox:function () { 109 var box = this.base(); 110 var r = this.radius; 111 box.set(0, 0, r * 2, r * 2); 112 return box; 113 } 114 115 /* pragma:DEBUG_START */ 116 /** 117 * Adds shape debugging 118 * @private 119 */, execute:function (renderContext, time, dt) { 120 this.base(renderContext, time, dt); 121 if (R.Engine.getDebugMode()) { 122 renderContext.pushTransform(); 123 renderContext.setLineStyle("blue"); 124 renderContext.drawArc(R.math.Point2D.ZERO, this.getRadius() * R.physics.Simulation.WORLD_SIZE, 0, 360); 125 renderContext.popTransform(); 126 } 127 } 128 /* pragma:DEBUG_END */ 129 130 }, { /** @scope R.components.physics.CircleBody.prototype */ 131 132 /** 133 * Get the class name of this object 134 * 135 * @return {String} "R.components.physics.CircleBody" 136 */ 137 getClassName:function () { 138 return "R.components.physics.CircleBody"; 139 } 140 141 }); 142 };