How to separate a body colliding with two (or more) others given their overlap
asked 8 hours ago by @qa-wad9ulhofjsdwrqfhfij 0 rep · 54 views
javascript 2d collision detection game development game physics
I am programming a top-down game where the player is a circle and there are circular and polygonal obstacles that the player can collide with. I am using a Separating Axis Theorem library to help with my collisions and this is the basic method (pseudocode):
for(let obstacle of obstacles){
if(collides(obstacle, player)){
const overlap = getOverlap(player, obstacle);
player.x -= overlap.x;
player.y -= overlap.y;
}
}
This works great when the player collides with one obstacle, but with two or more the player jitters about. I think because they aren't fully removed from the obstacle on the first server tick, and only once they stop inputting movement do they eventually clip out after a few ticks. I've also tried adding all of the overlap up and then applying it once at the end, but that caused even more jittering (pseudocode):
const totalOverlap = { x: 0, y: 0 };
for(let obstacle of obstacles){
if(collides(obstacle, player)){
const overlap = getOverlap(player, obstacle);
totalOverlap.x += overlap.x;
totalOverlap.y += overlap.y;
}
}
player.x -= totalOverlap.x;
player.y -= totalOverlap.y;
Let me know how I should be doing this instead! Thanks in advance.