Properly removing a body from the simulation (v2)

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
tomweiland
Posts: 99
Joined: Wed May 08, 2019 12:17 am

Properly removing a body from the simulation (v2)

Post by tomweiland »

I'm trying to remove a body from the simulation, but when I use Simulation.Bodies.Remove(bodyHandle) it gives me an error, saying "Index out of range" on line 66 of the Buffer class.

Maybe there's an example on how to do this somewhere in the demos, but I'm not sure where to start looking. What's the proper way to remove a body from a simulation (I don't want to remove the shape, if that changes anything)?
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Properly removing a body from the simulation (v2)

Post by Norbo »

Simulation.Bodies.Remove(bodyHandle) is indeed a correct way to remove a body.

My first guess for that error would be a removal of a body handle that is no longer present in the simulation, but other asserts (ValidateExistingHandle) should have caught that if the library is compiled in debug mode. It's conceivable that something could be corrupted such that the ValidateExistingHandle could be fooled, but that would be pretty unlikely.

There is a pretty thorough stress test covering the add/remove path (FountainStressTestDemo), so it's hard for me to say what the problem would be if ValidateExistingHandle actually ran and passed. It's sufficiently weird that the first thing I think of is multithreaded race conditions- if the physics loop is running on one thread and a different thread attempts to add/remove bodies, this sort of error could easily occur.

A minimal repro would probably be the quickest option again.
tomweiland
Posts: 99
Joined: Wed May 08, 2019 12:17 am

Re: Properly removing a body from the simulation (v2)

Post by tomweiland »

I tried just adding a basic box and trying to remove it, and it gave me a different error: "Batch index must be within the set's batches buffer."

I also checked the threading, and the removal code is running on the physics thread, so that shouldn't be an issue.

Here's the link to the repro, I used the same one as last time. It attempts to remove the object 5 seconds after starting.
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Properly removing a body from the simulation (v2)

Post by Norbo »

A couple of things:
1) Removing a body that a character is standing on confuses the demos character controller; the character sees that it's not supported and tries to redundantly remove its motion constraint. That's a bug in the character- I'll look into fixing it tomorrow.
2) If the character moves off the object before it is removed, the above bug doesn't occur, but the repro continues to attempt to remove the same handle and hits the ValidateExistingHandle check in Remove.
tomweiland
Posts: 99
Joined: Wed May 08, 2019 12:17 am

Re: Properly removing a body from the simulation (v2)

Post by tomweiland »

Norbo wrote: Thu May 30, 2019 4:04 am 1) Removing a body that a character is standing on confuses the demos character controller; the character sees that it's not supported and tries to redundantly remove its motion constraint. That's a bug in the character- I'll look into fixing it tomorrow.
Ok so that might explain why I was getting a different error when I tried it with a basic cube (the collider that I was originally removing hadn't touched the player at all yet).
Norbo wrote: Thu May 30, 2019 4:04 am 2) If the character moves off the object before it is removed, the above bug doesn't occur, but the repro continues to attempt to remove the same handle and hits the ValidateExistingHandle check in Remove.
Are you saying I put the code that tries to remove the collider in a loop? I don't remember doing that :?

Considering that I'm getting 2 different errors it may very well be an issue with that collider for some reason - I'll do some more testing tomorrow morning.
tomweiland
Posts: 99
Joined: Wed May 08, 2019 12:17 am

Re: Properly removing a body from the simulation (v2)

Post by tomweiland »

tomweiland wrote: Thu May 30, 2019 6:01 am Are you saying I put the code that tries to remove the collider in a loop? I don't remember doing that :?
I totally did make a loop :mrgreen:

I did some more investigating in my actual project, and when the character controller isn't touching the test collider it get's removed with no errors. So it's not an issue with my project setup, but rather the specific colliders - do compounds have a different removal process than regular bodies (the one producing the error is a compound)?

Not sure if I'll be able to reproduce it properly since I'm not even sure what's different about it besides the fact that it's a compound.
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Properly removing a body from the simulation (v2)

Post by Norbo »

The character bug should now be fixed: https://github.com/bepu/bepuphysics2/co ... b4beeeefd5

Compounds should not have different behavior with respect to Bodies.Remove. I do not observe any issues in the repro anymore (at least when the removal isn't attempted more than once). If you're seeing another issue, I'll need an updated repro.
tomweiland
Posts: 99
Joined: Wed May 08, 2019 12:17 am

Re: Properly removing a body from the simulation (v2)

Post by tomweiland »

Norbo wrote: Sat Jun 01, 2019 12:37 am The character bug should now be fixed: https://github.com/bepu/bepuphysics2/co ... b4beeeefd5
Sweet!
Norbo wrote: Sat Jun 01, 2019 12:37 am Compounds should not have different behavior with respect to Bodies.Remove.
I suspected that, but good to know.
Norbo wrote: Sat Jun 01, 2019 12:37 am I do not observe any issues in the repro anymore (at least when the removal isn't attempted more than once). If you're seeing another issue, I'll need an updated repro.
There aren't any issues in there anymore. I did some further testing, hoping to figure out how to reproduce the issue in an isolated example, but I came across the source of the issue in the process. I forgot to dispose all the references to an object which was trying to access the collider's position, even after it was removed, which is what was causing the "Index out of range" error.

Does BodyReference.Exists cause errors if the body has been removed? I was using it on a line which was part of the error's stack trace...I thought it would've simply returned false, not caused problems (or maybe it appears to cause problems, but doesn't actually)?

Thanks for all the help, and I'm sorry for taking your time with such a small oversight on my part :?
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Properly removing a body from the simulation (v2)

Post by Norbo »

Does BodyReference.Exists cause errors if the body has been removed? I was using it on a line which was part of the error's stack trace...I thought it would've simply returned false, not caused problems (or maybe it appears to cause problems, but doesn't actually)?
It should just return false if the body has been removed (or if the BodyReference instance is otherwise invalid or uninitialized). That's another bug, thanks for the report! :P

Fixed: https://github.com/bepu/bepuphysics2/co ... ef13ff00cd
tomweiland
Posts: 99
Joined: Wed May 08, 2019 12:17 am

Re: Properly removing a body from the simulation (v2)

Post by tomweiland »

Norbo wrote: Sun Jun 02, 2019 12:39 am It should just return false if the body has been removed (or if the BodyReference instance is otherwise invalid or uninitialized). That's another bug, thanks for the report! :P
No problem! Glad I could help out a little :)
Norbo wrote: Sun Jun 02, 2019 12:39 am Fixed: https://github.com/bepu/bepuphysics2/co ... ef13ff00cd
Awesome!
Post Reply