2012-11-09 17:52:12 +08:00
|
|
|
/* Copyright (c) 2012 Scott Lembcke and Howling Moon Software
|
|
|
|
* Copyright (c) 2012 cocos2d-x.org
|
2018-01-29 16:25:32 +08:00
|
|
|
* Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-06-24 15:54:02 +08:00
|
|
|
* Copyright (c) 2021 @aismann; Peter Eismann, Germany; dreifrankensoft
|
2012-11-09 17:52:12 +08:00
|
|
|
*
|
|
|
|
* 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"
|
|
|
|
|
2016-03-01 05:53:00 +08:00
|
|
|
#include "chipmunk/chipmunk_private.h"
|
2020-12-02 00:58:34 +08:00
|
|
|
|
2012-11-09 17:52:12 +08:00
|
|
|
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/ccTypes.h"
|
2021-10-23 23:27:14 +08:00
|
|
|
#include "math/CCMath.h"
|
2012-11-09 17:52:12 +08:00
|
|
|
|
2014-01-26 19:35:21 +08:00
|
|
|
|
2012-11-09 17:52:12 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2021-06-18 15:47:48 +08:00
|
|
|
|
2012-11-20 16:17:20 +08:00
|
|
|
NS_CC_EXT_BEGIN
|
2012-11-09 17:52:12 +08:00
|
|
|
|
2021-06-18 15:47:48 +08:00
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2021-06-24 15:54:02 +08:00
|
|
|
static const cpVect spring_verts[] = {
|
|
|
|
{0.00f, 0.0f},
|
|
|
|
{0.20f, 0.0f},
|
|
|
|
{0.25f, 3.0f},
|
|
|
|
{0.30f, -6.0f},
|
|
|
|
{0.35f, 6.0f},
|
|
|
|
{0.40f, -6.0f},
|
|
|
|
{0.45f, 6.0f},
|
|
|
|
{0.50f, -6.0f},
|
|
|
|
{0.55f, 6.0f},
|
|
|
|
{0.60f, -6.0f},
|
|
|
|
{0.65f, 6.0f},
|
|
|
|
{0.70f, -3.0f},
|
|
|
|
{0.75f, 6.0f},
|
|
|
|
{0.80f, 0.0f},
|
|
|
|
{1.00f, 0.0f},
|
|
|
|
};
|
|
|
|
static const int spring_count = sizeof(spring_verts) / sizeof(cpVect);
|
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
static Color4F ColorForBody(cpBody *body)
|
2012-11-09 17:52:12 +08:00
|
|
|
{
|
2016-08-19 16:28:47 +08:00
|
|
|
if (CP_BODY_TYPE_STATIC == cpBodyGetType(body) || cpBodyIsSleeping(body))
|
2012-11-09 17:52:12 +08:00
|
|
|
{
|
2016-08-19 16:28:47 +08:00
|
|
|
return Color4F(0.5f, 0.5f, 0.5f ,0.5f);
|
|
|
|
}
|
2016-03-01 05:53:00 +08:00
|
|
|
else if (body->sleeping.idleTime > cpBodyGetSpace(body)->sleepTimeThreshold)
|
2012-11-09 17:52:12 +08:00
|
|
|
{
|
2016-08-19 16:28:47 +08:00
|
|
|
return Color4F(0.33f, 0.33f, 0.33f, 0.5f);
|
|
|
|
}
|
2012-11-09 17:52:12 +08:00
|
|
|
else
|
|
|
|
{
|
2016-08-19 16:28:47 +08:00
|
|
|
return Color4F(1.0f, 0.0f, 0.0f, 0.5f);
|
|
|
|
}
|
2012-11-09 17:52:12 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
static Vec2 cpVert2Point(const cpVect &vert)
|
2012-11-09 17:52:12 +08:00
|
|
|
{
|
2021-08-05 17:23:55 +08:00
|
|
|
return (Vec2(vert.x, vert.y) );
|
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
|
|
|
{
|
2016-08-19 16:28:47 +08:00
|
|
|
cpBody *body = cpShapeGetBody(shape);
|
|
|
|
Color4F color = ColorForBody(body);
|
2012-11-09 17:52:12 +08:00
|
|
|
|
2020-07-29 03:29:08 +08:00
|
|
|
switch (shape->klass->type)
|
2012-11-09 17:52:12 +08:00
|
|
|
{
|
2016-08-19 16:28:47 +08:00
|
|
|
case CP_CIRCLE_SHAPE:
|
2012-11-09 17:52:12 +08:00
|
|
|
{
|
|
|
|
cpCircleShape *circle = (cpCircleShape *)shape;
|
|
|
|
cpVect center = circle->tc;
|
|
|
|
cpFloat radius = circle->r;
|
2013-07-12 18:04:32 +08:00
|
|
|
renderer->drawDot(cpVert2Point(center), cpfmax(radius, 1.0), color);
|
2016-03-01 05:53:00 +08:00
|
|
|
renderer->drawSegment(cpVert2Point(center), cpVert2Point(cpvadd(center, cpvmult(cpBodyGetRotation(body), radius))), 1.0, color);
|
2012-11-09 17:52:12 +08:00
|
|
|
}
|
|
|
|
break;
|
2016-08-19 16:28:47 +08:00
|
|
|
case CP_SEGMENT_SHAPE:
|
2012-11-09 17:52:12 +08:00
|
|
|
{
|
|
|
|
cpSegmentShape *seg = (cpSegmentShape *)shape;
|
2021-06-26 17:46:06 +08:00
|
|
|
renderer->drawSegment(cpVert2Point(seg->ta), cpVert2Point(seg->tb), cpfmax(seg->r, 1.0), color);
|
2012-11-09 17:52:12 +08:00
|
|
|
}
|
|
|
|
break;
|
2016-08-19 16:28:47 +08:00
|
|
|
case CP_POLY_SHAPE:
|
2012-11-09 17:52:12 +08:00
|
|
|
{
|
2021-06-26 17:46:06 +08:00
|
|
|
cpPolyShape* poly = (cpPolyShape*)shape;
|
2013-07-05 16:49:22 +08:00
|
|
|
Color4F line = color;
|
2012-11-09 17:52:12 +08:00
|
|
|
line.a = cpflerp(color.a, 1.0, 0.5);
|
2016-03-01 05:53:00 +08:00
|
|
|
int num = poly->count;
|
|
|
|
Vec2* pPoints = new (std::nothrow) Vec2[num];
|
|
|
|
for(int i=0;i<num;++i)
|
|
|
|
pPoints[i] = cpVert2Point(poly->planes[i].v0);
|
2021-06-26 17:46:06 +08:00
|
|
|
if (cpfmax(poly->r, 1.0) > 1.0) {
|
|
|
|
renderer->drawPolygon(pPoints, num, Color4F(0.5f, 0.5f, 0.5f, 0.0f), poly->r, color);
|
|
|
|
} else {
|
|
|
|
renderer->drawPolygon(pPoints, num, color, 1.0, line);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-11-20 16:17:20 +08:00
|
|
|
CC_SAFE_DELETE_ARRAY(pPoints);
|
2012-11-09 17:52:12 +08:00
|
|
|
}
|
|
|
|
break;
|
2016-08-19 16:28:47 +08:00
|
|
|
default:
|
|
|
|
cpAssertHard(false, "Bad assertion in DrawShape()");
|
|
|
|
}
|
2012-11-09 17:52:12 +08:00
|
|
|
}
|
|
|
|
|
2013-07-07 21:08:14 +08:00
|
|
|
static Color4F CONSTRAINT_COLOR(0, 1, 0, 0.5);
|
2012-11-09 17:52:12 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
static void DrawConstraint(cpConstraint *constraint, DrawNode *renderer)
|
2012-11-09 17:52:12 +08:00
|
|
|
{
|
2016-08-19 16:28:47 +08:00
|
|
|
cpBody *body_a = cpConstraintGetBodyA(constraint);
|
|
|
|
cpBody *body_b = cpConstraintGetBodyB(constraint);
|
2012-11-09 17:52:12 +08:00
|
|
|
|
2016-03-01 05:53:00 +08:00
|
|
|
if(cpConstraintIsPinJoint(constraint))
|
2012-11-09 17:52:12 +08:00
|
|
|
{
|
2016-03-01 05:53:00 +08:00
|
|
|
cpVect a = cpvadd(cpBodyGetPosition(body_a), cpvrotate(cpPinJointGetAnchorA(constraint), cpBodyGetRotation(body_a)));
|
|
|
|
cpVect b = cpvadd(cpBodyGetPosition(body_b), cpvrotate(cpPinJointGetAnchorB(constraint), cpBodyGetRotation(body_b)));
|
|
|
|
|
2013-07-12 18:04:32 +08:00
|
|
|
renderer->drawDot(cpVert2Point(a), 3.0, CONSTRAINT_COLOR);
|
|
|
|
renderer->drawDot(cpVert2Point(b), 3.0, CONSTRAINT_COLOR);
|
|
|
|
renderer->drawSegment(cpVert2Point(a), cpVert2Point(b), 1.0, CONSTRAINT_COLOR);
|
2016-03-01 05:53:00 +08:00
|
|
|
}
|
|
|
|
else if(cpConstraintIsSlideJoint(constraint))
|
2012-11-09 17:52:12 +08:00
|
|
|
{
|
2016-03-01 05:53:00 +08:00
|
|
|
cpVect a = cpvadd(cpBodyGetPosition(body_a), cpvrotate(cpSlideJointGetAnchorA(constraint), cpBodyGetRotation(body_a)));
|
|
|
|
cpVect b = cpvadd(cpBodyGetPosition(body_b), cpvrotate(cpSlideJointGetAnchorB(constraint), cpBodyGetRotation(body_b)));
|
2012-11-09 17:52:12 +08:00
|
|
|
|
2013-07-12 18:04:32 +08:00
|
|
|
renderer->drawDot(cpVert2Point(a), 3.0, CONSTRAINT_COLOR);
|
|
|
|
renderer->drawDot(cpVert2Point(b), 3.0, CONSTRAINT_COLOR);
|
|
|
|
renderer->drawSegment(cpVert2Point(a), cpVert2Point(b), 1.0, CONSTRAINT_COLOR);
|
2016-03-01 05:53:00 +08:00
|
|
|
}
|
|
|
|
else if(cpConstraintIsPivotJoint(constraint))
|
2012-11-09 17:52:12 +08:00
|
|
|
{
|
2016-03-01 05:53:00 +08:00
|
|
|
cpVect a = cpvadd(cpBodyGetPosition(body_a), cpvrotate(cpPivotJointGetAnchorA(constraint), cpBodyGetRotation(body_a)));
|
|
|
|
cpVect b = cpvadd(cpBodyGetPosition(body_b), cpvrotate(cpPivotJointGetAnchorB(constraint), cpBodyGetRotation(body_b)));
|
2012-11-09 17:52:12 +08:00
|
|
|
|
2013-07-12 18:04:32 +08:00
|
|
|
renderer->drawDot(cpVert2Point(a), 3.0, CONSTRAINT_COLOR);
|
|
|
|
renderer->drawDot(cpVert2Point(b), 3.0, CONSTRAINT_COLOR);
|
2016-03-01 05:53:00 +08:00
|
|
|
}
|
|
|
|
else if(cpConstraintIsGrooveJoint(constraint))
|
2012-11-09 17:52:12 +08:00
|
|
|
{
|
2016-03-01 05:53:00 +08:00
|
|
|
cpVect a = cpvadd(cpBodyGetPosition(body_a), cpvrotate(cpGrooveJointGetGrooveA(constraint), cpBodyGetRotation(body_a)));
|
|
|
|
cpVect b = cpvadd(cpBodyGetPosition(body_a), cpvrotate(cpGrooveJointGetGrooveB(constraint), cpBodyGetRotation(body_a)));
|
|
|
|
cpVect c = cpvadd(cpBodyGetPosition(body_b), cpvrotate(cpGrooveJointGetAnchorB(constraint), cpBodyGetRotation(body_b)));
|
2012-11-09 17:52:12 +08:00
|
|
|
|
2013-07-12 18:04:32 +08:00
|
|
|
renderer->drawDot(cpVert2Point(c), 3.0, CONSTRAINT_COLOR);
|
|
|
|
renderer->drawSegment(cpVert2Point(a), cpVert2Point(b), 1.0, CONSTRAINT_COLOR);
|
2016-03-01 05:53:00 +08:00
|
|
|
}
|
|
|
|
else if(cpConstraintIsDampedSpring(constraint))
|
2012-11-09 17:52:12 +08:00
|
|
|
{
|
2021-06-24 15:54:02 +08:00
|
|
|
cpDampedSpring* spring = (cpDampedSpring*) constraint;
|
|
|
|
|
|
|
|
cpVect a = cpTransformPoint(body_a->transform, spring->anchorA);
|
|
|
|
cpVect b = cpTransformPoint(body_b->transform, spring->anchorB);
|
|
|
|
|
|
|
|
renderer->drawDot(cpVert2Point(a), 3.0, CONSTRAINT_COLOR);
|
|
|
|
renderer->drawDot(cpVert2Point(b), 3.0, CONSTRAINT_COLOR);
|
|
|
|
|
|
|
|
cpVect delta = cpvsub(b, a);
|
|
|
|
cpFloat cos = delta.x;
|
|
|
|
cpFloat sin = delta.y;
|
|
|
|
cpFloat s = 1.0f / cpvlength(delta);
|
|
|
|
|
|
|
|
cpVect r1 = cpv(cos, -sin * s);
|
|
|
|
cpVect r2 = cpv(sin, cos * s);
|
|
|
|
|
|
|
|
cpVect* verts = (cpVect*) alloca(spring_count * sizeof(cpVect));
|
|
|
|
for (int i = 0; i < spring_count; i++) {
|
|
|
|
cpVect v = spring_verts[i];
|
|
|
|
verts[i] = cpv(cpvdot(v, r1) + a.x, cpvdot(v, r2) + a.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < spring_count - 1; i++) {
|
|
|
|
renderer->drawSegment(cpVert2Point(verts[i]), cpVert2Point(verts[i+1]), 1.0, CONSTRAINT_COLOR);
|
|
|
|
}
|
2016-03-01 05:53:00 +08:00
|
|
|
}
|
2012-11-09 17:52:12 +08:00
|
|
|
else
|
|
|
|
{
|
2021-06-24 15:54:02 +08:00
|
|
|
CCLOG("Cannot draw constraint");
|
2016-03-01 05:53:00 +08:00
|
|
|
}
|
2012-11-09 17:52:12 +08:00
|
|
|
}
|
2020-12-02 00:58:34 +08:00
|
|
|
|
2012-11-09 17:52:12 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
// implementation of PhysicsDebugNode
|
2012-11-09 17:52:12 +08:00
|
|
|
|
2014-05-31 07:42:05 +08:00
|
|
|
void PhysicsDebugNode::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
|
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;
|
|
|
|
}
|
2014-03-05 11:41:46 +08:00
|
|
|
// clear the shapes information before draw current shapes.
|
|
|
|
DrawNode::clear();
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
cpSpaceEachShape(_spacePtr, (cpSpaceShapeIteratorFunc)DrawShape, this);
|
2016-08-19 16:28:47 +08:00
|
|
|
cpSpaceEachConstraint(_spacePtr, (cpSpaceConstraintIteratorFunc)DrawConstraint, this);
|
2012-11-09 17:52:12 +08:00
|
|
|
|
2014-05-31 07:42:05 +08:00
|
|
|
DrawNode::draw(renderer, transform, flags);
|
2012-11-09 17:52:12 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
PhysicsDebugNode::PhysicsDebugNode()
|
2014-01-26 19:35:21 +08:00
|
|
|
: _spacePtr(nullptr)
|
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
|
|
|
{
|
2021-08-05 17:23:55 +08:00
|
|
|
PhysicsDebugNode*node = new (std::nothrow) 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
|
|
|
}
|
|
|
|
|
2020-12-02 00:58:34 +08:00
|
|
|
NS_CC_EXT_END
|