1 /****************************************************************************
  2  Copyright (c) 2010-2012 cocos2d-x.org
  3  Copyright (c) 2008-2010 Ricardo Quesada
  4  Copyright (c) 2011      Zynga Inc.
  5  Copyright (c) 2012 Scott Lembcke and Howling Moon Software
  6 
  7  http://www.cocos2d-x.org
  8 
  9  Permission is hereby granted, free of charge, to any person obtaining a copy
 10  of this software and associated documentation files (the "Software"), to deal
 11  in the Software without restriction, including without limitation the rights
 12  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 13  copies of the Software, and to permit persons to whom the Software is
 14  furnished to do so, subject to the following conditions:
 15 
 16  The above copyright notice and this permission notice shall be included in
 17  all copies or substantial portions of the Software.
 18 
 19  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 20  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 21  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 22  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 23  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 24  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 25  THE SOFTWARE.
 26  ****************************************************************************/
 27 
 28 /*
 29  IMPORTANT - READ ME!
 30 
 31  This file sets pokes around in the private API a lot to provide efficient
 32  debug rendering given nothing more than reference to a Chipmunk space.
 33  It is not recommended to write rendering code like this in your own games
 34  as the private API may change with little or no warning.
 35  */
 36 
 37 // Helper. Converts an array of numbers into an array of vectors(x,y)
 38 cc.__convertVerts = function (verts) {
 39     var ret = [];
 40     for (var i = 0; i < verts.length / 2; i++) {
 41         ret[i] = {x:verts[i * 2], y:verts[i * 2 + 1]};
 42     }
 43     return ret;
 44 };
 45 
 46 cc.ColorForBody = function (body) {
 47     if (body.isRogue() || body.isSleeping()) {
 48         return cc.c4f(0.5, 0.5, 0.5, 0.5);
 49     } else if (body.nodeIdleTime > body.space.sleepTimeThreshold) {
 50         return cc.c4f(0.33, 0.33, 0.33, 0.5);
 51     } else {
 52         return cc.c4f(1, 0, 0, 0.5);
 53     }
 54 };
 55 
 56 cc.DrawShape = function (shape, renderer) {
 57     var body = shape.body;
 58     var color = cc.ColorForBody(body);
 59     switch (shape.collisionCode) {
 60         case cp.CircleShape.prototype.collisionCode:
 61             this.drawDot(shape.tc, Math.max(shape.r, 1.0), color);
 62             this.drawSegment(shape.tc, cp.v.add(shape.tc, cp.v.mult(body.rot, shape.r)), 1.0, color);
 63             break;
 64         case cp.SegmentShape.prototype.collisionCode:
 65             this.drawSegment(shape.ta, shape.tb, Math.max(shape.r, 2.0), color);
 66             break;
 67         case cp.PolyShape.prototype.collisionCode:
 68             var line = cc.c4f(color.r, color.g, color.b, cc.lerp(color.a, 1.0, 0.5));
 69             this.drawPoly(cc.__convertVerts(shape.tVerts), color, 1.0, line);
 70             break;
 71         default:
 72             cc.Assert(false, "Bad assertion in DrawShape()");
 73     }
 74 };
 75 
 76 cc.DrawConstraint = function (constraint, renderer) {
 77     var body_a = constraint.a;
 78     var body_b = constraint.b;
 79     var a, b;
 80 
 81     if (constraint instanceof cp.PinJoint) {
 82         a = body_a.local2World(constraint.anchr1);
 83         b = body_b.local2World(constraint.anchr2);
 84         this.drawDot(a, 3.0, cc.CONSTRAINT_COLOR);
 85         this.drawDot(b, 3.0, cc.CONSTRAINT_COLOR);
 86         this.drawSegment(a, b, 1.0, cc.CONSTRAINT_COLOR);
 87     } else if (constraint instanceof cp.SlideJoint) {
 88         a = body_a.local2World(constraint.anchr1);
 89         b = body_b.local2World(constraint.anchr2);
 90 
 91         this.drawDot(a, 3.0, cc.CONSTRAINT_COLOR);
 92         this.drawDot(b, 3.0, cc.CONSTRAINT_COLOR);
 93         this.drawSegment(a, b, 1.0, cc.CONSTRAINT_COLOR);
 94     } else if (constraint instanceof cp.PivotJoint) {
 95         a = body_a.local2World(constraint.anchr1);
 96         b = body_b.local2World(constraint.anchr2);
 97         this.drawDot(a, 3.0, cc.CONSTRAINT_COLOR);
 98         this.drawDot(b, 3.0, cc.CONSTRAINT_COLOR);
 99     } else if (constraint instanceof cp.GrooveJoint) {
100         a = body_a.local2World(constraint.grv_a);
101         b = body_a.local2World(constraint.grv_b);
102         var c = body_b.local2World(constraint.anchr2);
103 
104         this.drawDot(c, 3.0, cc.CONSTRAINT_COLOR);
105         this.drawSegment(a, b, 1.0, cc.CONSTRAINT_COLOR);
106     } else if (constraint instanceof cp.DampedSpring) {
107         // TODO
108     } else {
109         //printf("Cannot draw constraint\n");
110     }
111 };
112 
113 cc.CONSTRAINT_COLOR = cc.c4f(0, 1, 0, 0.5);
114 
115 /**
116  A Node that draws the components of a physics engine.
117  Supported physics engines:
118  - Chipmunk
119  - Objective-Chipmunk
120  */
121 cc.PhysicsDebugNode = cc.DrawNode.extend({
122     _spaceObj:null,
123     _spacePtr:null,
124 
125     getSpace:function () {
126         return this._spacePtr;
127     },
128 
129     setSpace:function (space) {
130         this._spacePtr = space;
131     },
132 
133     draw:function (context) {
134         if (!this._spacePtr)
135             return;
136 
137         this._spacePtr.eachShape(cc.DrawShape.bind(this));
138         this._spacePtr.eachConstraint(cc.DrawConstraint.bind(this));
139         this._super();
140         this.clear();
141     }
142 });
143 
144 /** Create a debug node for an Objective-Chipmunk space. */
145 cc.PhysicsDebugNode.debugNodeForChipmunkSpace = function (space) {
146     var node = new cc.PhysicsDebugNode();
147     if (node.init()) {
148         node._spaceObj = space;
149         node._spacePtr = space.space;
150         return node;
151     }
152     return null;
153 };
154 
155 /** Create a debug node for a regular Chipmunk space. */
156 cc.PhysicsDebugNode.debugNodeForCPSpace = function (space) {
157     var node = new cc.PhysicsDebugNode();
158     if (node.init()) {
159         node._spacePtr = space;
160         return node;
161     }
162     return null;
163 };
164 
165 cc.PhysicsDebugNode.create = cc.PhysicsDebugNode.debugNodeForCPSpace;
166 
167