Final Project – Laserball

Matthew Shapiro

 

CS161 – Computer Animation and Visualization

March 18, 2002

 

 

Introduction:

For my final project I chose to create a pinball game.  The idea was to create something to demonstrate collision detection using OpenGL.  This is basically your standard pinball game.  The object is to keep the ball from falling through the hole at the bottom of the machine by flipping it up towards the top of the machine by using the flippers, all while trying to rack up points by bouncing the ball off bumpers and things.

 

Implementation:

I started off by creating the flippers which are just GL_TRIANGLES.  I got them to flip by simply applying a rotation matrix whenever the program detects either the left or right shift key being pressed.  Both flippers can flip at the same time so they don’t have to wait for the other to finish moving.  Next I drew lines to represent the boundary and ramps for the machine.  The lines were actually implemented by use of what I called a “plane structure” which holds vertex values and the normal vector.  The normal vector is used to implement the collision detection algorithms.  The next thing I did was draw the ball which is also contained in a structure which holds the ball’s position, current velocity, and current acceleration.  To update the ball’s forces I used Euler integration with gravity to give it smooth movement.  For testing purposes I gave the ball an initial velocity to the left so that it won’t drop straight through the hole.

 

Collision Detection:

This is the heart of the program.  Without collision detection, there would be no digital pinball and thus no program for me.  Unfortunately, there isn’t much collision detection going on in my program.  The idea is that I’ve got the ball and I’ve got it moving; so what if it hits a wall?  Normally the ball doesn’t know that it’s hitting a wall and it doesn’t care.  The method I tried using was to cast a ray from the ball until it intersected a plane (which would be calculated using it’s normal vector and an arbitrary point on the plan).  I could then calculate the distance from the ball to the plane and figure out when and where exactly it was going to hit the plane.  Once the distance between the ball and plane becomes zero we have to implement the “bounce” off the wall.  We would do this by finding the incident vector of the ball, by looking at it’s velocity, then calculating the reflective vector using the normal to the plane.  We then update the ball’s velocity with the new direction obtained from the reflective vector and the ball is on its way looking for something else to bounce off of.  This is the theory anyway.  I spent the better part of two whole days trying to implement this strategy and got no where.  Somehow my Y-components of my reflective vector were blowing up to infinity.  At this point I attempted to implement a strategy that just checks how close the ball is to any plane and when the distance was zero I would give the ball a new velocity direction by basically making it go the opposite direction.  I got close with this one, but figuring out how to make it so the program would check if the ball were hitting each plane was difficult.  For testing purposes I only implemented this for four out of eight planes, not including the flippers.

 

Notes:

I never did get my program fully functional.  To “play” the game the user can either push the space ball to get the ball moving or they can click the “play” button in the control window.  The flippers are controlled by the left and right “shift” keys which flip the left and right flippers, respectively.

 

Demo:

Here’s a short clip of my program in action.  You can see the flippers flipping and the ball bouncing off the left edge and ramp.  Not quite the effects I was going for.  laserball.mpg

 

Here’s another picture of my program.  The white line in the middle of the layout in the first picture is an artifact of my implementation.  It was not intended to be there.  But it is interesing how it appears and disappears with the flipper movement.

 

shdwfonz@cats.ucsc.edu