Code Correctness
Throughout this course, we also want you to focus on your code quality in addition to the functionality of your code. For the complete list of code quality items we’ll look at throughout the quarter, please consult the code quality document. If we see code quality issues during code reviews, we will ask your group to resubmit the project with those issues fixed.
Force Abstraction
This week, our scene abstraction also needs to support “registering” force and impulses (in a similar way to how sdl_wrapper supports
registering key handlers). To do this, you will need to implement some new functionality in body and scene. The idea is, during
each tick, the scene runs all attached forces. The forces can be “registered” using the scene_add_force_creator function. We have
declared a force_creator_t typedef which takes in a piece of auxillary state to be used by the actual implementation of the force.
Additionally, implementation of force creators for gravity between two bodies, a spring force between two bodies, and drag on a single body have been provided.
Consider what additional abstractions you can make to link the provided forces to your scene and bodies.
Before you start, copy over the body.c, scene.c, sdl_wrapper.c, and vector.c files (in library) from last week’s repo to this week’s repo.
Do not copy over their respective .h files since we have updated them this week.
We have provided you with new versions of the relevant interfaces (body.h and scene.h) that you’ll have to implement in body.c and scene.c. Note that documentation for existing functions might have updated, so make sure you to fix your functions such that they adhere to the updated header files (the Compare feature in VSCode is helpful for this).
The list of functions that you will need to implement or modify are:
- In
scene:scene_add_force_creatorscene_tickscene_initscene_free
- In
body:body_add_forcebody_add_impulsebody_tickbody_initbody_reset
All the documentation for these functions can be ound in their respective .h files.
You are allowed to add functionality to forces.c and forces.h, but not remove any existing code.
Here are a few hints:
- A force creator is a function that takes in a list of bodies and an aux struct which contains a force constant. It applies a force, such as Newtonian gravity, spring, and drag force, to the list of bodies provided to it when the function is called.
- Examples of this can be seen in forces.c, which we have provided for you.
- You should create a struct and any additionally functions (init, free) to support this struct to store the information of the force creator and its associated aux when a force is added via
scene_add_force_creator. This struct should be used to call theforce_creator_tfunction on itsaux_tand bodies every timescene_tickis called.
Testing
We have provided tests for the physics engine files. All provided tests must pass in order to receive full credit. As a reminder, these can be run with make test.
Below is a demo of “damping” which you can run to test your spring and drag forces in action. We have provided the demo binary, so it only requires a correct implementation of the engine to work. If the engine is implemented correctly, the damping demo should have the following behavior:
To run the above demo, run the following command: make damping.
Once all your group members have finished, work with them to complete the extension (more information in the main project page).