Archive 17/01/2023.

How do I draw debug geometry?

TheComet

I’m trying to draw CollisionShapes because things aren’t doing what they’re supposed to in my physics but I can’t figure out how to do that.

I thought it would have been as simple as:

PhysicsWorld* world = scene_->GetComponent<PhysicsWorld>(); world->DrawDebugGeometry(scene_->CreateComponent<DebugRenderer>());

I also tried that in conjunction with:

I’m not getting anywhere because there’s no mention of it in the documentation. At least not in a way I can understand.

Dave82

[quote=“TheComet”]I’m trying to draw CollisionShapes because things aren’t doing what they’re supposed to in my physics but I can’t figure out how to do that.

I thought it would have been as simple as:

PhysicsWorld* world = scene_->GetComponent<PhysicsWorld>(); world->DrawDebugGeometry(scene_->CreateComponent<DebugRenderer>());

I also tried that in conjunction with:

I’m not getting anywhere because there’s no mention of it in the documentation. At least not in a way I can understand.[/quote]

The easiest way is you create a debug renderer and draw necessary data in E_POSTRENDERUPDATE. This will ensure your debug data will be drawed on top of other graphical elements in the scene.

Just subscribe to this event and draw your debug data there :

[code]void MyApp::Start()
{
scene->CreateComponent();
SubscribeToEvent(E_POSTRENDERUPDATE, URHO3D_HANDLER(MyApp, drawDebug));
}

void MyApp::drawDebug(Urho3D::StringHash eventType, Urho3D::VariantMap& eventData)
{
DebugRenderer * dbgRenderer = scene->GetComponent();
if (dbgRenderer)
{
// Draw navmesh data
DynamicNavigationMesh * navMesh = scene->GetComponent();
navMesh->DrawDebugGeometry(dbgRenderer,false);

       // Draw Physics data :
       PhysicsWorld * phyWorld = scene->GetComponent<PhysicsWorld>();
       phyWorld->DrawDebugGeometry(dbgRenderer,false); 

      // etc
  } 

}[/code]

It is recommended to nullcheck your components (PhysicsWorld , DynamicMesh etc) before you draw them

TheComet

Thanks a lot, that worked!

cirosantilli

Now also shown on the Physics sample: https://github.com/urho3d/Urho3D/blob/9b22e16324276ad58eafff645d3f5721059a29f6/Source/Samples/11_Physics/Physics.cpp#L315