arkanoid game

20

Click here to load reader

Upload: graphitech

Post on 11-May-2015

1.930 views

Category:

Education


2 download

DESCRIPTION

This project is aimed at developing an Arkanoid game also called as Ball and Bricks game using JOGL.

TRANSCRIPT

Page 1: Arkanoid Game

Arkanoid Game

CCCComputeromputeromputeromputer Graphics Graphics Graphics Graphics

JOGL JOGL JOGL JOGL Implementation ProjectImplementation ProjectImplementation ProjectImplementation Project

Submitted BySubmitted BySubmitted BySubmitted By

Anuradha JambunathanAnuradha JambunathanAnuradha JambunathanAnuradha Jambunathan

Matricola: 128682Matricola: 128682Matricola: 128682Matricola: 128682

12121212 ---- 02020202 ---- 2008200820082008

Page 2: Arkanoid Game

Table of Contents

1 Introduction...........................................................................................3

1.1 OpenGL .................................................................................................3

1.2 JOGL......................................................................................................4

1.3 Open GL Commands............................................................................5 1.3.1 Color Manipulation Commands .......................................................5 1.3.2 Drawing and Matrix Manipulation Commands ...............................5 1.3.3 Stack Manipulation Commands.......................................................6 1.3.4 Texture manipulation Commands ...................................................6 1.3.5 Viewing manipulation commands ...................................................7

2 Requirements........................................................................................8

3 Design Analysis..................................................................................10

3.1 Classes and Methods.........................................................................10

3.2 Class Diagram.....................................................................................15

4 Implementation ...................................................................................16

4.1 Collision detection Algorithm............................................................16

4.2 Additional Features ............................................................................17

4.3 Software Details..................................................................................18

5 Conclusion & Future Work ................................................................18

6 References ..........................................................................................19

7 Appendix .............................................................................................20

7.1 Game Screenshot ...............................................................................20

Page 3: Arkanoid Game

1 Introduction

Computer games are considered to be the fast growing and more

interesting field of study today. The interactive entertainment field makes use of

the latest computer technologies and helps for the development of research in

areas such as computer simulation, computer aided designing, virtual reality,

artificial intelligence etc. Development of games is considered to be the more

creative way of software development. Game development requires more of

innovative thinking and ideas to work in the imaginary world.

OpenGL (Open Graphics Library) is a standard specification which is used

to write software application to create 2D and 3D graphics. Open GL is a platform

independent API which makes it easy to use and has good performance

capability. The Open GL utility library (GLU) provides sophisticated modeling

features which are not available in standard Open GL.

Java Open GL (JOGL) is a library that’s helps to use the OpenGL in java

programming language. It is also easy to program using JOGL with a good

programming knowledge of JAVA. This project is developed using JOGL.

This project is aimed at developing an Arkanoid game also called as Ball

and Bricks game using JOGL. All the inbuilt methods available in Open GL are

not used and the own methods are implemented to demonstrate the same

functionality.

This game is made very simple and interactive for use. The UI designed

makes use of all the features of Open GL learnt during the course. The game is

designed as a 2D game area embedded in a 3D world to give a feel of playing in

3D world to the user.

1.1 OpenGL

OpenGL was developed by Silicon Graphics Inc. (SGI) in 1992. It is a

software interface to graphics hardware. It is a standard specification which

contains of nearly 150 commands that is used to specify the primitives or objects

and create operations that are required to produce 3D applications. It is a

Page 4: Arkanoid Game

hardware independent interface that can be implemented in any hardware

platform.

It has got two main advantages, first to hide the complexity of the

interacting hardware device by providing a single uniform API for the programmer

and secondly it is easy to use and provides a good performance and also focus

on rendering.

Open GL does not have high level complicated commands for creating 3D

objects, whereas these features are provided by the library built top on Open GL.

Open GL utility library (GLU) is used to create complex objects like sphere,

cylinder , quadric surfaces etc.

GL utility library (GLU) contains routines which make use of the Open GL

low level commands for setting up matrices for viewing and specific

transformations etc. Every window system has a library that extends the

functionality of the window system to support the OpenGL rendering.

GLUT which is the OpenGL utility toolkit is a library which is used to

simplify the interaction with the windowing system. GLUT is used to write Open

GL programs without taking care about the windowing system. GLUT provides

following simplified functionalities like opening a window, getting input from

mouse or keyboard, menus etc.

1.2 JOGL

Java OpenGL (JOGL) is a library that helps to use Open GL in java

programming language. It provides full access to Open GL 2.0 and also it

integrates with swing and AWT.

It is used to create hardware supported 3D graphics to the applications

that are written in Java. It is easy because anyone who has a good knowledge in

java can do the program very easily. In this project, the game is developed using

JOGL.

Page 5: Arkanoid Game

1.3 Open GL Commands

There are nearly 150 commands present in Open GL. In this section we

discuss some of the important commands and the commands which are used in

this project

1.3.1 Color Manipulation Commands

glClearColor – This specifies the clear values for the color buffers. Values given

are between the range 0 and 1.

glColor3f – This specifies the red, green and blue color float explicitly and sets

the current alpha intensity value to 1.

1.3.2 Drawing and Matrix Manipulation Commands

glBegin, glEnd – This command is used as a delimiter of the vertices used to

draw a primitive. All the vertices used for drawing a primitive are used inside this

enclosing block.

glVertex – This command is used within the glBegin and glEnd block to specify

the points used to draw the desired primitive. The current color, textures are

associated with the vertex when this command is called.

glLoadIdentity – This command is used to load the identity matrix by replacing

the current matrix in the stack.

glMatrixMode – This command specifies which matrix is the currently used one.

The mode can take three values which can be one of the following:

a. GL_MODELVIEW - This is used to position the view in the scene.

Used for 3D modeling, all the operations done are applied on the

modelview matrix stack.

Page 6: Arkanoid Game

b. GL_PROJECTION – This defines the way the 3D scene is

projected as 2D scene. All the operations done are applied on the

projection matrix stack.

c. GL_TEXTURE – Applies all the operations on the texture matrix

stack.

glTranslate – This command is used to translate the current position with the

given x, y and z values. The current matrix which is specified previously in the

glMatrixMode is multiplied by the translation matrix. The multiplied result replaces

the current matrix value.

glRotate – This command is used to rotate for the given rotation angle around

the vector x, y and z. Similarly the current matrix which is specified previously in

the glMatrixMode is multiplied by the rotation matrix. The multiplied result

replaces the current matrix value.

glScale - This command is used to produce a desired scaling factor along the x,

y and z axes. Similarly the current matrix which is specified previously in the

glMatrixMode is multiplied by the scaling matrix. The multiplied result replaces

the current matrix value.

1.3.3 Stack Manipulation Commands

glPushMatrix, glPopMatrix – This command is used to push and pop the

current matrix stack used.

1.3.4 Texture manipulation Commands

glGenTextures - This command is used to generate texture names with takes as

parameter the number of texture to be generated and the array where the

generated texture names are stored.

Page 7: Arkanoid Game

glBindTexture – This command is used to bind the name of the texture to the

texture target.

glTexParameteri – This command assigns the values to the texture parameter

specified. The target defines the corresponding texture target.

glTexImage2D – This command is used to specify a 2D texture image

glTexCoord – This command is used to specify the texture coordinates in 1D,

2D or 3D. The glTextCoord2d used in this project specifies the S-Tangent and T-

tangent vales of the texture used.

1.3.5 Viewing manipulation commands

glViewport – This command is used to set the view port area by specifying the

lower left corner pixel points of the rectangle, height and width of the rectangle

area.

gluOrtho2D – This command specifies the 2D projection matrix and sets up the

2D orthographic viewing region. It specifies the left and right vertical clipping

planes and bottom, top horizontal clipping planes.

gluPerspective – This command specifies the view coordinate system into world

coordinate system. The aspect ratio should match the view port aspect ratio in

order to see the image without any distortion.

Page 8: Arkanoid Game

2 Requirements

This project is aimed at developing an interactive “Arkanoid” game also

called as “Ball and Bricks” game using JOGL. The game interface should be

easy to use by the user and provide a good visual presentation for attracting the

player.

Let us look into the requirements for creating this Arkanoid game

1. The Game developed should be an interactive Arkanoid play.

2. The Arkanoid game developed should have a paddle which helps to

hold the ball.

3. The ball should bounce inside the play area and target to break the

bricks.

4. The ball is allowed only to move inside the play area boundary.

5. Once there is a collision of the ball with the bricks or the play area

boundary or the paddle the ball has to bounce in the corresponding

direction.

6. The Direction of the ball motion must be calculated based on the

direction of the collision.

7. Once the ball hits the target bricks, then the brick should be broken or

destroyed.

8. The game score should be shown based on the number of bricks

destroyed by the user.

9. Each time the ball is dropped or missed then the lives should be

decreased.

10. The maximum number of lives for the player is given as three.

11. The game should be over if all the lives are gone and still there are

some bricks which are not broken.

12. Player should win the game if he breaks all the bricks before all the

lives are finished.

13. Bonus should be given to the player is the ball hits the selected brick

marked in a different color.

Page 9: Arkanoid Game

14. The Bonus brick should marked in blue color.

15. When the bonus brick is hit by the ball, five bricks adjacent to the

bonus brick should be broken and the score should be increased.

16. The Arkanoid play area should a 2D plane embedded in a 3D

environment.

17. The Game should be played using keyboard control or using mouse as

desired by the player.

18. The User interface should be made more attractive by giving texture

for play area, ball and bricks.

19. There should a combined swing frame which displays the current

score, lives to the user.

20. There should be also some extra features available to change the

background color, rotate the play board after the game etc.

21. The inbuilt Open GL functions like glTranslate; glRotate etc should not

be used. Own matrix operations should be implemented to

demonstrate the above functionalities.

22. The Game should be developed using JOGL.

The game was developed based on the above requirements.

Page 10: Arkanoid Game

3 Design Analysis

In this section, we discuss the object-oriented approach followed in the

designing of the game, the actual algorithm used to detect collisions,

assumptions and limitations of the game. This game is developed using JOGL

and uses Java Swing technology for presentation.

3.1 Classes and Methods

The various classes and important methods used are discussed below.

Class – BouncingApplication

This Class contains the main method. Used for startup of the application. This

class contains all the swing components used for this game. The Registration of

the GLEventListener, KeyListener, and MousemotionListener is done here.

Methods Description

main() The main method is invoked by JVM and

it instantiates the class (Bouncing

Application) and opens the frame

(JFrame) and registers the event

handlers. Creates the canvas for the

renderer display and creates the visual

components.

Class – BouncingBallRenderer

This class extends the GLEventListener, ActionListener and MenuListener. This

contains all the graphics related methods and the key algorithm used for collision

detection and displaying the graphics.

Methods Description

init(GLAutoDrawable gLDrawable) This method is called the first time and it

Page 11: Arkanoid Game

initializes the graphics display. The

matrixes are set to identity and all

textures are created here. This method

calls the display

display(GLAutoDrawable

gLDrawable)

This method is used for rendering the

graphics. This method is called repeated

by using an Animator. This methods

contains all the important methods for

displaying the game and contains the

algorithm used

reshape(GLAutoDrawable

gLDrawable, int x, int y, int

width, int height)

This method is called when the window is

resized

drawBoundaryBox(GL gl) This method is used to draw the outer

boundary box of the play area.

drawBricks(GL gl) This method is used to draw or display

the target bricks to break.

checkGameOver() This method is used to check if the game

is over. It can be the case if all the bricks

are broken or all the lives are finished.

updatePlatePosition(GL gl) This method is called to track the new

position of the plate moved by the player

and draw the new position. This is

accomplished by performing the

operations on the Plate Matrix Stack

drawBallOnPlate(GL gl) This method is used to draw the ball on

the plate

updateBallPosition() This method is used to calculate the

movement of the ball. It increments the x

and y value for the movement based on

the direction of the bouncing ball. This

Page 12: Arkanoid Game

method contains the logic for the collision

detection.

calculateStackResult(boolean

plate)

This method is used to calculate the

result of the Plate Matrix stack and Ball

Matrix stack based on the Boolean value

passed. True for Plate and False for ball.

breakBrick(int i) This method is used to break the brick.

The parameter value identifies which

brick to break.

moveCamera(double xvalue, double

yvalue,double zvalue,boolean

isPlate)

This method is used to perform the matrix

operation. The push operation of the

corresponding matrix stack is done here

whenever there is a movement in the ball

or the plate.

keyPadMovement() This method contains the movement for

the key press

mouseMovement() This method contains the movement

update when mouse control is used.

actionPerformed(ActionEvent e) This method is invoked when a button

click is done. When the play button, color

button is clicked etc.

menuSelected(MenuEvent arg0) This method checks the menu (Game

control) selected by the user.

Class – KeyNavigator

This class extends KeyAdapter class. This class is invoked whenever a key is

pressed by the user.

Methods Description

keyPressed(KeyEvent e) The method is invoked when any key is

pressed.

Page 13: Arkanoid Game

keyReleased(KeyEvent e) The method is invoked when any key is

released.

processMovement(KeyEvent ,boolean

commandIsActive)

This method sets the movement variable

according to the key pressed.

Class – MouseNavigator

This class implements the MouseListener, MouseMotionListenerclass. This class

is invoked whenever a mouse movement is performed by the user.

Methods Description

mouseClicked(MouseEvent arg0) The method is invoked when the mouse

is clicked.

mouseMoved(MouseEvent arg0) The method is invoked when mouse is

moved.

Class – MatrixStack

This class contains the matrix operations. 2 Matrix stacks are created one for the

plate and one for the ball.

Methods Description

push(double[][] matrix) The method is invoked when push

operation is performed on the matrix

stack

pop() The method is invoked when pop

operation is performed on the matrix

stack

flushStack() This method clears the matrix stack.

Page 14: Arkanoid Game

Class – NGonBall

This class is used to create a Ngon Ball used for the Game

Methods Description

drawNGon(double x, double y) The method is used to generate the

tuples for the Ngon Ball.

There are some more classes used for applying texture like BitmapLoader for

loading the bmp file, ResourceRetriever, TextureLoader. Not all the methods for

these classes are mentioned above.

Description about all the methods and classes can be retrieved from the java doc

presented in the project.

Page 15: Arkanoid Game

3.2 Class Diagram

The below figure shows the list of classes used in this project and the

dependencies between the classes.

Fig.1

Page 16: Arkanoid Game

4 Implementation

In this section the algorithm used for the game is discussed. The main

algorithm consists of the collision detection of the ball with the boundary, bricks

and the plate.

Later in the appendix section the screenshot of the game is shown

4.1 Collision detection Algorithm

1. The ball’s position is updated by incrementing/decrementing the ‘x’ value

and incrementing/decrementing the ‘y’ value.

2. For every display call

// updating with ‘x’ value

a. If ball’s minimum ‘x’ bound equals the left boundary of the play area

i. Increment ‘x’ by 0.1

Else

ii. Decrement ‘x’ by 0.1

b. If ball’s maximum ‘x’ bound equals the right boundary of the play area

i. Decrement ‘x’ by 0.1

Else

ii. Increment ‘x’ by 0.1

c. If ball’s minimum ‘y’ bound equals the top of the plate and if the ‘x’

value of the ball lies within the ‘x’ bounds of the plate

i. Increment ‘y’ by 0.1 and increment/decrement ‘x’ based on the

place of coincidence of the ball.

Else

ii. Decrement ‘Y’ by 0.1 and the ball is missed. So, lives count is

reduced by 1.

Page 17: Arkanoid Game

//To detect collision

d. For each brick

i. If the brick has not been collided

1. If the midpoint of the ball lies within the ‘x’ and ‘y’

bounds brick vertices,

a. Ball collided the brick

b. Increment/decrement ‘x’ and ‘y’ values according

to the movement of the ball i.e. based on the

previous values of the ‘x’ and ‘y’

2. If the top of the ball touches the bottom of the brick

side,

a. Ball collided the brick

b. Increment/decrement ‘x’ and decrement ‘y’

values according to the movement of the ball i.e.

based on the previous values of the ‘x’ and ‘y’

3. If the max/min ‘x’ of the ball touches the sides of the

brick,

a. Ball collided the brick

b. Increment/decrement ‘x’ and decrement ‘y’

values according to the movement of the ball i.e.

based on the previous values of the ‘x’ and ‘y’

4.2 Additional Features

• Three lives are provided for the user

• Game can be played using either keyboard or mouse

• Bonus points are awarded to the user if he breaks the bricks

in blue color.

Page 18: Arkanoid Game

• When the Blue bricks are hit, the five bricks adjacent to it are

broken as bonus.

• Another type of bonus is given, when a green brick is broken

the ball attaches to the plate automatically till the next life is

lost. This feature allows the user to target on more bricks

and increase the score

4.3 Software Details

Development IDE Eclipse Version: 3.2.2

Operating System Windows XP

Other Technologies Used Java Swing

Library Used JOGL 1.1.0

Programming language JAVA

Important Jar files Used for JOGL jogl.jar

gluegen-rt.jar

5 Conclusion & Future Work

Hence in this project an interactive Arkanoid game using JOGL has

been implemented. It also incorporates all the features learnt during the course

for example applying Texture, Ngon object, transformation matrices etc. Hence

this project was a very good start for developing good video games with 2D and

3D features

The following are the possible extensions to the game

• Can have levels with various difficulty like Beginner, Intermediate,

Expert etc.

• Can be extended as a multi player game

Page 19: Arkanoid Game

6 References

1. “OpenGL RedBook – OpenGL programming Guide “, Dave Astle,Kelvin

Hawkins

2. Tutorial for applying Texture

http://www.paulsprojects.net/tutorials/simplebump/simplebump.html

3. Open GL command Reference

http://www.ncrg.aston.ac.uk/~cornfosd/graphics/opengl/openglman.html

4. Course Lecture slides

Page 20: Arkanoid Game

7 Appendix

7.1 Game Screenshot