I've been stuck for the past few days on implementing multiple body collision response, and I can't seem to find any good resource to help me advance.
Colliding between two objects is simple enough. What I have so far is this:
public static void ResolveCollision(ref Vector3 aVelocity, float aMass,
ref Vector3 bVelocity, float bMass)
{
var totalMass = aMass + bMass;
var temp = (aMass / totalMass * aVelocity) + (bMass / totalMass * bVelocity);
bVelocity = (bMass / totalMass * bVelocity) + (aMass / totalMass * aVelocity);
aVelocity = temp;
}
This works fine in resolving collisions individually between objects. The issue that I need to resolve is when two or more bodies are involved in the calculation. For example, pushing an object against a wall or pushing two objects next to each other in the same direction. The component missing in my code is summing up the masses of the objects involved so that the final displacement is believable.
I am hesitant to implement a solution that once resolving the collision between A and B, that B will go and test its movement and resolve collisions between B and C, and return a response back to A to adjust itself to account for any findings, because that could become messy and recursive.
I have also considered an "island" approach, where connecting bodies form an "island" that has a total mass which would be used when resolving collisions. However, this does not solve for the case of pushing an object against a wall, as I would omit walls and floors from being part of islands as their infinite mass would affect calculations of pushing objects vertially or horizontally respectively, if they are contributing their mass to the island.
Are either of my suggestions close to the mark, or what kind of pattern could I use to efficiently resolve collisions between multiple bodies?