
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]








