Genbox / VelcroPhysics

High performance 2D collision detection system with realistic physics responses.
MIT License
666 stars 113 forks source link

Fixed contact list skipping issue #45

Closed dazerdude closed 3 years ago

dazerdude commented 6 years ago

ContactManager.Collide would skip contacts while iterating if Destroy was called. When destroy was called the list was downsized, but i was still incremented. I fixed this by copying the list at the beginning of the iteration.

KonradBreitsprecher commented 6 years ago

Thanks! That resolved the issue of unbalanced OnCollision-OnSeparation events I had.

ngrigoryev commented 6 years ago

I guess, solution has low performance and unnecessary gc work. Is it better to check in 'for' cycle about list sync?

dazerdude commented 6 years ago

Yeah. This definitely isn't as performant as it could be. It does unnecessary allocations and cpu work. It worked for my use case though, and I didn't really want to mess around too much with code I didn't understand that well. The better solution would be to either correct the iteration variable when destroy occurs, or keep a local list of destroyed objects and remove them after the loop completes.

Genbox commented 3 years ago

This should have been resolved after I switched to using a linked list for contacts. Could you please check if it works?

dazerdude commented 3 years ago

Unfortunately I don't think I can repro this anymore. My code is extremely different from where it was when I wrote this PR.

Genbox commented 3 years ago

No problems. Thanks for the PR and everything.