• Create Account

## Double Collision Responses on same Object executing in order: How to fix it?

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

8 replies to this topic

### #1asperatology  Members

687
Like
0Likes
Like

Posted 27 September 2012 - 01:02 AM

Here's a diagram depicting what I meant as "double collision response":

I have in my code a way to work out the collision responses. It resolves the object from being inside the designated collision area by pushing the object outward in correlation to the speed vector.

The code works for 2 objects colliding each other, but never more than 2. There are some cases, apparently becoming frequent, that involves an object colliding with other two objects at the same time.

When that happens, the collision detection executes linearly. In other words, it executes the collision detection one by one. The top of the funnel is always the first to calculate the collision detection and its response to the object. After calculation, it's the bottom collision detection's turn. I see the variables while debugging change rapidly and offcourse, which causes the object itself to get pushed up and down. I think the culprit is the order of execution, where the first collision response of the first collision area made the object move away and towards the second collision area.

The second collision response of the second collision area made the object move away and towards the first collision area. This continues in a tight game loop, which upon viewing, it would look like the object is moving up and down rapidly.

Does anyone know how to fix this? Thanks!

The code in question is here: (2 objects collision response)

[source lang="java"]public void resolveCollision(Ball b) { float xVelocity = this.speed[0] - b.speed[0]; float yVelocity = this.speed[1] - b.speed[1]; float xDist = this.position[0] - b.position[0]; float yDist = this.position[1] - b.position[1]; float dotProduct = xDist * xVelocity + yDist * yVelocity; if (dotProduct > 0) { float distSquared = xDist * xDist + yDist * yDist; float collisionScale = dotProduct / distSquared; float xCollision = xDist * collisionScale; float yCollision = yDist * collisionScale; b.speed[0] += xCollision; b.speed[1] += yCollision; this.speed[0] -= xCollision; this.speed[1] -= yCollision; }}public void collisionResponse(Ball b) { double dx = this.position[0] - b.position[0]; double dy = this.position[1] - b.position[1]; double dist = Math.hypot(dx, dy); double penetration = Math.max(0, radius + b.radius - dist); float radiusA = (float) (penetration * dx / (dist * 2)); float radiusB = (float) (penetration * dy / (dist * 2)); this.position[0] += radiusA; this.position[1] += radiusB; b.position[0] -= radiusA; b.position[1] -= radiusB;}[/source]

### #2Aressera  Members

2667
Like
0Likes
Like

Posted 27 September 2012 - 01:21 AM

There is no easy way to do this without implementing a sequential impulse constraint solver which breaks down the multiple collision case into multiple iterations of pairwise constraints. Check out the code for Box2D or Chipmunk Physics for a simple example.

### #3Rld_  Members

1777
Like
0Likes
Like

Posted 27 September 2012 - 01:48 AM

One way I would try in this situation is to not check for collision at all when it enters. To me it seems that at the moment it enters, there is only one way to go and it's basically "stuck" in between the 2 walls. Once it enters, perhaps you can let it follow a path of some sort and when it goes out again, check for collisions again. This way you also ensure you don't get unwanted behavior from the collision response.

### #4asperatology  Members

687
Like
0Likes
Like

Posted 27 September 2012 - 02:18 AM

One way I would try in this situation is to not check for collision at all when it enters. To me it seems that at the moment it enters, there is only one way to go and it's basically "stuck" in between the 2 walls. Once it enters, perhaps you can let it follow a path of some sort and when it goes out again, check for collisions again. This way you also ensure you don't get unwanted behavior from the collision response.

Do you mean I have to create a pre-determined path once the object gets near? Man, that's a bit challenging to come up with something like that... Especially when creating one...

EDIT: Creating a pre-determined path: Very advanced... It took a lot of energy, but I didn't manage it. Can't find anything that tells me how to start creating a pre-determined path... Bezier curve...

Edited by tom_mai78101, 27 September 2012 - 02:29 AM.

### #5serumas  Members

787
Like
0Likes
Like

Posted 27 September 2012 - 06:52 AM

I wonder why your body stil vibrating after it comes out of "gate"?
I had problem something like yours.
Try to make softer collision responce , how to say, do not responce fully:
softness=0..1;
place more two circles with soft responce on gate circles with full response, but with bigger radius...
but its very dependant on situation in game, good to see real situation in game

Edited by serumas, 27 September 2012 - 06:58 AM.

### #6asperatology  Members

687
Like
0Likes
Like

Posted 27 September 2012 - 08:02 AM

You mean something like this:

EDIT:

Ok, I have created a Beizer Curve. Now, I will be working my way to get the Beizer Curve path correct. Last, the problem may not seem as difficult as I thought it would be. If I just set the starting position of the Beizer Curve as the object's current position, and using boolean conditions to switch to moving on a Beizer Curve, I might be able to move the object through the funnel, and toggle back to letting the user take control and disable the Beizer Curve movement.

The more I see it, the more I believe that this is the solution... Will post in the future.

Picture:

Edited by tom_mai78101, 27 September 2012 - 09:30 AM.

### #7NEXUSKill  Members

475
Like
0Likes
Like

Posted 27 September 2012 - 03:10 PM

Multiple collisions are quite likely, but there is a big difference between checking for collisions and reacting to them, trying to do both at the same time will lead you to this kind of design dilemas.

What I do is check for collisions first, store any positive results, and then give the owner of a collision body a list of references to all the objects it collided with at that frame, then you can address the problem with much more info, you know you collided with two objects and you van adjust the ball trajectory accordingly without causing that ping pong effect.

Its also useful to make a difference between the event of two objects colliding for first time and the detection of collision between two objects we know were colliding last frame.
Game making is godlike

### #8asperatology  Members

687
Like
0Likes
Like

Posted 28 September 2012 - 03:56 AM

Do you know how to adjust the ball trajectory? I would have 2 vectors obtained from two different collision events, but I would also have no clue on making them "merge" into one vector.

### #9NEXUSKill  Members

475
Like
0Likes
Like

Posted 28 September 2012 - 10:47 AM

I'm rusty on the math for that, perhaps if you ask in the math & physics forum...
Game making is godlike