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):
- The ball must bounce off the paddle, the walls, the bricks (as well as destroy the bricks)
- The game should reset when the player loses
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:
-
force_const
: while this isn’t a new field, we wanted to make a note about this becauseforce_const
won’t necessarily be used by all collision force creators (destructive collisions are just one example). However, it will be used by some, and there’s another good reason why we include it that we’ll touch on later. -
handler
: this is a collision handler that will run if a collision occurs between two bodies. Its type,collision_handler_t
, has been defined for you inforces.h
. -
aux
: thisvoid *
field is for any extra information that thecollision_aux_t
will need access to. It’s a little bit meta and might be hard to wrap your head around at first, but this field will be especially helpeful when we need to reference variables like our state or scene.
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.
Task 0.
Modify breakout.c
and test that it works by running make demo
or make NO_ASAN=true demo
(for less lag).
Task 1. When you are done, your group should meet and complete the questions here.