[V2] Remove body issue

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
b1fg2
Posts: 19
Joined: Mon Sep 24, 2018 3:36 pm

[V2] Remove body issue

Post by b1fg2 » Thu Dec 03, 2020 4:21 am

i try to remove the body from the simulation and unregister from contactEvent. But the problem is i still get the body handle from the integrateVelocity function after i remove the body from simulation. Am i doing right?
im using the 2.2 release version
IntegrateVelocity(int handle, in RigidPose pose, in BodyInertia localInertia, int workerIndex, ref BodyVelocity velocity)
{

}

b1fg2
Posts: 19
Joined: Mon Sep 24, 2018 3:36 pm

Re: [V2] Remove body issue

Post by b1fg2 » Thu Dec 03, 2020 6:48 am

I think I found the problem. the scenario is I try to repeatedly spawn a body and destroy it after 2 seconds. While at the same time the PoseIntegratorCallbacks IntegrateVelocity method is processed in another thread. And the New body handle reuses the handle int. Any idea to avoid it?

Norbo
Site Admin
Posts: 4928
Joined: Tue Jul 04, 2006 4:45 am

Re: [V2] Remove body issue

Post by Norbo » Thu Dec 03, 2020 6:42 pm

A few things:
1) The index passed into IntegrateVelocity is not a handle, but rather an index into the contiguous active set. The integrator only touches bodies that are active, so it doesn't do the handle indirection by default. If you want the handle, you'll need to look it up through the Simulation.Bodies.IndexToHandle mapping.
2) If a body is removed, the handle associated with it is freed for reuse by later added bodies. This cannot be avoided. You should treat handles like pointers- if the thing they point to is deleted (removed from the simulation) and you try to dereference them, assume bad things will happen.
3) Adding to/removing from the simulation while it's executing on another thread is guaranteed to eventually break something, even if that isn't what's causing problems here. Bodies, statics, and constraints should only be added or removed while the engine is not executing.

Post Reply