Help with dealing with multiple objects collision.

Monkey Forums/Monkey Programming/Help with dealing with multiple objects collision.

Jesse(Posted 2012) [#1]
I have been working on this vector engine. it supposed to be a circle to wall and circle to circle interaction including gravity and friction. I pretty much have it working as it should but I run into a problem. When running the code with multiple balls interacting with each other, most of the time it works fine but every now and then a ball will pass through another ball with out colliding and I can't figure out why. It might have to do with the floating point error but I have done a bunch of test and none seem to fix it.

The code uses recursion as a way to find the recurring collisions and resolving response per frame.

Also friction works good but the gravity is not properly applied and does not work for now. I will add proper gravity once this problem is resolved.

I tried to document it as much as possible to make it "theoretically" easier to understand and hope it helps:

I am posting this code in hope that somebody here can actually help me solve this problem. Here is the code if somebody wants to give it a crack.



[EDIT] WORKED IT OUT. IT DID HAVE TO DO WITH THE FLOATING POINT INACCURACY.

Hopefully somebody here with enough vector math skills can help me solve this problem


Dima(Posted 2012) [#2]
I have only glanced at the code - understanding it would take me quiet some time. Usually collisions fail due to objects moving too fast or incorrect response - such as object getting stuck between two other objects, checking the first properly then bouncing off the second back into the first.

These are just random thoughts as i don't have real feedback, but is this line working as intended? I would imagine that self collision would just continue to the next ball in list and not exit the loop, unless i am mistaken in understanding the code in which case i apologize in advance.

' if collision with self get next object
If ball = ball2 Exit

Cheers,
Dima


wiebow(Posted 2012) [#3]
My experience with line graphics is that you need to run the collision code a lot more then you expect, esp. with fast moving objects. So if you are experiencing that sometimes it works and sometimes not, the prime suspect is usually too few collision checks per frame. Call the collision check 3 or 4 times in a loop and try to see if that fixes your problem.


Jesse(Posted 2012) [#4]
@Dima & wiebow

thanks for trying it out!

The vector math/logic in the example does not allow for speed collision error. No matter how fast the ball is traveling it will still find the point of collision on a single pass.
I am thinking it's most likely an incorrect response that has to do with the floating point error at calculation.

the thing is that while checking collision, a pass through all of the balls is made to check the collision that happens first and it returns the time lapsed relative to the ball movement relative to the frame duration. as it is cycling through all of the collisions it is storing only the collision that happens earliest. and ignoring all of the collisions that happen after.
once it finds the collision(s) that happen first it advances the ball movement only to the point the two balls make contact and reduces the movement vector by that amount and adjust the ball direction(bounce) based on the collision. if there is amount of movement left it will call the "Process Method" again. this will repeat itself until all of the movement is spent. So there is no collision left uncheck and there can not be more than a total of collisions that equal more than the amount of balls at a time.

the only time a collision is ignored is if the two balls distance is less than the total radius of both balls but in theory it should not happen because they never supposed to be at a distance less than the sum of both radius. Hanse my theory of the floating point error.

[edited]

@Dima for this:

if collision with self get next object
If ball = ball2 Exit



There are two loops, the outer loop cycles trough the balls from the first to the last and the inner loop cycles through the loop from the last to the first. that prevents the loop from doing repeated ball collisions. Repeated collisions will start to happen when the inner loop ball is the same as the outer loop ball and is why I have it exits the inner loop.


NoOdle(Posted 2012) [#5]
instead of exit, have you tried continue? Just wondered if exiting the inner loop could cause checks to be missed.

[edit] Also when I attempted dynamic collisions I noticed that I would resolve one collision and cause another in the solution. Might be playing a role in the errors you are noticing. I will have a look through your code tomorrow and give it some more thought when I am less drunk!


Jesse(Posted 2012) [#6]
Thanks NoOdle for taking a look at it.


nstead of exit, have you tried continue?



if I replace the exit with continue, it will do redundant collisions plus it will go in to an endless recursion because of the way collision response is handled. Also the balls that have been collided with will be checked again and is what it's trying to avoid.

I thing I'll wait to hear your opinion when you are sober. ;)

thanks.