Collision Detection

advertisement
CS4281 Internet Application Development
Anthony, Chan Ho Kwing 96116563
Dennis, Yu Hon Fai 96096615
Marius, Yung Ka Ho 96103600
OBJECTIVES .................................................................................................................................... 2
GAME FEATURES ........................................................................................................................... 3
DOUBLE BUFFERED SMOOTH ANIMATION........................................................................................... 3
COLLISION DETECTION ..................................................................................................................... 3
BACKGROUND ANIMATION................................................................................................................ 3
OBSTACLES ....................................................................................................................................... 3
CLOCK .............................................................................................................................................. 3
SYSTEM DESIGN AND ARCHITECTURE .................................................................................. 4
GAME LOOP ...................................................................................................................................... 4
THE COLLIDER CLASS AND ITS’ SUBCLASSES ..................................................................................... 4
THE BACKGROUND CLASS AND ITS’ SUBCLASSES .............................................................................. 6
COLLISION DETECTION .............................................................................................................. 7
CIRCLE TO CIRCLE COLLISION DETECTION ......................................................................................... 7
CIRCLE TO LINE SEGMENT COLLISION DETECTION ............................................................................. 7
INTERFACE DESIGN AND USER MANUAL .............................................................................. 9
INTERFACE DESIGN ........................................................................................................................... 9
USER MANUAL................................................................................................................................ 11
CONCLUSION................................................................................................................................. 12
WHAT WE HAVE LEARNED ............................................................................................................... 12
CONTRIBUTIONS.............................................................................................................................. 12
FILE LISTING................................................................................................................................. 13
Page 1 of 13
CS4281 Internet Application Development
Anthony, Chan Ho Kwing 96116563
Dennis, Yu Hon Fai 96096615
Marius, Yung Ka Ho 96103600
Objectives
The objectives of this assignment are to implement a pinball machine in Java and use the object
oriented system development approach to develop the system so as to enhance the knowledge of
Java programming as well as game programming.
Page 2 of 13
CS4281 Internet Application Development
Anthony, Chan Ho Kwing 96116563
Dennis, Yu Hon Fai 96096615
Marius, Yung Ka Ho 96103600
Game Features
Double buffered smooth animation
The game has used double buffer to eliminate flicker so that smooth animation has achieved.
Collision Detection
Relatively precise collision detection has been developed including ball to ball collision, ball to line
collision and ball to paddle collision.
Background Animation
A background animation that simulates what the pilot see in a space ship flying through space has
been implemented and therefore the player can see many stars in the game. The brightness of the
stars are also calculated depending on the distance form the viewer, therefore far star will be
dimmer and close star will be brighter.
Obstacles
Different obstacles have been added to the game, for example, the paddle and blob obstacles. They
will behave differently when the pinball collided with them. For example marks will be given to the
player when the blob is collided by the pinball.
Clock
A clock counting how long the player has played is implemented and displayed in the game.
Page 3 of 13
CS4281 Internet Application Development
Anthony, Chan Ho Kwing 96116563
Dennis, Yu Hon Fai 96096615
Marius, Yung Ka Ho 96103600
System Design and Architecture
The system developed has followed discipline in object oriented system development, for example,
polymorphism and inheritance have been used extensively and have made the the development of
the system easier.
Game Loop
In each discrete time frame of the game, we move the ball and detect any collision occurred and we
draw the game. In pseudo code:
MoveObjects();
CollisionDetection();
CollisionResponse();
DrawTheGame();
The Collider class and its’ subclasses
Collider
score
draw()
collided()
boundOff()
response()
BoundBox
Blob
DownBound
BigBlob
The Collider class and its subclasses
Page 4 of 13
Paddle
CS4281 Internet Application Development
Anthony, Chan Ho Kwing 96116563
Dennis, Yu Hon Fai 96096615
Marius, Yung Ka Ho 96103600
All the objects that will collide with the pinball including the obstacles and the paddles are
subclasses of the Collider class.
abstract class Collider
{
int score = 0;
void
response() { };
abstract boolean collided(PinBall ball);
abstract boolean boundOff(PinBall ball);
abstract void draw(Graphics g);
};
The abstract Collider class
The inheritance used here allow us to use a for loop to perform collision detection of the PinBall to
any kind of Collider and this really make life easier.
for(int j=0;j<colliders.size();j++)
{
Collider collider = (Collider)colliders.elementAt(j);
if( collider.collided(theBall) )
{
collider.boundOff(theBall);
collider.response();
score += collider.score;
break;
}
}
And the polymorphism mechanism allows the colliding object to behave differently when collided
with the pinball. Also, this approach also allow the programmer to add a new instance of colliding
objects easy, for example, to add a new Blob instance, what the programmer need to do is the
following code:
Blob blob = new Blob(applet,80,80,30,ballImage,hitImage,scoreImage);
colliders.addElement(blob);
By writing just 2 lines of code, a blob instance is added and the collision detection and response of
the blob to the pinball and the drawing of the blob are handled automatically.
Page 5 of 13
CS4281 Internet Application Development
Anthony, Chan Ho Kwing 96116563
Dennis, Yu Hon Fai 96096615
Marius, Yung Ka Ho 96103600
The Background class and its’ subclasses
Background
draw()
step()
StarBackground
ScrollBackground
The Background class and its subclasses
The StarBackground and the ScrollBackground are subclasses of the abstract Background class.
This architecture has many benefits, first it allows us to swap the background animation easily by
writing:
background = starBackground;
or
background = scrollBackground;
Secondly, new type of background animation can be easily created by deriving the Background
class and implements the step() and draw(Graphics g) methods. For example, if the developer want
to create a fire background that simulate the fire and use this fire background in the program, what
he need is to do is extending the Background class and override the draw() method and step()
method.
Page 6 of 13
CS4281 Internet Application Development
Anthony, Chan Ho Kwing 96116563
Dennis, Yu Hon Fai 96096615
Marius, Yung Ka Ho 96103600
Collision Detection
Collision between obstacles and the pinball are detected in each discrete frame. Including PinBall to
Blob collision (circle to circle), Pinball to Paddle collision (circle to line segment) and others.
Since collisions are detected in each discrete time frame, there are several problems that may arise.
Problem 1: moving too fast
If the pinball moving to fast, it may penetrate the obstacle in 2 successive time frame and this make
the detection of the collision fail.
Pinball at t1
Pinball at t2
Circle to circle collision detection
Circle to circle collision detection is easy, we just calculate the distance of the two circles and see
whether this is smaller then the sum of the radii of the circles, the distance is smaller, we know that
collision has occurred.
i.e. Distance(C1, C2) <= R1+R2 implies two ball collided
Circle to line segment collision detection
Assuming the line segment is defined as its’ two end points P1(x1,y1) and P2(x2,y2). And the circle
C(xc,yc,r) where x, y is the coordinates of the center and r is the radius.
We show below how the collision between PinBall and the DownBound is detected in the game. A
DownBound object prevents the PinBall from moving down from it.
Page 7 of 13
CS4281 Internet Application Development
Anthony, Chan Ho Kwing 96116563
Dennis, Yu Hon Fai 96096615
Marius, Yung Ka Ho 96103600
I
A DownBound object
P
First we parameterize the line
x = x1+(x2-x1)*t
y = y1+(y2-y1)*t
To see whether a point P is inside the region of the BoundDown object, we first calculate the
interception point I by solving:
x = x1+(Ix-x1)*t
y = y1+(Iy-y1)*t
Since I and P have the same x-coordinate, therefore
x = x1+(Px-x1)*t
y = y1+(Iy-y1)*t
Solving for Iy, we have Iy = y1+(y2-y1)*(Px-x1)/(x2-x1);
Then we compare Iy with Py and if Iy is smaller than Py, we know that the point P is inside the
region of the BoundDown object.
Now we can use a bounding box to represent the PinBall and test whether each of the four corner of
the bounding box is inside the BoundDown region.
Page 8 of 13
CS4281 Internet Application Development
Anthony, Chan Ho Kwing 96116563
Dennis, Yu Hon Fai 96096615
Marius, Yung Ka Ho 96103600
Interface Design and User Manual
Interface Design
The user interface is designed to be easy to use and attractive so that the users can perform all the
operations they want to do and also develop an immerse feeling towards the game.
The following screen shots show different states of the game.
Main menu of the pinball machine
Page 9 of 13
CS4281 Internet Application Development
Anthony, Chan Ho Kwing 96116563
Dennis, Yu Hon Fai 96096615
Marius, Yung Ka Ho 96103600
Playing the pinball machine
Game paused!
Page 10 of 13
CS4281 Internet Application Development
Anthony, Chan Ho Kwing 96116563
Dennis, Yu Hon Fai 96096615
Marius, Yung Ka Ho 96103600
Game Over
User Manual
The following table lists all the operations that the users can perform.
Operations
To Start the game
To launch the ball
To move the left Paddle
To move the right paddle
To pause or un-pause the game
Actions
Press the start game button
Use the mouse to drag the launcher and release
the mouse
Press the key "z" or "Z"
Press the key "/"
Press the key "p" or "P"
Page 11 of 13
CS4281 Internet Application Development
Anthony, Chan Ho Kwing 96116563
Dennis, Yu Hon Fai 96096615
Marius, Yung Ka Ho 96103600
Conclusion
What we have learned
Many features in Java language have been investigated, for example, inheritance, polymorphism,
interface and Java’s thread programming are all covered in the game. Inheritance and polymorphism
have been used extensively and the approach has helped to simplify the system and also allow the
system to be easily modified, extended and maintained. Besides, game programming skills have
been studied and this includes how to use a game thread to run a game, how collisions are detected,
how the pinball is moved and accelerated due to gravity and how double buffer is used to eliminate
flicker.
The assignment has given us a wonderful opportunity to dig into Java programming and also to
have much fun time on the process of developing a computer game.
Contributions
Graphics designs and report writing: Anthony, Chan Ho Kwing 96116563
Programming and report writing: Dennis, Yu Hon Fai 96096615
Information gathering, interface design and report writing: Marius, Yung Ka Ho 96103600
Page 12 of 13
CS4281 Internet Application Development
Anthony, Chan Ho Kwing 96116563
Dennis, Yu Hon Fai 96096615
Marius, Yung Ka Ho 96103600
File Listing
Classes
Note: The classes with indents are subclasses of the class above it.
Vector2D.java --- The maths class use to manipulate vector
Collider.java --- The super class of all classes whose instances will collide with the PinBall

DownBound.java --- prevents the PinBall from moving down from it.

Paddle.java --- paddles

BoundBox.java --- Rectangular region that the pinball can NOT penetrate

Blob.java -- The circular Blob
o
BigBlob.java --- The Big Circular Blob
Background.java --- Super class of all Background

StarBackground.java --- Flying through space background

ScrollBackground.java --- Scrolling Background
Sprite.java --- The super class of all sprite classes that represent the sprite used in the game

Banner.java --- The Banner used to display the score and time played
PinBall.java --- The pinball class
World.java --- Defining the gravity and provide utility functions to load images
Star.java --- The star use in the background
MMAnimation.java --- The classes used to perform the animation in the main menu
ScoreBox.java --- score that animates once the pinball hit the blob
Launcher.java --- Launcher of the pinball
RollButton.java --- The rollover button
Interfaces
Loopable.java -- The Loopable interface
Page 13 of 13
Download