Ball Mania

Alexandre Mattos
CSC 471 Spring 2006
Zoë Wood


Sample track.

Project Description
Ball Mania had four goals:
1. To experiment with animation.
2. To implement collision detection.
3. To model proper physics of a ball bouncing and rolling on a track.
4. To allow the user to build fun tracks for a ball to roll on.
The user can place different track pieces, and then have a ball roll down the track. The ball accelerates and slows down based on a physics model.

Download (Click Here)

Commands
w - move camera forward

s - move camera backward
a - pan camera left
d - pan camera right
e - erase current piece
z/x - rotate current piece in X-Y plane
p - pause ball
r - reset ball
left mouse - place and select pieces
right mouse - rotate current piece using virtual trackball
q - quit

Collision Detection
The ball was modeled as a point and a vector for the current velocity. Each ramp was modeled as 6 planes (one for each side). Each time the ball was moved, a line was created using the ball's current position and the ball's next position.


The intersection of the ball's trajectory and the ramp's plane was calculated using the following equations:
(Equations and images borrowed from http://astronomy.swin.edu.au/~pbourke/geometry/planeline)
Equation of a plane: Ax + By + Cz + D = 0
First Point P1 = (x1, y1, z1)
Second Point P2 = (x2, y2, z2)
A (x1 + u (x2 - x1)) + B (y1 + u (y2 - y1)) + C (z1 + u (z2 - z1)) + D = 0


Line and plane intersect if 0 < u < 1.
Collision Spot = P1 + u ( P2 - P1)

It was then necessary to check whether the collision spot was within the bounds of the ramp. In order to do this the following equation was used:
Compute the sum of the angles made between the interior point and each pair of points making up the ramp. If the sum was 360 degrees, then the point was within the bounds of the ramp.

Ball Movement
Each time the ball was updated, the current gravity vector was added to the ball's velocity.
Whenever the ball collided with a surface, it was redirected at the reflection angle, minus a certain amount of dampening along the normal of the surface.



The ball had a bounce coefficient that was used to calculate how much of it's velocity would be dampened when colliding with a surface.  
The loss vector was calculated as follows:
loss = (Normal * (Normal * Reflection Vector)) / Bounce + (.1 / Bounce)
The final ball's trajectory was the reflection vector minus the loss vector.

Resources
http://astronomy.swin.edu.au/~pbourke/geometry/planeline/
http://astronomy.swin.edu.au/~pbourke/geometry/insidepoly/