EDIT 2: Hey guys I made a rigid body dynamics system but it suffers from major issues when theres more than 2 bodies colliding at once. The way it works is that it finds which objects are colliding with eachother and puts them into pairs. Each pair being 2 objects colliding with eachother but this doesn't seem to always work. When the three objects collide, they pass through eachother and the program starts to lag intensively because of calculations it does when objects are moving inside of eachother. I was wondering, since all my objects are boxes with a fixed radius, do I need to push the objects back a bit so that they aren't overlapping, and then do the momentum calculations? Whats the way of solving such a problem?

TestStack is a list of all the objects in my engine. TEST_COUNT is the number of objects in my engine. Ispointinbox is the function used to calculate if a point is in a box, I tested it around and it works since no rotation transformations are put on the box. If source code for this function is needed, Ill provide it.

This is the collision function. It works by saying that the object with the most momentum in a collision will lose momentum while the object with the least momentum gains momentum. This is described with the proportion variables. The debug function simply prints a variable.

I was wondering, since all my objects are boxes with a fixed radius, do I need to push the objects back a bit so that they aren't overlapping, and then do the momentum calculations? Whats the way of solving such a problem?

Hmm, looking over the code, i just realized how extremely unoptimized it is

For point 1, I realized that this was happening but I changed it and will remove it for now since I only ever have 2 objects on the screen at once due to limitations of momentum calculations that only involve 2 bodies. Once you get 3 or 4, treating them as seperate 2 body collisions doesn't really work too well.

Point 2, fixed it.

Point 3, Im not sure I udnerstand this correctly. You mean to find the boxes max x, min x, max y and min y and see if the other min/maxes of the other box fall in between?

Point 4, thats just cause I need to pass the position in my TestStack of both objects colliding. Im not sure, is there a easier way of doing this?

Point 5, I ended up changing them. It works properly now.

What are these competing strategies btw? The link is just a list of all the libraries that do physics.

3. Yes, you should test the min and max on each axis for overlap. No overlap on any axis = no collision. (This is the Separating Axis Theorem, which you might want to look up.) As a bonus, the smallest overlap identifies the most useful axis to take for the collision normal.

4. Yes, but you might want to call it with the indices of the colliding pair, not always 0 and 1.

The only strategy I would recommend at this point would be iterative impulses. You need a list of colliding pairs, which you have, but it would be good to do the overlap test before adding them.

The problem generally is that solving a collision for one pair will affect the solution for another pair. So you have to revisit the collisions multiple times and repeat the calculations until every pair is heading apart.

Get it working perfectly for two bodies, then try for more. Good luck, this stuff isn't easy.

Hmm okay, and I know this may seem like a begginer question, but what exactly is the collision normal? From my understanding its the centre of one object to the centre of another when they are colliding. Would that be correct?

Yeah I just realized I only had those two. That was cause I saw that 3+ body collisions didn't work well so I simply stuck with making the collision physics work properly for two objects. I was thinking of doing spatial hashing but like you said, first get the physics working for two objects

The vector between the object centres is only the direction of the collision normal if the objects are circles. From the code above it appears that you are using rectangles.

Think of it as the direction you need to move intersecting objects so that they can be separated with the minimum movement. For non-rotating rectangles this is either the x or y axis. Getting the normal correct is important for collisions looking right.

So if I get this correctly, the normals angle is the angle between the two objects but its length is the distance between the two objects when they don't overlap? When I read up a tutorial, they simply said its the distance between the two objects centre's during collision, but I don't recall ever hearing that the objects can't overlap.