Assignment 3: Game Illustrating Collision Detection

 

Introduction

This assignment involves the creation and rendering of a simple 3D game with collision detection included. The game of pong is chosen. The paddle and ball of the game are modelled in 3D. The game is played on one of the surface of a large cube which acts as a table. For simpicity, GL_LIGHTING is disabled.

 

Theory

The game enviroment is created with a sphere as the ball and a 3D rectanglar block as the paddle. The view is that of a 3rd person, looking over the game from the top, behind the paddle. A large wirecube is created as the walls of the game which the ball will bounce off when hit. The game play is such that the player attempts to prevent the ball from dropping below the paddle by controlling the paddle which acts as an obstacle. When the ball hits the walls or the paddle, it will bounce off the surface and continue moving in the opposite direction. For simplicity, and showing that the collision works, the ball will not disappear if the paddle misses the ball. Instead, the ball will bounce off the wall of the cube and continue with the game.

In order to detect the collision, bounding boxes are used. In the coding of the game, structues such as vector, BoundingBox and ball are used to store the attributes which indicates the boundaries of the object and may be used for collision detection. In particular, Bounding Box are created for the ball and paddle to store the data which may be easily compared for collision detection. The ball structure contains the velocity for the movement of each axis.

Sample code showing the structures

struct vector{ GLfloat x; GLfloat y; GLfloat z; };

struct BoundingBox{ //store the max and min values of a bounding box

vector min;

vector max; };

 struct ball{ //store the ball data

GLfloat x; GLfloat y; GLfloat z; GLfloat xv; //steps of the x axis GLfloat zv; //steps of the z axis GLfloat radius; };

In detecting collision of the ball with the paddle,  comparisons of the ball's bounding box with the paddle extreme end vertex values is done. For the movement of the paddle according to the keybaord input, the bounding box of the paddle is used to compare with the walls values to ensure that the ball does not pass through the walls. Reactions of the ball when collision is detected is simply changing the direction of the velocity which gives a view of bouncing off surfaces.

Sample code on reactions on detection of collision

 if (BallBB.min.z + PongBall.zv <= 0.6|| BallBB.max.z + PongBall.zv >=2||BallBB.max.z + PongBall.zv <= 0.6 ) //hit the top wall

{ PongBall.zv = -PongBall.zv; //change direction }

The bouncing off walls effect is created by negating the ball structures' axis step values. ball.xv is the step for the x-axis while ball.zv is the step for the z-axis.

The keyboard function is incoperated into the game using the glutKeyboardFunc function. glutIdleFunc is used to create the animation while glutPostRedisplay() is used to force a re-rendering of the screen after editing the ball attributes.

 

Program

The codes and .exe of the game may be downloaded here. (Please right click and click as target as). The game is not without errors. When the game first starts, the ball bounce off without hitting a paddle. However, once the ball hits the paddle once or twice, the ball will not bounce off at incorrect. Despite my efforts to check out the initial conditions set in the game (since the errors happened mostly when the game first starts), I am not able to fix the error in time.

 

Conclusion

Collision may be detected and reacted to rather easily with the use of bounding boxes. During the creation of this assignment, I realised some simple mistakes that I made in assignment 2 and these were fixed in this assignment. Much calculations have to be done even when implementing a simple bounding box collision detection game.

References

http://www.ultimategameprogramming.com/OpenGLPage7.htm

Last Updated on 31 August 2003

Done By: Ong Kian Wee Christina

 

 

 

1