Game Physics 2 Assignment 2 - Rotations Study Notes

Assignment Overview

  • Course: Game Physics 2 (Humber College, Game Programming Advanced Diploma).

  • Instructor: Dr. Umer Noor.

  • Assignment: Assignment 2 – Rotations.

  • Grade Value: 25%25\%.

  • Format: Individual assignment; peer consultation is allowed, but sharing work or submitting non-original content constitutes academic misconduct.

  • Objective: To build a C++ simulation where a sphere (ball) rolls along a plane based on the plane's orientation, controlled via the keyboard. This involves implementing rotational physics on top of existing linear physics knowledge.

Development Setup and Scene Configuration

  • Base Code: The project is built in tandem with the Computer Graphics course. It uses starter code initially provided in that course.

  • Visual Identification: To identify objects in 3D, the simulation should be run in wireframe mode.

    • Key "w": Toggles wireframe mode.

    • Implementation Detail: Set drawInWireMode to true within Scene0 or the relevant scene code to keep it active by default.

  • File Structure:

    • Create Scene1p.cpp and Scene1p.h (the "p" denotes physics).

    • Modify SceneManager to execute Scene1p.

Rotational Physics Variables (Body Class)

The Body class must be extended to support rotational dynamics. The following variables are required:

  • Angular Dynamics:

    • angularVel: A Vec3 representing the angular velocity.

    • angularAcc: A Vec3 representing the angular acceleration.

  • Mass and Inertia:

    • rotationalInertia: A Matrix3 representing the object's resistance to rotational acceleration.

    • radius: A float representing the size of the Body (initial value set to 1.0f1.0f).

  • Orientation:

    • orientation: A Quaternion representing the object's current rotation in 3D space.

Core Physics Methods

ApplyTorque
  • Function: void ApplyTorque(Vec3 torque)

  • Mathematical Model: Updates angular acceleration by multiplying the torque vector by the inverse of the rotational inertia matrix.

  • Ball Rolling Physics: A ball rolling on a surface rotates about the contact point, not its center of mass. This requires the Parallel Axis Theorem to adjust the rotational inertia.

    • For a solid sphere, the inertia is typically 25×m×r2\frac{2}{5} \times m \times r^2.

    • Using Parallel Axis Theorem, add m×r2m \times r^2 to the main diagonal terms of the inertia matrix.

UpdateAngularVelocity
  • Function: void UpdateAngularVel(float deltaTime)

  • Equation: new angular velocity=old angular velocity+(angular acceleration×deltaTime)\text{new angular velocity} = \text{old angular velocity} + (\text{angular acceleration} \times \text{deltaTime})

UpdateOrientation
  • Function: void UpdateOrientation(float deltaTime)

  • Process:

    1. Find the axis of rotation by normalizing the angularVel vector.

    2. Calculate the rotation angle (in radians): angle=angular velocity×deltaTime\text{angle} = |\text{angular velocity}| \times \text{deltaTime}.

    3. Convert the angle to degrees.

    4. Create a quaternion using a library function, such as: Quaternion rotation = QMath::angleAxisRotation(angleDegrees, axis).

    5. Update orientation: orientation = rotation * orientation.

Spatial Updates
  • UpdatePos(float deltaTime): Standard linear position update.

  • UpdateVel(float deltaTime): Standard linear velocity update.

  • GetModelMatrix(): Builds a combined transformation matrix for the GPU.

    • T=MMath::translate(pos)T = \text{MMath::translate(pos)}

    • R=MMath::toMatrix4(orientation)R = \text{MMath::toMatrix4(orientation)}

    • S=MMath::scale(Vec3(radius, radius, radius))S = \text{MMath::scale(Vec3(radius, radius, radius))}

    • Return: T×R×ST \times R \times S

Scene Implementation: The Plane

  • Mesh: Use Plane.obj located in the meshes folder.

  • Scene Variables:

    • Body* plane

    • Mesh* planeMesh

    • Plane planeShape (Custom struct)

  • Plane Struct Definition:

    • Vec3 normal (e.g., Vec3(0, 0, 1.0f) initially).

    • float d (distance from origin, default 0.0f0.0f).

  • Controls (WASD keys):

    • Use SDL_SCANCODE_W, SDL_SCANCODE_A, etc., to rotate the plane.

    • Function: plane->orientation *= QMath::angleAxisRotation(-deltaTheta, axis).

    • Orientation updates to the plane must also rotate its mathematical normal: planeShape->normal = QMath::rotate(planeShape->normal, rot).

Torque Calculation for Rolling

Torque is what causes the ball to accelerate down a ramp. It is calculated in two parts:

1. Torque Magnitude
  • Finding the Angle (θ): The angle between the plane's normal and the "Up" vector.

    • Using the dot product: UpNormal=cos(θ)\text{Up} \cdot \text{Normal} = \cos(\theta), assuming both vectors are normalized.

  • Torque Magnitude Formula: Torque Magnitude=weight of ball×radius of ball×sin(θ)\text{Torque Magnitude} = \text{weight of ball} \times \text{radius of ball} \times \sin(\theta)

2. Torque Direction (Rotation Axis)
  • The axis of rotation is perpendicular to both the "Up" vector and the plane's normal vector.

  • The axis of rotation is determined by the interaction of the "Up" vector and the plane's normal vector. For instance, if the Up vector is represented as Up=(0,1,0)\text{Up} = (0, 1, 0) (pointing along the y-axis) and the plane's normal vector is given as Normal=(0,0,1)\text{Normal} = (0, 0, 1) (pointing along the z-axis), the axis of rotation can be found using the cross product of these two vectors: Rotation Axis=Up×Normal=(0,1,0)×(0,0,1)=(1,0,0).\text{Rotation Axis} = \text{Up} \times \text{Normal} = (0, 1, 0) \times (0, 0, 1) = (1, 0, 0). Thus, the rotation axis is along the x-axis, indicating that rotation occurs around this axis when the plane rotates. This relationship allows the ball to roll along the surface defined by these vectors effectively.

  • Cross Product: Use the cross product to find this axis.

  • Final Torque Vector: torque=torqueMag×normalized RotationAxis\mathbf{\text{torque}} = \text{torqueMag} \times \text{normalized RotationAxis}

Combining Rotational and Linear Motion

To simulate rolling (where rotation drives translation), linear velocity must be calculated from angular velocity.

  • Equation: v=ω×r\mathbf{v} = \boldsymbol{\omega} \times \mathbf{r}

  • In 3D Simulation: Use VMath::cross(sphere->angularVel, rVector).

  • The rVector: This is the vector from the sphere's center to the contact point. It is identical to the plane's normal vector but scaled to the magnitude of the sphere's radius.

Surface Constraints

To prevent the ball from hovering or sinking into the plane, ensure the distance between the center of the sphere and the plane is always exactly equal to the radius.

  • Point-to-Plane Distance Formula: Use this mathematical formula from Game Mathematics 2 to adjust the ball's position along the normal as it rolls.

Grading Rubric Details

  • Part 1 (5%): Ball Spins and Translates

    • Relates linear and angular velocity.

    • Correct implementation of underlying math and C++ Core Guidelines.

  • Part 2 (5%): Rolling Torque and Rotational Inertia

    • Angular acceleration derived from torque and inertia calculations.

  • Part 3 (3%): Rolling Based on Plane Orientation

    • Complete integration where the ball rolls perfectly as the plane rotates in 3D space.

  • Presentation: For all parts, the student must be able to explain the code and perform live-coding/refactoring in person.