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: .
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
drawInWireModetotruewithinScene0or the relevant scene code to keep it active by default.
File Structure:
Create
Scene1p.cppandScene1p.h(the "p" denotes physics).Modify
SceneManagerto executeScene1p.
Rotational Physics Variables (Body Class)
The Body class must be extended to support rotational dynamics. The following variables are required:
Angular Dynamics:
angularVel: AVec3representing the angular velocity.angularAcc: AVec3representing the angular acceleration.
Mass and Inertia:
rotationalInertia: AMatrix3representing the object's resistance to rotational acceleration.radius: Afloatrepresenting the size of theBody(initial value set to ).
Orientation:
orientation: AQuaternionrepresenting 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 .
Using Parallel Axis Theorem, add to the main diagonal terms of the inertia matrix.
UpdateAngularVelocity
Function:
void UpdateAngularVel(float deltaTime)Equation:
UpdateOrientation
Function:
void UpdateOrientation(float deltaTime)Process:
Find the axis of rotation by normalizing the
angularVelvector.Calculate the rotation angle (in radians): .
Convert the angle to degrees.
Create a quaternion using a library function, such as:
Quaternion rotation = QMath::angleAxisRotation(angleDegrees, axis).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.
Return:
Scene Implementation: The Plane
Mesh: Use
Plane.objlocated in the meshes folder.Scene Variables:
Body* planeMesh* planeMeshPlane planeShape(Custom struct)
Plane Struct Definition:
Vec3 normal(e.g.,Vec3(0, 0, 1.0f)initially).float d(distance from origin, default ).
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: , assuming both vectors are normalized.
Torque Magnitude Formula:
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 (pointing along the y-axis) and the plane's normal vector is given as (pointing along the z-axis), the axis of rotation can be found using the cross product of these two vectors: 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:
Combining Rotational and Linear Motion
To simulate rolling (where rotation drives translation), linear velocity must be calculated from angular velocity.
Equation:
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.