Skip to content

Commit

Permalink
Use correct color type to fix debug drawing in PhysicsWorld (#2334)
Browse files Browse the repository at this point in the history
* Use correct color type

* Create debug draw node at time of setting debug draw mask
Add accessor to get debug draw node
  • Loading branch information
rh101 authored Jan 14, 2025
1 parent 1eec339 commit 3da481f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
37 changes: 24 additions & 13 deletions core/physics/PhysicsWorld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,8 @@ static void DrawCircle(cpVect p,
cpSpaceDebugColor fill,
cpDataPointer data)
{
const Color4B fillColor(fill.r, fill.g, fill.b, fill.a);
const Color4B outlineColor(outline.r, outline.g, outline.b, outline.a);
const Color4F fillColor(fill.r, fill.g, fill.b, fill.a);
const Color4F outlineColor(outline.r, outline.g, outline.b, outline.a);
DrawNode* drawNode = static_cast<DrawNode*>(data);
float radius = PhysicsHelper::cpfloat2float(r);
Vec2 centre = PhysicsHelper::cpv2vec2(p);
Expand All @@ -250,7 +250,7 @@ static void DrawFatSegment(cpVect a,
cpSpaceDebugColor /*fill*/,
cpDataPointer data)
{
const Color4B outlineColor(outline.r, outline.g, outline.b, outline.a);
const Color4F outlineColor(outline.r, outline.g, outline.b, outline.a);
DrawNode* drawNode = static_cast<DrawNode*>(data);
drawNode->drawSegment(PhysicsHelper::cpv2vec2(a), PhysicsHelper::cpv2vec2(b),
PhysicsHelper::cpfloat2float(r == 0 ? _debugDrawThickness : r), outlineColor);
Expand All @@ -268,8 +268,8 @@ static void DrawPolygon(int count,
cpSpaceDebugColor fill,
cpDataPointer data)
{
const Color4B fillColor(fill.r, fill.g, fill.b, fill.a);
const Color4B outlineColor(outline.r, outline.g, outline.b, outline.a);
const Color4F fillColor(fill.r, fill.g, fill.b, fill.a);
const Color4F outlineColor(outline.r, outline.g, outline.b, outline.a);
DrawNode* drawNode = static_cast<DrawNode*>(data);
int num = count;
Vec2* seg = new Vec2[num];
Expand All @@ -283,7 +283,7 @@ static void DrawPolygon(int count,

static void DrawDot(cpFloat /*size*/, cpVect pos, cpSpaceDebugColor color, cpDataPointer data)
{
const Color4B dotColor(color.r, color.g, color.b, color.a);
const Color4F dotColor(color.r, color.g, color.b, color.a);
DrawNode* drawNode = static_cast<DrawNode*>(data);
drawNode->drawDot(PhysicsHelper::cpv2vec2(pos), _debugDrawThickness, dotColor);
}
Expand Down Expand Up @@ -317,11 +317,13 @@ static cpSpaceDebugColor ColorForShape(cpShape* shape, cpDataPointer /*data*/)

void PhysicsWorld::debugDraw()
{
if (_debugDraw == nullptr)
if (!_debugDraw)
{
return;
}

if (!_debugDraw->getParent())
{
_debugDraw = DrawNode::create();
_debugDraw->setIsolated(true);
_debugDraw->retain();
Director::getInstance()->getRunningScene()->addChild(_debugDraw);
}

Expand Down Expand Up @@ -855,10 +857,19 @@ void PhysicsWorld::removeAllBodies()

void PhysicsWorld::setDebugDrawMask(int mask)
{
if (mask == DEBUGDRAW_NONE && _debugDraw)
if (mask == DEBUGDRAW_NONE)
{
_debugDraw->removeFromParent();
AX_SAFE_RELEASE_NULL(_debugDraw);
if (_debugDraw)
{
_debugDraw->removeFromParent();
AX_SAFE_RELEASE_NULL(_debugDraw);
}
}
else if (!_debugDraw)
{
_debugDraw = DrawNode::create();
_debugDraw->setIsolated(true);
_debugDraw->retain();
}

_debugDrawMask = mask;
Expand Down
9 changes: 8 additions & 1 deletion core/physics/PhysicsWorld.h
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,14 @@ class AX_DLL PhysicsWorld
*
* @return An integer number.
*/
int getDebugDrawMask() { return _debugDrawMask; }
int getDebugDrawMask() const { return _debugDrawMask; }

/**
* Get the debug draw node
*
* @return Pointer to draw node, which may be nullptr
*/
DrawNode* getDebugDraw() const { return _debugDraw; }

/**
* To control the step of physics.
Expand Down

0 comments on commit 3da481f

Please sign in to comment.