2012-11-09 17:52:12 +08:00
|
|
|
/* Copyright (c) 2012 Scott Lembcke and Howling Moon Software
|
|
|
|
* Copyright (c) 2012 cocos2d-x.org
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
|
|
|
#include "CCPhysicsDebugNode.h"
|
|
|
|
|
|
|
|
#if CC_ENABLE_CHIPMUNK_INTEGRATION
|
|
|
|
|
|
|
|
#include "ccTypes.h"
|
|
|
|
#include "cocoa/CCGeometry.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2012-11-20 16:17:20 +08:00
|
|
|
NS_CC_EXT_BEGIN
|
2012-11-09 17:52:12 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
IMPORTANT - READ ME!
|
|
|
|
|
|
|
|
This file sets pokes around in the private API a lot to provide efficient
|
|
|
|
debug rendering given nothing more than reference to a Chipmunk space.
|
|
|
|
It is not recommended to write rendering code like this in your own games
|
|
|
|
as the private API may change with little or no warning.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static ccColor4F ColorForBody(cpBody *body)
|
|
|
|
{
|
|
|
|
if (cpBodyIsRogue(body) || cpBodyIsSleeping(body))
|
|
|
|
{
|
2012-11-20 16:17:20 +08:00
|
|
|
return ccc4f(0.5f, 0.5f, 0.5f ,0.5f);
|
2012-11-09 17:52:12 +08:00
|
|
|
}
|
|
|
|
else if (body->CP_PRIVATE(node).idleTime > body->CP_PRIVATE(space)->sleepTimeThreshold)
|
|
|
|
{
|
2012-11-20 16:17:20 +08:00
|
|
|
return ccc4f(0.33f, 0.33f, 0.33f, 0.5f);
|
2012-11-09 17:52:12 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-20 16:17:20 +08:00
|
|
|
return ccc4f(1.0f, 0.0f, 0.0f, 0.5f);
|
2012-11-09 17:52:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
static Point cpVert2ccp(const cpVect &vert)
|
2012-11-09 17:52:12 +08:00
|
|
|
{
|
|
|
|
return CCPointMake(vert.x, vert.y);
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
static Point* cpVertArray2ccpArrayN(const cpVect* cpVertArray, unsigned int count)
|
2012-11-09 17:52:12 +08:00
|
|
|
{
|
2012-11-20 16:17:20 +08:00
|
|
|
if (count == 0) return NULL;
|
2013-06-20 14:15:53 +08:00
|
|
|
Point* pPoints = new Point[count];
|
2012-11-20 16:17:20 +08:00
|
|
|
|
|
|
|
for (unsigned int i = 0; i < count; ++i)
|
|
|
|
{
|
|
|
|
pPoints[i].x = cpVertArray[i].x;
|
|
|
|
pPoints[i].y = cpVertArray[i].y;
|
|
|
|
}
|
|
|
|
return pPoints;
|
2012-11-09 17:52:12 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
static void DrawShape(cpShape *shape, DrawNode *renderer)
|
2012-11-09 17:52:12 +08:00
|
|
|
{
|
|
|
|
cpBody *body = shape->body;
|
|
|
|
ccColor4F color = ColorForBody(body);
|
|
|
|
|
|
|
|
switch (shape->CP_PRIVATE(klass)->type)
|
|
|
|
{
|
|
|
|
case CP_CIRCLE_SHAPE:
|
|
|
|
{
|
|
|
|
cpCircleShape *circle = (cpCircleShape *)shape;
|
|
|
|
cpVect center = circle->tc;
|
|
|
|
cpFloat radius = circle->r;
|
|
|
|
renderer->drawDot(cpVert2ccp(center), cpfmax(radius, 1.0), color);
|
|
|
|
renderer->drawSegment(cpVert2ccp(center), cpVert2ccp(cpvadd(center, cpvmult(body->rot, radius))), 1.0, color);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CP_SEGMENT_SHAPE:
|
|
|
|
{
|
|
|
|
cpSegmentShape *seg = (cpSegmentShape *)shape;
|
|
|
|
renderer->drawSegment(cpVert2ccp(seg->ta), cpVert2ccp(seg->tb), cpfmax(seg->r, 2.0), color);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CP_POLY_SHAPE:
|
|
|
|
{
|
|
|
|
cpPolyShape *poly = (cpPolyShape *)shape;
|
|
|
|
ccColor4F line = color;
|
|
|
|
line.a = cpflerp(color.a, 1.0, 0.5);
|
2013-06-20 14:15:53 +08:00
|
|
|
Point* pPoints = cpVertArray2ccpArrayN(poly->tVerts, poly->numVerts);
|
2012-11-20 16:17:20 +08:00
|
|
|
renderer->drawPolygon(pPoints, poly->numVerts, color, 1.0, line);
|
|
|
|
CC_SAFE_DELETE_ARRAY(pPoints);
|
2012-11-09 17:52:12 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2012-11-20 16:34:55 +08:00
|
|
|
cpAssertHard(false, "Bad assertion in DrawShape()");
|
2012-11-09 17:52:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static ccColor4F CONSTRAINT_COLOR = {0, 1, 0, 0.5};
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
static void DrawConstraint(cpConstraint *constraint, DrawNode *renderer)
|
2012-11-09 17:52:12 +08:00
|
|
|
{
|
|
|
|
cpBody *body_a = constraint->a;
|
|
|
|
cpBody *body_b = constraint->b;
|
|
|
|
|
|
|
|
const cpConstraintClass *klass = constraint->CP_PRIVATE(klass);
|
|
|
|
if (klass == cpPinJointGetClass())
|
|
|
|
{
|
|
|
|
cpPinJoint *joint = (cpPinJoint *)constraint;
|
|
|
|
|
|
|
|
cpVect a = cpBodyLocal2World(body_a, joint->anchr1);
|
|
|
|
cpVect b = cpBodyLocal2World(body_b, joint->anchr2);
|
|
|
|
|
|
|
|
renderer->drawDot(cpVert2ccp(a), 3.0, CONSTRAINT_COLOR);
|
|
|
|
renderer->drawDot(cpVert2ccp(b), 3.0, CONSTRAINT_COLOR);
|
|
|
|
renderer->drawSegment(cpVert2ccp(a), cpVert2ccp(b), 1.0, CONSTRAINT_COLOR);
|
|
|
|
}
|
|
|
|
else if (klass == cpSlideJointGetClass())
|
|
|
|
{
|
|
|
|
cpSlideJoint *joint = (cpSlideJoint *)constraint;
|
|
|
|
|
|
|
|
cpVect a = cpBodyLocal2World(body_a, joint->anchr1);
|
|
|
|
cpVect b = cpBodyLocal2World(body_b, joint->anchr2);
|
|
|
|
|
|
|
|
renderer->drawDot(cpVert2ccp(a), 3.0, CONSTRAINT_COLOR);
|
|
|
|
renderer->drawDot(cpVert2ccp(b), 3.0, CONSTRAINT_COLOR);
|
|
|
|
renderer->drawSegment(cpVert2ccp(a), cpVert2ccp(b), 1.0, CONSTRAINT_COLOR);
|
|
|
|
}
|
|
|
|
else if (klass == cpPivotJointGetClass())
|
|
|
|
{
|
|
|
|
cpPivotJoint *joint = (cpPivotJoint *)constraint;
|
|
|
|
|
|
|
|
cpVect a = cpBodyLocal2World(body_a, joint->anchr1);
|
|
|
|
cpVect b = cpBodyLocal2World(body_b, joint->anchr2);
|
|
|
|
|
|
|
|
renderer->drawDot(cpVert2ccp(a), 3.0, CONSTRAINT_COLOR);
|
|
|
|
renderer->drawDot(cpVert2ccp(b), 3.0, CONSTRAINT_COLOR);
|
|
|
|
}
|
|
|
|
else if (klass == cpGrooveJointGetClass())
|
|
|
|
{
|
|
|
|
cpGrooveJoint *joint = (cpGrooveJoint *)constraint;
|
|
|
|
|
|
|
|
cpVect a = cpBodyLocal2World(body_a, joint->grv_a);
|
|
|
|
cpVect b = cpBodyLocal2World(body_a, joint->grv_b);
|
|
|
|
cpVect c = cpBodyLocal2World(body_b, joint->anchr2);
|
|
|
|
|
|
|
|
renderer->drawDot(cpVert2ccp(c), 3.0, CONSTRAINT_COLOR);
|
|
|
|
renderer->drawSegment(cpVert2ccp(a), cpVert2ccp(b), 1.0, CONSTRAINT_COLOR);
|
|
|
|
}
|
|
|
|
else if (klass == cpDampedSpringGetClass())
|
|
|
|
{
|
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// printf("Cannot draw constraint\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
// implementation of PhysicsDebugNode
|
2012-11-09 17:52:12 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void PhysicsDebugNode::draw()
|
2012-11-09 17:52:12 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (! _spacePtr)
|
2012-11-09 17:52:12 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
cpSpaceEachShape(_spacePtr, (cpSpaceShapeIteratorFunc)DrawShape, this);
|
|
|
|
cpSpaceEachConstraint(_spacePtr, (cpSpaceConstraintIteratorFunc)DrawConstraint, this);
|
2012-11-09 17:52:12 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
DrawNode::draw();
|
|
|
|
DrawNode::clear();
|
2012-11-09 17:52:12 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
PhysicsDebugNode::PhysicsDebugNode()
|
2013-06-15 14:03:30 +08:00
|
|
|
: _spacePtr(NULL)
|
2012-11-09 17:52:12 +08:00
|
|
|
{}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
PhysicsDebugNode* PhysicsDebugNode::create(cpSpace *space)
|
2012-11-09 17:52:12 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
PhysicsDebugNode *node = new PhysicsDebugNode();
|
2012-11-09 17:52:12 +08:00
|
|
|
if (node)
|
|
|
|
{
|
|
|
|
node->init();
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
node->_spacePtr = space;
|
2012-11-09 17:52:12 +08:00
|
|
|
|
|
|
|
node->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
PhysicsDebugNode::~PhysicsDebugNode()
|
2012-11-09 17:52:12 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
cpSpace* PhysicsDebugNode::getSpace() const
|
2012-11-22 15:19:16 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _spacePtr;
|
2012-11-22 15:19:16 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void PhysicsDebugNode::setSpace(cpSpace *space)
|
2012-11-22 15:19:16 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_spacePtr = space;
|
2012-11-22 15:19:16 +08:00
|
|
|
}
|
|
|
|
|
2012-11-20 16:17:20 +08:00
|
|
|
NS_CC_EXT_END
|
2012-11-09 17:52:12 +08:00
|
|
|
|
|
|
|
#endif // CC_ENABLE_CHIPMUNK_INTEGRATION
|