Box2D Threading Raycasts

General Box2D issues or C++ specific issues
engineer202
Posts: 2
Joined: Sat Jan 23, 2016 10:22 pm

Box2D Threading Raycasts

Postby engineer202 » Sat Jan 23, 2016 11:27 pm

Backstory
I've come across a performance hit when using Box2D's raycasts. I've switched to using a MT version of Box2D which really helps the physics aspect and allows all 4 of my cores to be used but when implement raycasts into my entities (which each do 840 raycasts) most of the time is spent waiting for the main thread to finish it's job.

Main Point
So I've created a crude thread pool to offload the raycasts through a task-worker-queue structure. But when I tested it compared against ST I found MT to be ~twice as slow as ST. I'm not sure whether it's my thread pool structure (which I tested to be faster when the task at hand is CPU intensive, not simple tasks) or that Box2D's raycasts aren't thread supported (I have debugged the code step by step and kinda see that 2 threads never occupy the raycast function at the same time.

Code

Code: Select all

class Task : public b2RayCastCallback
{
   void Box2DRaycast::execute()
   {
      m_world->RayCast(this, m_p1, m_p2);
   }

   float32 Box2DRaycast::ReportFixture(b2Fixture* fixture, const b2Vec2& point, const b2Vec2& normal, float32 fraction)
   {
      if (fraction < m_fraction)
      {
         m_hit = true;
         m_fixture = fixture;
         m_point = point;
         m_normal = normal;
         m_fraction = fraction;
      }
      return fraction;
   }
}

// Test task list
std::vector<Task> tasks(840 * num_entites); // any number of entities
for (Task t : tasks)
   threadpool.add_task(t); // This adds the task to a queue which any waiting thread grabs and executes


Louis Langholtz
Posts: 21
Joined: Tue Dec 20, 2016 10:53 am

Re: Box2D Threading Raycasts

Postby Louis Langholtz » Fri May 05, 2017 7:22 am

HI engineer202,

Sounds like some interesting stuff you're doing!

I am very curious about the following:

1. What's the MT version of Box2D that you switched to using?
2. Are you certain that the MT version that you switched to is really thread safe?
3. If so, how was that ascertained/proved?
3. Did you determine that the ray-casting calculation was CPU intensive enough to make the overhead of the multithreading worthwhile?
4. If so, how did you do that: timing trials, CPU usage stats, or other mechanisms?

Thanks for posting about this!


Return to “Bugs, Requests, and Feedback”



Who is online

Users browsing this forum: No registered users and 1 guest