CS 3 (Spring 2024) Project 06: Collision Resolution (Demo)

In this project, you will add collision resolution to last week’s collision detection and modify your space invaders game into a breakout game.

Demos

The main demo this week is a simple version of Breakout.

Your game demo must have the following features (but feel free to play around and implement anything you want otherwise):

We have give you starter code along with some in-line comments for the demo.

Note that you should only need to replace the parts in demo.c marked with TODO.

Before you start, however, here are a few things we want to go over:

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_force_creator() 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:

Finally, notice how we aren’t declaring a new collision_aux_free function. As of now, when the scene tries to free the force creator, it will use body_aux_free. Will that be a problem?

It actually won’t because of how we define collision_aux_t. Look at how the collision_aux_t struct is defined: the first two fields are the exact same as all the fields in body_aux_t. This allows casting the collision_aux_t to be casted as a body_aux_t, so body_aux_free will work as expected.

Furthermore, none of the other fields in collision_aux_t need to be freed. It could be argued that aux should be owned and freed by the collision_aux_t, but to keep only one free function, we’ll give the ownership of aux to the caller. At the end of the day, this is a design choice that we’ve made, and there’s not necessarily a “right” answer.

create_physics_collision and create_destructive_collision

We’ve defined a generic create_collision function, which is referenced in create_physics_collision and create_destructive_collision (the latter will be implemented by the engine student). The key takeaway here is that abstraction can make our code much simpler and also more flexible! Note how create_physics_collision doesn’t actually needs to pass in an extra aux, but it does need to pass in a constant (the elasticity).

Look over these functions and their documentation carefully! You will be referencing these functions and making your own collision functions in breakout.c.

body_reset

We’ve added a new body_reset function for you in body.h that resets the force and impulse of the body.