Working on debug renderer

This commit is contained in:
Daniel Chappuis 2020-05-06 15:21:25 +02:00
parent 56077bba6b
commit 0321cf4f89
3 changed files with 41 additions and 5 deletions

View File

@ -75,7 +75,8 @@ class DebugRenderer : public EventListener {
COLLIDER_BROADPHASE_AABB = 1 << 1, COLLIDER_BROADPHASE_AABB = 1 << 1,
COLLISION_SHAPE = 1 << 2, COLLISION_SHAPE = 1 << 2,
CONTACT_POINT = 1 << 3, CONTACT_POINT = 1 << 3,
}; CONTACT_NORMAL = 1 << 4,
};
/// Struture that represents a point of the DebugRenderer /// Struture that represents a point of the DebugRenderer
struct DebugPoint { struct DebugPoint {
@ -134,6 +135,9 @@ class DebugRenderer : public EventListener {
/// Default radius of the sphere displayed to represent contact points /// Default radius of the sphere displayed to represent contact points
static constexpr decimal DEFAULT_CONTACT_POINT_SPHERE_RADIUS = decimal(0.1); 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 -------------------- // // -------------------- Attributes -------------------- //
/// Memory allocator /// Memory allocator
@ -154,6 +158,9 @@ class DebugRenderer : public EventListener {
/// Radius of the sphere displayed to represent contact points /// Radius of the sphere displayed to represent contact points
decimal mContactPointSphereRadius; decimal mContactPointSphereRadius;
/// Lenght of contact normal
decimal mContactNormalLength;
// -------------------- Methods -------------------- // // -------------------- Methods -------------------- //
/// Draw an AABB /// Draw an AABB
@ -229,7 +236,13 @@ class DebugRenderer : public EventListener {
/// Set the contact point sphere radius /// Set the contact point sphere radius
void setContactPointSphereRadius(decimal 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); void computeDebugRenderingPrimitives(const PhysicsWorld& world);
/// Clear all the debugging primitives (points, lines, triangles, ...) /// Clear all the debugging primitives (points, lines, triangles, ...)
@ -295,6 +308,17 @@ inline void DebugRenderer::setContactPointSphereRadius(decimal radius) {
mContactPointSphereRadius = 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 #endif

View File

@ -42,12 +42,13 @@ using namespace reactphysics3d;
// Constructor // Constructor
DebugRenderer::DebugRenderer(MemoryAllocator& allocator) DebugRenderer::DebugRenderer(MemoryAllocator& allocator)
:mAllocator(allocator), mLines(allocator), mTriangles(allocator), mDisplayedDebugItems(0), mMapDebugItemWithColor(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, uint32>(DebugItem::COLLIDER_AABB, static_cast<uint32>(DebugColor::MAGENTA))); mMapDebugItemWithColor.add(Pair<DebugItem, uint32>(DebugItem::COLLIDER_AABB, static_cast<uint32>(DebugColor::MAGENTA)));
mMapDebugItemWithColor.add(Pair<DebugItem, uint32>(DebugItem::COLLIDER_BROADPHASE_AABB, static_cast<uint32>(DebugColor::YELLOW))); mMapDebugItemWithColor.add(Pair<DebugItem, uint32>(DebugItem::COLLIDER_BROADPHASE_AABB, static_cast<uint32>(DebugColor::YELLOW)));
mMapDebugItemWithColor.add(Pair<DebugItem, uint32>(DebugItem::COLLISION_SHAPE, static_cast<uint32>(DebugColor::GREEN))); mMapDebugItemWithColor.add(Pair<DebugItem, uint32>(DebugItem::COLLISION_SHAPE, static_cast<uint32>(DebugColor::GREEN)));
mMapDebugItemWithColor.add(Pair<DebugItem, uint32>(DebugItem::CONTACT_POINT, static_cast<uint32>(DebugColor::RED))); mMapDebugItemWithColor.add(Pair<DebugItem, uint32>(DebugItem::CONTACT_POINT, static_cast<uint32>(DebugColor::RED)));
mMapDebugItemWithColor.add(Pair<DebugItem, uint32>(DebugItem::CONTACT_NORMAL, static_cast<uint32>(DebugColor::WHITE)));
} }
// Destructor // Destructor
@ -438,7 +439,7 @@ void DebugRenderer::computeDebugRenderingPrimitives(const PhysicsWorld& world) {
void DebugRenderer::onContact(const CollisionCallback::CallbackData& callbackData) { void DebugRenderer::onContact(const CollisionCallback::CallbackData& callbackData) {
// If we need to draw contact points // 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 each contact pair
for (uint p = 0; p < callbackData.getNbContactPairs(); p++) { 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(); 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]));
}
} }
} }
} }

View File

@ -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::COLLIDER_BROADPHASE_AABB, true);
mPhysicsWorld->getDebugRenderer().setIsDebugItemDisplayed(rp3d::DebugRenderer::DebugItem::COLLISION_SHAPE, 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_POINT, true);
mPhysicsWorld->getDebugRenderer().setIsDebugItemDisplayed(rp3d::DebugRenderer::DebugItem::CONTACT_NORMAL, true);
for (int i=0; i<NB_COMPOUND_SHAPES; i++) { for (int i=0; i<NB_COMPOUND_SHAPES; i++) {