diff --git a/include/reactphysics3d/utils/DebugRenderer.h b/include/reactphysics3d/utils/DebugRenderer.h index b5978e4e..01344d7e 100644 --- a/include/reactphysics3d/utils/DebugRenderer.h +++ b/include/reactphysics3d/utils/DebugRenderer.h @@ -75,7 +75,8 @@ class DebugRenderer : public EventListener { COLLIDER_BROADPHASE_AABB = 1 << 1, COLLISION_SHAPE = 1 << 2, CONTACT_POINT = 1 << 3, - }; + CONTACT_NORMAL = 1 << 4, + }; /// Struture that represents a point of the DebugRenderer struct DebugPoint { @@ -134,6 +135,9 @@ class DebugRenderer : public EventListener { /// Default radius of the sphere displayed to represent contact points static constexpr decimal DEFAULT_CONTACT_POINT_SPHERE_RADIUS = decimal(0.1); + /// Default radius of the sphere displayed to represent contact points + static constexpr decimal DEFAULT_CONTACT_NORMAL_LENGTH = decimal(1.0); + // -------------------- Attributes -------------------- // /// Memory allocator @@ -154,6 +158,9 @@ class DebugRenderer : public EventListener { /// Radius of the sphere displayed to represent contact points decimal mContactPointSphereRadius; + /// Lenght of contact normal + decimal mContactNormalLength; + // -------------------- Methods -------------------- // /// Draw an AABB @@ -229,7 +236,13 @@ class DebugRenderer : public EventListener { /// Set the contact point sphere radius void setContactPointSphereRadius(decimal radius); - /// Generate the rendering primitives (triangles, lines, ...) of a physics world + /// Return the length of contact normal + decimal getContactNormalLength() const; + + /// Return the length of contact normal + void setContactNormalLength(decimal contactNormalLength); + + /// Generate the rendering primitives (triangles, lines, ...) of a physics world void computeDebugRenderingPrimitives(const PhysicsWorld& world); /// Clear all the debugging primitives (points, lines, triangles, ...) @@ -295,6 +308,17 @@ inline void DebugRenderer::setContactPointSphereRadius(decimal radius) { mContactPointSphereRadius = radius; } + +// Return the length of contact normal +inline decimal DebugRenderer::getContactNormalLength() const { + return mContactNormalLength; +} + +// Return the length of contact normal +inline void DebugRenderer::setContactNormalLength(decimal contactNormalLength) { + mContactNormalLength = contactNormalLength; +} + } #endif diff --git a/src/utils/DebugRenderer.cpp b/src/utils/DebugRenderer.cpp index 43362052..979920ba 100644 --- a/src/utils/DebugRenderer.cpp +++ b/src/utils/DebugRenderer.cpp @@ -42,12 +42,13 @@ using namespace reactphysics3d; // Constructor DebugRenderer::DebugRenderer(MemoryAllocator& allocator) :mAllocator(allocator), mLines(allocator), mTriangles(allocator), mDisplayedDebugItems(0), mMapDebugItemWithColor(allocator), - mContactPointSphereRadius(DEFAULT_CONTACT_POINT_SPHERE_RADIUS) { + mContactPointSphereRadius(DEFAULT_CONTACT_POINT_SPHERE_RADIUS), mContactNormalLength(DEFAULT_CONTACT_NORMAL_LENGTH) { mMapDebugItemWithColor.add(Pair(DebugItem::COLLIDER_AABB, static_cast(DebugColor::MAGENTA))); mMapDebugItemWithColor.add(Pair(DebugItem::COLLIDER_BROADPHASE_AABB, static_cast(DebugColor::YELLOW))); mMapDebugItemWithColor.add(Pair(DebugItem::COLLISION_SHAPE, static_cast(DebugColor::GREEN))); mMapDebugItemWithColor.add(Pair(DebugItem::CONTACT_POINT, static_cast(DebugColor::RED))); + mMapDebugItemWithColor.add(Pair(DebugItem::CONTACT_NORMAL, static_cast(DebugColor::WHITE))); } // Destructor @@ -438,7 +439,7 @@ void DebugRenderer::computeDebugRenderingPrimitives(const PhysicsWorld& world) { void DebugRenderer::onContact(const CollisionCallback::CallbackData& callbackData) { // If we need to draw contact points - if (getIsDebugItemDisplayed(DebugItem::CONTACT_POINT)) { + if (getIsDebugItemDisplayed(DebugItem::CONTACT_POINT) || getIsDebugItemDisplayed(DebugItem::CONTACT_NORMAL)) { // For each contact pair for (uint p = 0; p < callbackData.getNbContactPairs(); p++) { @@ -454,7 +455,17 @@ void DebugRenderer::onContact(const CollisionCallback::CallbackData& callbackDat Vector3 point = contactPair.getCollider1()->getLocalToWorldTransform() * contactPoint.getLocalPointOnShape1(); - drawSphere(point, DEFAULT_CONTACT_POINT_SPHERE_RADIUS, mMapDebugItemWithColor[DebugItem::CONTACT_POINT]); + if (getIsDebugItemDisplayed(DebugItem::CONTACT_POINT)) { + + // Contact point + drawSphere(point, DEFAULT_CONTACT_POINT_SPHERE_RADIUS, mMapDebugItemWithColor[DebugItem::CONTACT_POINT]); + } + + if (getIsDebugItemDisplayed(DebugItem::CONTACT_NORMAL)) { + + // Contact normal + mLines.add(DebugLine(point, point + contactPoint.getWorldNormal() * mContactNormalLength, mMapDebugItemWithColor[DebugItem::CONTACT_NORMAL])); + } } } } diff --git a/testbed/scenes/collisionshapes/CollisionShapesScene.cpp b/testbed/scenes/collisionshapes/CollisionShapesScene.cpp index dd60fdbd..1958bb76 100644 --- a/testbed/scenes/collisionshapes/CollisionShapesScene.cpp +++ b/testbed/scenes/collisionshapes/CollisionShapesScene.cpp @@ -59,6 +59,7 @@ CollisionShapesScene::CollisionShapesScene(const std::string& name, EngineSettin mPhysicsWorld->getDebugRenderer().setIsDebugItemDisplayed(rp3d::DebugRenderer::DebugItem::COLLIDER_BROADPHASE_AABB, true); mPhysicsWorld->getDebugRenderer().setIsDebugItemDisplayed(rp3d::DebugRenderer::DebugItem::COLLISION_SHAPE, true); mPhysicsWorld->getDebugRenderer().setIsDebugItemDisplayed(rp3d::DebugRenderer::DebugItem::CONTACT_POINT, true); + mPhysicsWorld->getDebugRenderer().setIsDebugItemDisplayed(rp3d::DebugRenderer::DebugItem::CONTACT_NORMAL, true); for (int i=0; i