1 /**
  2  * The Render Engine
  3  * PolyBodyComponent
  4  *
  5  * @fileoverview A physical polygonal body component for use in a {@link R.physics.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.PolyBody",
 37     "requires":[
 38         "R.components.physics.BaseBody",
 39         "R.math.Math2D"
 40     ]
 41 });
 42 
 43 /**
 44  * @class An extension of {@link R.components.BaseBody} which creates a polygonal
 45  *        rigid body.  The polygon must be a convex polygon.
 46  *
 47  * @param name {String} Name of the component
 48  * @param points {Array} An array of points defining the body shape
 49  *
 50  * @extends R.components.physics.BaseBody
 51  * @constructor
 52  * @description A polygonal rigid body component.
 53  */
 54 R.components.physics.PolyBody = function () {
 55     return R.components.physics.BaseBody.extend(/** @scope R.components.physics.PolyBody.prototype */{
 56 
 57         points:null,
 58         center:null,
 59         extents:null,
 60 
 61         /**
 62          * @private
 63          */
 64         constructor:function (name, points) {
 65             var fixDef = new Box2D.Dynamics.b2FixtureDef();
 66             fixDef.shape = new Box2D.Collision.Shapes.b2PolygonShape();
 67 
 68             this.base(name, fixDef);
 69             this.points = points;
 70 
 71             // Calculate the extents of the points
 72             this.extents = R.math.Math2D.getBoundingBox(points);
 73 
 74             // Calculate the center of the points
 75             this.center = R.math.Math2D.getCenterOfPoints(points);
 76             this.setLocalOrigin(this.center);
 77         },
 78 
 79         /**
 80          * Destroy the object
 81          */
 82         destroy:function () {
 83             this.center.destroy();
 84             this.extents.destroy();
 85             this.base();
 86         },
 87 
 88         /**
 89          * Return the object to the pool.
 90          */
 91         release:function () {
 92             this.points = null;
 93             this.center = null;
 94             this.extents = null;
 95             this.base();
 96         },
 97 
 98         /**
 99          * Deprecated in favor of {@link #setGameObject}
100          * @deprecated
101          */
102         setHostObject:function (hostObj) {
103             this.setGameObject(hostObj);
104         },
105 
106         setGameObject:function (gameObject) {
107             this.base(gameObject);
108 
109             var scaled = [], pt = R.math.Point2D.create(0, 0);
110             for (var p = 0; p < this.points.length; p++) {
111                 pt.set(this.points[p]);
112                 pt.div(R.physics.Simulation.WORLD_SIZE);
113                 scaled.push(new Box2D.Common.Math.b2Vec2(pt.x, pt.y));
114             }
115             this.getFixtureDef().shape.SetAsArray(scaled);
116             pt.destroy();
117         },
118 
119         /**
120          * Get a box which bounds the body, local to the body.
121          * @return {R.math.Rectangle2D}
122          */
123         getBoundingBox:function () {
124             return this.extents;
125         },
126 
127         /**
128          * Get the extents of the box's body.
129          * @return {R.math.Point2D}
130          */
131         getExtents:function () {
132             return this.extents;
133         }
134 
135         /* pragma:DEBUG_START */
136         /**
137          * Adds shape debugging
138          * @private
139          */, execute:function (renderContext, time, dt) {
140             this.base(renderContext, time, dt);
141             if (R.Engine.getDebugMode()) {
142                 renderContext.pushTransform();
143                 renderContext.setLineStyle("blue");
144                 renderContext.setScale(1 / this.getScale());
145                 renderContext.drawPolygon(this.points);
146                 renderContext.popTransform();
147             }
148         }
149         /* pragma:DEBUG_END */
150 
151 
152     }, { /** @scope R.components.physics.BoxBody.prototype */
153 
154         /**
155          * Get the class name of this object
156          *
157          * @return {String} "R.components.physics.BoxBody"
158          */
159         getClassName:function () {
160             return "R.components.physics.BoxBody";
161         }
162 
163     });
164 };