Physics
Table of Contents
School assignments for year 2 classes. Most of the work below was created in a framework supplied by our teacher Mr. Dini.
N-body Simulation #
All bodies have a random velocity at initialization. Collision is ignored in this simulation.
Soft-body Dynamics #
Spherical Collision #
10void BallResponse(Ball& b1, Ball& b2)
11{
12 // Axis of collision
13 Vec3f Axis = (b2.mPos - b1.mPos).Normalize();
14
15 // Velocity projected on the collision axis
16 Vec3f velBefore1 = Axis * DOT(Axis, b1.mVelocity);
17 Vec3f velBefore2 = Axis * DOT(Axis, b2.mVelocity);
18
19 // Velocity after collision
20 float rMass = (1.0f / (b1.mMass + b2.mMass);
21 Vec3f velAfter1 = ((b1.mMass * velBefore1) - (b2.mMass * velBefore1) + (2.0f * b2.mMass * velBefore2)) * rMass);
22 Vec3f velAfter2 = ((2.0f * b1.mMass * velBefore1) - (b1.mMass * velBefore2) + (b2.mMass * velBefore2)) * rMass);
23
24 // the new velocity
25 b1.mVelocity = b1.mVelocity - velBefore1 + velAfter1;
26 b2.mVelocity = b2.mVelocity - velBefore2 + velAfter2;
27}
Bouncing Cube #
The challenge of this assignment was primarily understanding the enormous formula. (and making sure the simulation keeps its energy when the coefficient of restitution is 1.0f)