CS 18 (Spring 2026) Project 04: Collision Resolution (Physics Engine)

In this project, you will add collision resolution to last week’s collision detection.

Collision Resolution

Last week, you implemented find_collision() to detect collisions between two convex polygons. This week, you will implement collision resolution, which changes the velocities of the bodies based on the physics of the collision. Here are your tasks for this week:

Finding the collision axis

When two bodies collide in 3D space, there is a contact plane tangent to both bodies at the point of collision. The collision creates a “normal force” perpendicular to the contact plane. In 2D space, there is an analogous contact line tangent to the bodies. Just as in 3D physics, the applied force is perpendicular to the contact line. We refer to the unit vector perpendicular to the contact line as the “collision axis.” (There are actually two perpendicular directions; we will choose the one that points from the first body towards the second.) In the example below, the black line is the contact line and the red vector is the collision axis (from the green body towards the blue body).

collision

When bodies are polygons rather than continuous shapes, there is no longer a tangent line, but we can approximate a collision axis.

Since you implemented the Separating Axis method last week, the collision axis is simply the axis onto which the shapes’ projections have the least overlap.

Copying files Over

First, copy over the following library files from last week’s project:

You may/may not also have to copy code from last week’s forces.c and forces.h if your teammates added helper functions/structs.

You will have to copy other files over as well, but they will be specified for you in the guide.

Collisions

We have added new collision functions for you in forces.h and forces.c. Like forces, collisions are “registered” in the scene using the create_x() x_collision_handler() pattern. Take a look at them for more detail, and take note of the following things:

collision_aux_t

We’ve added a new collision_aux_t struct in forces.c that the new collision functions reference. Note that its structure is similar to that of body_aux_t with the exception of three additional fields:

Take a look at collision_aux_free and note how aux is owned and freed by the collision_aux_t. Like before, you can pass in NULL as the freer if the aux shouldn’t be freed.

Given this information, your next task is to rewrite create_destructive_collision after implementing destructive_collision.

Note that with the new abstraction that we’ve made for creating collisions, these two functions should be very short.

Collision Axis

First, take a look at collision.h. Since we care about the separating axis now, find_collision now returns a collision_info_t struct that contains whether or not the bodies collided in addition to the axis.

We’ve already rewritten find_collision to use the new return type of compare_collision. Take a look at it to see how compare_collision is supposed to interact with it.

Notice the double “overlap” variables. These variables represent the minimum overlap calculated by the two collision functions. We’ll pass in their addresses to compare_collision so that the function can update their values every time a new minimum overlap is found. If a collision has occurred, we’ll compare the minimum overlap from both calls and return the separating axis that corresponds to the smaller of the two.

Next, copy and paste your implementation of get_max_min_projections and compare_collision from last week into collision.c, taking care not to overwrite the collision_info_t return type. Here are the changes that you will have to make:

Computing the impulse

This week, we’re also adding elastic collisions to our physics engine (specifically, in forces.c).

You may remember from Ph 1a that when bodies collide, momentum is conserved. However, kinetic energy is only conserved when the bodies collide elastically (like billiard balls). You will implement collisions with variable amounts of elasticity, controlled by a parameter double elasticity. If elasticity is 1, the collision is elastic. If elasticity is 0, the collision is completely inelastic; if the bodies are moving along a line, they stick together when they collide. Values of elasticity between 0 and 1 correspond to partially elastic collisions. If elasticity is greater than 1, the collision is “super-elastic”: kinetic energy increases in the collision.

There is a simple formula for the impulse due to a collision with a certain elasticity, so we will apply impulses instead of forces to resolve the collision. The formula can be found on Wikipedia, but here’s an explanation:

Given that

The impulse applied to each body is parallel to the collision axis. The parallel component of the impulse applied to the first body is:

impulse

To conserve momentum, an equal and opposite impulse is applied to the second body. We have added this force_creator for you in forces.c.

Notes

Because bodies with mass INFINITY are not affected by forces or impulses, it is natural to use them as “walls” in a collision. For example, you could have implemented the bounce demo in project01 by making the star collide elastically with an infinite-mass wall on each side of the scene. This requires a minor change to the formula for computing the impulse: if one of the masses is INFINITY, the reduced mass (m_a * m_b / (m_a + m_b)) in the formula becomes the mass of the other body.

If two bodies collide in a tick and an impulse is applied to resolve the collision, they may still be colliding in the next tick. In this case, a second impulse should not be applied to the bodies in the next tick. A collision between two bodies will only be resolved if they were not colliding in the previous tick. The logic for this has been implemented for you already in collision_force_creator.

Once all your group members have finished, work with them to complete the extension (more information in the main project page).