Introduction
For CPE471, I created a relatively simple physics simulation. This physics simulation modeled balls in a cube-shaped room.
Implementation
The physics engine works by keeping track of the positions of all objects in the scene (as must be done anyway to know where to draw them). These objects also have a velocity vector which is tracked by the engine. Every tick of the engine, which happens on each redraw of the scene, the objects move by their velocity. Gravity is implemented as a constant acceleration on every sphere in the scene (planes are considered fixed), which occurs every tick.
These collisions are all modeled as realistic inelastic collisions in three dimensions. This requires collision detection between spheres and spheres (which is performed using bounding spheres), and between sphere and planes (which is also performed mathematically).
Once a collision has been detected, the collision vector must be calculated from the collision point. The velocity of the object is then projected onto this collision vector, to determine the portion of the velocity in the collision direction. This velocity is swapped between the two objects (except for planes, which are immovable, in which case it is reflected to the sphere), while the rejection (which represents the velocity vector in the direction parallel to the collision vector) is kept. This is described in more detail here.
These collisions can take place between balls of differing size and mass, and the physics engine takes these properties into account, adjusting the transfer of momentum appropriately. This can be seen in the video below, where the balls summoned are of different sizes and masses (the dark blue ball is light and small, the red ball is light and large, and the light blue ball is small and heavy). This video shows off most of the interesting and realistic properties of the physics engine.
Fun Stuff
There are also some fun properties of the system. The video playlist below shows a black ball (which has a very large mass), and a white ball, which has been modified to be completely elastic (no momentum lost in collisions). You can also see the direction of gravity being manipulated on a large number of balls, and a few other intesting things.