ray-casting failure in multi-threading

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
yanbo2u
Posts: 21
Joined: Sun Oct 04, 2015 4:41 pm

ray-casting failure in multi-threading

Post by yanbo2u »

I have a huge task of 100 ray-casting operations (to simulate a lidar sensor).

It hurts the update function response. Then I decided to use a separate thread doing the ray-casting. It works very smoothly during the first a few seconds, then I got a null_pointer_exception during space.RayCast(ray, out rcr), while there is no null variables in the context. I guess there must be something inside the engine that complains. I guess some entities are changed during my thread.

There isn't much information available in IDE's debugger.
yanbo2u
Posts: 21
Joined: Sun Oct 04, 2015 4:41 pm

Re: ray-casting failure in multi-threading

Post by yanbo2u »

so far, I just simply catch this exception without doing anything.

the program runs ok, but this is not a solution.
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: ray-casting failure in multi-threading

Post by Norbo »

Ray casts cannot be used while the broad phase is updating, because its acceleration structure is used to perform the ray casts. You could lock against the Space.BroadPhase.Locker to ensure that it's not updating at the same time, but this introduces a potential stall on the main thread.

Instead, I would recommend doing the rays across multiple threads using a parallel forloop that joins before continuing on the main thread. That way there's no concern about the space updating at the same time.

That said, 100 rays is not a lot of rays. In a scene with 6150 dynamic objects and a few hundred large static meshes (the CharacterStressTestDemo), casting 1000 infinite length rays takes between 8 and 12 milliseconds on a single thread of my 3770K@4.5ghz. About 1.8-2.8 milliseconds multithreaded. If the rays are only 15 units long, the time drops to around 2-3 ms single threaded.

If you're seeing numbers significantly higher than this adjusted for hardware, something weird may be going on.
Post Reply