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

Started by
7 comments, last by NEXUSKill 11 years, 6 months ago
Here's a diagram depicting what I meant as "double collision response":

Untitled-7.png

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 [color=#FF0000]first collision response of the first collision area made the object move away and towards the [color=#0000FF]second collision area.

[color=#0000FF]The second collision response of the second collision area made the object move away and towards the [color=#FF0000]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]
Advertisement
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.
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.

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...
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;
b.position[0] -= radiusA*softness;
b.position[1] -= radiusB*softness;
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
You mean something like this:

Untitled-8.png


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:

Untitled-9.png
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

LinkedIn profile: http://ar.linkedin.com/pub/andres-ricardo-chamarra/2a/28a/272


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.
I'm rusty on the math for that, perhaps if you ask in the math & physics forum...
Game making is godlike

LinkedIn profile: http://ar.linkedin.com/pub/andres-ricardo-chamarra/2a/28a/272


This topic is closed to new replies.

Advertisement