From 3da481fde75637cbcab7b0fb0cd6832ad7cfd4d9 Mon Sep 17 00:00:00 2001 From: RH Date: Wed, 15 Jan 2025 01:28:05 +1100 Subject: [PATCH] Use correct color type to fix debug drawing in PhysicsWorld (#2334) * Use correct color type * Create debug draw node at time of setting debug draw mask Add accessor to get debug draw node --- core/physics/PhysicsWorld.cpp | 37 +++++++++++++++++++++++------------ core/physics/PhysicsWorld.h | 9 ++++++++- 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/core/physics/PhysicsWorld.cpp b/core/physics/PhysicsWorld.cpp index a2cfcbe8dae6..85389f1ad7e9 100644 --- a/core/physics/PhysicsWorld.cpp +++ b/core/physics/PhysicsWorld.cpp @@ -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(data); float radius = PhysicsHelper::cpfloat2float(r); Vec2 centre = PhysicsHelper::cpv2vec2(p); @@ -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(data); drawNode->drawSegment(PhysicsHelper::cpv2vec2(a), PhysicsHelper::cpv2vec2(b), PhysicsHelper::cpfloat2float(r == 0 ? _debugDrawThickness : r), outlineColor); @@ -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(data); int num = count; Vec2* seg = new Vec2[num]; @@ -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(data); drawNode->drawDot(PhysicsHelper::cpv2vec2(pos), _debugDrawThickness, dotColor); } @@ -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); } @@ -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; diff --git a/core/physics/PhysicsWorld.h b/core/physics/PhysicsWorld.h index 6619298e18c1..90fab7eef708 100644 --- a/core/physics/PhysicsWorld.h +++ b/core/physics/PhysicsWorld.h @@ -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.