the quantum avenger an application of object oriented design principles in game development andres...

1
The Quantum Avenger An Application of Object Oriented Design Principles in Game Development Andres Calderon Jaramillo - Dr. Hong K. Sung (Faculty Advisor) This project was developed as the final assignment for an undergraduate course in Object Oriented Programming at the University of Central Oklahoma. Students were instructed to design an animated game and implement it in the Java programming language. There were two primary constraints: first, at least two design patterns were to be used; second, the game design needed to follow a generalized data- animator-renderer framework provided by the instructor. BACKGROUND 1 [1] “2-Dimensional Elastic Collisions Without Trigonometry.” http://www.imada.sdu.dk/~rolf/Edu/DM815/E10/2dcollis ions.pdf. Accessed: March 15, 2014. Game development is an area in which object-oriented programming principles can be applied quite naturally. Game entities can be readily represented as objects, and relationships between entities can be modeled using associations, inheritance, polymorphism, and design patterns. A game, The Quantum Avenger, was designed to meet the assignment's requirements. In essence, the game consists of a number of objects that can be shot by a ship controlled by the player through the keyboard. Some of the basic rules include: The player selects among three levels of difficulty. At the beginning of a session, there is a fixed number of spherical objects on the screen. These objects may be electrically charged and must interact with each other accordingly: positive spheres are red, negative spheres are blue, and neutral spheres are white. The player provides acceleration and direction to the ship (an equilateral triangle) using the keyboard. There are some restrictions on the ship’s velocity to prevent it from speeding out of control. The player makes the ship fire missiles using the keyboard in order to destroy spherical objects. The ship may be electrically charged (as indicated by the color of the tip). It can affect the motion of spherical objects. Spherical objects do not exert action-at-a-distance forces on the ship. Every collision of the ship with an spherical object decreases the ship’s health. The player wins when all objects have been destroyed. The player loses when the ship’s health reaches zero. METHODS 2 REFERENCES 4 Design Patterns At least two design patterns were used in this project: Strategy pattern: This pattern is used to make the different game screens (main menu, game area, losing screen, winning screen, etc.) interchangeable while allowing each one to encapsulate specific behaviors: In another scenario, this pattern is used to centrally handle some events generated within each game screen: Observer pattern: In one scenario, this pattern is used to notify the main data structure of the disappearance of an object from the screen (a spherical object being destroyed or a missile crossing the boundaries of the game area): CONCLUSION 3 A satisfactory solution to the final assignment was provided. It successfully illustrated the application of object-oriented design principles to the development of a game that used non-trivial simulations of physical phenomena. Basic Framework The framework provided by the instructor gave students a general starting point for the game design. There were three components used in this framework: Data structure (GameData in figure): an object that stores the entities in the game. Animator: an object that uses a separate thread to periodically update the state of the game entities. Renderer (GamePanel in figure): an object that displays the game entities in their current state. Collision Detection Collisions between spherical objects are detected using their radii. More formally, two spherical objects have collided if and only if the distance between their centers is less than or equal to the sum of their radii. In order to detect a collision between the ship and a spherical object, the boundary of the ship is approximated using a circle. Its center is the circumcenter of the triangle. The radius is calculated as follows: 2 radiu incircle radius le circumcirc r C Using this circle, collisions are detected in the same manner as with spherical objects. Collisions with walls and missiles are detected in a similar fashion. Collision Handling Collisions between spherical objects must be handled using the laws of physics, including the conservation of momentum. Collisions are assumed to be elastic. The method used to calculate the speed and direction of each object after the collision is explained in detail in [1]. Actions at a Distance Because the ship and the spherical objects may be electrically charged, their interactions must be simulated. A formula analogous to Coulomb’s Law is used to calculate the force between two charged bodies: r F ˆ distance charge charge 3 2 1 The acceleration and velocity are calculated from this force using methods from classical mechanics. Spherical Object (Negative) Spherical Object (Positive) Spherical Object (Neutral) Ship (Blue tip - negative) Missile Animator GamePanel GameData High-Level Framework’s UML Class Diagram r R d Circumcircle Incircle Collision Approximation Circle known v i 1 known v i 2 ? f 1 v ? f 2 v R r d Object for which force is being calculated r ˆ F distan 2 charge 1 charg F JPanel IntroductionPanel GamePanel ResultScreen Main GameFigure Particle FreeParticle Missile << interface >> Disappearable << interface >> DisappearanceListener GameData 1..* DisappearanceEvent JPanel GamePanel ResultScreen << interface >> Screen << interface >> ScreenListener Main ScreenEvent IntroductionPan el Circumcenter

Upload: suzan-norton

Post on 18-Jan-2018

223 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: The Quantum Avenger An Application of Object Oriented Design Principles in Game Development Andres Calderon Jaramillo - Dr. Hong K. Sung (Faculty Advisor)

The Quantum AvengerAn Application of Object Oriented Design Principles in Game DevelopmentAndres Calderon Jaramillo - Dr. Hong K. Sung (Faculty Advisor)

This project was developed as the final assignment for an undergraduate course in Object Oriented Programming at the University of Central Oklahoma. Students were instructed to design an animated game and implement it in the Java programming language. There were two primary constraints: first, at least two design patterns were to be used; second, the game design needed to follow a generalized data-animator-renderer framework provided by the instructor.

BACKGROUND1

[1] “2-Dimensional Elastic Collisions Without Trigonometry.” http://www.imada.sdu.dk/~rolf/Edu/DM815/E10/2dcollisions.pdf. Accessed: March 15, 2014.

Game development is an area in which object-oriented programming principles can be applied quite naturally. Game entities can be readily represented as objects, and relationships between entities can be modeled using associations, inheritance, polymorphism, and design patterns.

A game, The Quantum Avenger, was designed to meet the assignment's requirements. In essence, the game consists of a number of objects that can be shot by a ship controlled by the player through the keyboard. Some of the basic rules include:

• The player selects among three levels of difficulty.

• At the beginning of a session, there is a fixed number of spherical objects on the screen. These objects may be electrically charged and must interact with each other accordingly: positive spheres are red, negative spheres are blue, and neutral spheres are white.

• The player provides acceleration and direction to the ship (an equilateral triangle) using the keyboard. There are some restrictions on the ship’s velocity to prevent it from speeding out of control.

• The player makes the ship fire missiles using the keyboard in order to destroy spherical objects.

• The ship may be electrically charged (as indicated by the color of the tip). It can affect the motion of spherical objects. Spherical objects do not exert action-at-a-distance forces on the ship.

• Every collision of the ship with an spherical object decreases the ship’s health.

• The player wins when all objects have been destroyed. The player loses when the ship’s health reaches zero.

METHODS2

REFERENCES4

Design PatternsAt least two design patterns were used in this project:

Strategy pattern:

This pattern is used to make the different game screens (main menu, game area, losing screen, winning screen, etc.) interchangeable while allowing each one to encapsulate specific behaviors:

In another scenario, this pattern is used to centrally handle some events generated within each game screen:

Observer pattern:

In one scenario, this pattern is used to notify the main data structure of the disappearance of an object from the screen (a spherical object being destroyed or a missile crossing the boundaries of the game area):

CONCLUSION3

A satisfactory solution to the final assignment was provided. It successfully illustrated the application of object-oriented design principles to the development of a game that used non-trivial simulations of physical phenomena.

Basic FrameworkThe framework provided by the instructor gave students a general starting point for the game design. There were three components used in this framework:

• Data structure (GameData in figure): an object that stores the entities in the game.

• Animator: an object that uses a separate thread to periodically update the state of the game entities.

• Renderer (GamePanel in figure): an object that displays the game entities in their current state.

Collision DetectionCollisions between spherical objects are detected using their radii. More formally, two spherical objects have collided if and only if the distance between their centers is less than or equal to the sum of their radii.

In order to detect a collision between the ship and a spherical object, the boundary of the ship is approximated using a circle. Its center is the circumcenter of the triangle. The radius is calculated as follows:

2radiusincircleradiuslecircumcirc

rC

Using this circle, collisions are detected in the same manner as with spherical objects. Collisions with walls and missiles are detected in a similar fashion.

Collision HandlingCollisions between spherical objects must be handled using the laws of physics, including the conservation of momentum. Collisions are assumed to be elastic. The method used to calculate the speed and direction of each object after the collision is explained in detail in [1].

Actions at a DistanceBecause the ship and the spherical objects may be electrically charged, their interactions must be simulated. A formula analogous to Coulomb’s Law is used to calculate the force between two charged bodies:

rF ˆdistance

chargecharge3

21

The acceleration and velocity are calculated from this force using methods from classical mechanics.

Spherical Object(Negative)

Spherical Object(Positive)

Spherical Object(Neutral)

Ship(Blue tip - negative)

Missile

Animator

GamePanel

GameData

High-Level Framework’s UML Class Diagram

rRd

Circumcircle

IncircleCollision Approximation Circle

knownv i 1

knownv i 2

?f1 v

?f2 v

R rd

Object for which force is being calculated

F distance

2charge

1charge

F

JPanel

IntroductionPanel

GamePanel

ResultScreen

Main

GameFigure

Particle

FreeParticle

Missile

<< interface >>Disappearable

<< interface >>DisappearanceListener

GameData1..*

DisappearanceEvent

JPanel

GamePanel

ResultScreen

<< interface >>Screen

<< interface >>ScreenListener

Main

ScreenEvent

IntroductionPanel

Circumcenter