54

3D Shooting Game

Embed Size (px)

Citation preview

Page 1: 3D Shooting Game

8/7/2019 3D Shooting Game

http://slidepdf.com/reader/full/3d-shooting-game 1/54

Page 2: 3D Shooting Game

8/7/2019 3D Shooting Game

http://slidepdf.com/reader/full/3d-shooting-game 2/54

Overview• Our Goal

• Why make a game• Thanks Giving to “OpenGL Game

”• Storyline

• Design

• Problems

• Screenshots

Page 3: 3D Shooting Game

8/7/2019 3D Shooting Game

http://slidepdf.com/reader/full/3d-shooting-game 3/54

Our GoalThe main goal of our project was to make a 3D First Person Shooter Game.Although there are lots of such games already in the market. We wished tomake it our own way. Initially When we opted for our project we knew onlyabout how to make a Room. But later Studying various Books and Websiteswe got the ideas to load an MD2 Model which would be the monster inour ame. We could have desi ned our own MD2 Model. But due to lack of

time, We used the Models that were developed for Quake2 game.Our Game World Consists of Various Rooms. These Rooms have a Slidingdoor which gets automatically opened when the player is near it, and getsclosed when player moves away from it.Our Project is implemented in C++/OpenGL. C++ takes care of the BackendPartOpenGL takes care of the Frontend part.

Page 4: 3D Shooting Game

8/7/2019 3D Shooting Game

http://slidepdf.com/reader/full/3d-shooting-game 4/54

Why Make a Game?• Something We always wanted to do

• More fun than previous projects• Graphics programming

• Uses concepts learned in CS courses

Page 5: 3D Shooting Game

8/7/2019 3D Shooting Game

http://slidepdf.com/reader/full/3d-shooting-game 5/54

Thanks to

“OpenGL Game Programming” for It’sInvaluable Ideas for our Project

Page 6: 3D Shooting Game

8/7/2019 3D Shooting Game

http://slidepdf.com/reader/full/3d-shooting-game 6/54

StorylineYou’ve been trapped inside a Monster

House. You Got to Kill all the Sodmonsters of Level 1 and get into theLevel 2. The Level 2 brin s ou to attackwith Ogro Monsters.

That’s It! You are set free from

the Monster house: GAME OVER

Page 7: 3D Shooting Game

8/7/2019 3D Shooting Game

http://slidepdf.com/reader/full/3d-shooting-game 7/54

DesignData Structures/Algorithms:

• Our Game Engine• Trees

• CObject and Collision Detection• Game Engine Core

• Artificial Intelligence

• Game Mode

Page 8: 3D Shooting Game

8/7/2019 3D Shooting Game

http://slidepdf.com/reader/full/3d-shooting-game 8/54

Our Game Engine

Page 9: 3D Shooting Game

8/7/2019 3D Shooting Game

http://slidepdf.com/reader/full/3d-shooting-game 9/54

Our Game Engine’s Class Design

Page 10: 3D Shooting Game

8/7/2019 3D Shooting Game

http://slidepdf.com/reader/full/3d-shooting-game 10/54

Example of Game World Architecture

Page 11: 3D Shooting Game

8/7/2019 3D Shooting Game

http://slidepdf.com/reader/full/3d-shooting-game 11/54

Tree Data StructureTo make a Tree we need Nodes to get attached to it, So we introduce theconcept of CNode class. An CNode would look something like this:

Page 12: 3D Shooting Game

8/7/2019 3D Shooting Game

http://slidepdf.com/reader/full/3d-shooting-game 12/54

Attaching Nodes

Page 13: 3D Shooting Game

8/7/2019 3D Shooting Game

http://slidepdf.com/reader/full/3d-shooting-game 13/54

void AttachTo(CNode *newParent)

{// if this node is already attached to another node, then detach

if (parentNode)Detach();parentNode = newParent;

if (parentNode->childNode){prevNode = parentNode->childNode->prevNode;nextNode = parentNode->childNode;

- - - =

parentNode->childNode->prevNode = this;}else{parentNode->childNode = this; // this is the first child

}}

Page 14: 3D Shooting Game

8/7/2019 3D Shooting Game

http://slidepdf.com/reader/full/3d-shooting-game 14/54

CObject

To make anything a part of our 3D World, it should first be inherited fromCObject class. Later the inherited object can have different properties.

For Example: a Room and a Monster are not the same. Room is staticwhereas Monster attacks the player, it has animation in it and also someamount of AI.

CObject is basically derived from the CNode, which makes the Tree DataStructure.

Page 15: 3D Shooting Game

8/7/2019 3D Shooting Game

http://slidepdf.com/reader/full/3d-shooting-game 15/54

The Attributes of an Object are:Position Vector: Where the object is in the 3D WorldVelocity Vector: With what velocity the object is movingAcceleration Vector: Acceleration of the objectmaxX: This determines the maximum value of x component from the origin of theobjectminX: This determines the minimum value of x component from the origin of the objectmaxY: This determines the maximum value of y component from the origin of theobjectminY: This determines the minimum value of y component from the origin of the object

maxZ: This determines the maximum value of z component from the origin of the objectminZ: This determines the minimum value of z component from the origin of the objectSize: This describes the Size of the Object from the origin of the objectisDead: This determines whether the object should be in the 3D World or not.

The attributes maxX, minx, maxY, minY, maxZ, minZ describes the bounding box of theobject for Collision Detection w.r.t other objects.

Page 16: 3D Shooting Game

8/7/2019 3D Shooting Game

http://slidepdf.com/reader/full/3d-shooting-game 16/54

The CObject has the following methods:

Protected Member Functions:

virtual void OnAnimate(scalar_t deltaTime)virtual void OnDraw(CCamera *camera)virtual void OnCollision(CObject *collisionObject)virtual void OnPrepare()

Public Member Functions:void Draw(CCamera *camera)void Animate(scalar_t deltaTime)void ProcessCollisions(CObject *obj)void Prepare()CObject *FindRoot()

Page 17: 3D Shooting Game

8/7/2019 3D Shooting Game

http://slidepdf.com/reader/full/3d-shooting-game 17/54

The OnPrepare () has the following default code:

virtual void OnPrepare(){

ProcessCollisions(FindRoot()); // perform collisions starting// with root world object

}

The code of void Draw(CCamera *camera) is as Shown:void Draw(CCamera *camera){

glPushMatrix();OnDraw(camera); // draw this objectif (HasChild()) // draw children

((CObject*)childNode)->Draw(camera);glPopMatrix();

// draw siblingsif (HasParent() && !IsLastChild())

((CObject*)nextNode)->Draw(camera);}

Page 18: 3D Shooting Game

8/7/2019 3D Shooting Game

http://slidepdf.com/reader/full/3d-shooting-game 18/54

The Trace for Animate(deltaTime) is as shown below:

The Tree is Traversed in a Preorder Fashion

Page 19: 3D Shooting Game

8/7/2019 3D Shooting Game

http://slidepdf.com/reader/full/3d-shooting-game 19/54

The void ProcessCollisions(CObject *obj) also does something similar to Draw()and Animate() but it checks for the condition of Collision Occurrence first.The Following is the code:// perform collision detection

void ProcessCollisions(CObject *obj){

if(obj->maxX > position.x&& obj->minX < position.x&& obj->maxY > position.y&& obj->minY < position.y&& obj->maxZ > position.z

&& o j->minZ < position.z{

OnCollision(obj); // perform this object's collisionwith obj

// test child collisions with objif (HasChild())

((CObject*)childNode)->ProcessCollisions(obj);

// test sibling collisions with objif (HasParent() && !IsLastChild())

((CObject*)nextNode)->ProcessCollisions(obj);}

Page 20: 3D Shooting Game

8/7/2019 3D Shooting Game

http://slidepdf.com/reader/full/3d-shooting-game 20/54

// if obj has children, check collisions with these childrenif (obj->HasChild())

ProcessCollisions((CObject*)(obj->childNode));

// if obj has siblings, check collisions with these siblingsif (obj->HasParent() && !obj->IsLastChild())

ProcessCollisions((CObject*)(obj->nextNode));}

The ProcessCollisions(CObject *obj) is never directly called, instead it is called viaOnPrepare().

Page 21: 3D Shooting Game

8/7/2019 3D Shooting Game

http://slidepdf.com/reader/full/3d-shooting-game 21/54

The code of void Prepare() is as shown:// prepare objectvoid Prepare(){

OnPrepare(); // prepare this object

if (HasChild()) // prepare children((CObject*)childNode)->Prepare();

if (HasParent() && !IsLastChild()) // prepare siblings((CObject*)nextNode)->Prepare();

}

This makes up an Object!

Page 22: 3D Shooting Game

8/7/2019 3D Shooting Game

http://slidepdf.com/reader/full/3d-shooting-game 22/54

THE GAME ENGINE CORE

Our Game Engine Core is nothing but than the display Callback GameCycle ()The typical game cycle consists of the following steps:1. Gather input: This is taken care by the glutKeyboardFunc (OnKeyPress)2. Move player3. Perform artificial intelligence (AI)

4. Calculate physics5. Render the sceneAnd the code performing the same is shown below:

// prepare objects and perform collisionsgame or - repare ;

// move/orient camera. This is same as PlayergameCamera->Animate(deltaTime);

// move/orient objects. This wud let Monsters to think //and doors to slidegameWorld->Animate(deltaTime);// draw objectsgameWorld->Draw(gameCamera);//swap buffers. This Renders the SceneglutSwapBuffers();

Page 23: 3D Shooting Game

8/7/2019 3D Shooting Game

http://slidepdf.com/reader/full/3d-shooting-game 23/54

Handling InputThe Keyboard Callback OnKeyPress is as shown for the GAMESTART mode:

switch(key) {case GLUT_KEY_UP:

gameCamera->velocity += CVector(0,0,5);break;

case GLUT_KEY_DOWN:

gameCamera->velocity += CVector(0,0,5);break;

case GLUT_KEY_RIGHT:

- .

case GLUT_KEY_LEFT:

gameCamera->yaw -= 2.5; break;case GLUT_KEY_PAGE_UP: mouseSensitivity += 0.05f; break;case GLUT_KEY_PAGE_DOWN: mouseSensitivity -= 0.05f;

if (mouseSensitivity < 0.05)mouseSensitivity = 0.05;

break;case 27: mode=MMENU;

}

Page 24: 3D Shooting Game

8/7/2019 3D Shooting Game

http://slidepdf.com/reader/full/3d-shooting-game 24/54

Page 25: 3D Shooting Game

8/7/2019 3D Shooting Game

http://slidepdf.com/reader/full/3d-shooting-game 25/54

The Texture Class

class CTexture{private:

long int scaledWidth;long int scaledHeight;

public:int width;int height;

unsigned int texID;unsigned char *data;CTexture() { data = NULL; count++; printf("%d " , count ); }~CTexture() { Unload(); }void LoadTexture(char *filename);

void Unload(){

glDeleteTextures(1, &texID);

if (data != NULL)

free(data);

Page 26: 3D Shooting Game

8/7/2019 3D Shooting Game

http://slidepdf.com/reader/full/3d-shooting-game 26/54

data = NULL;

}};

The LoadTexture(filename) lets to load the desired texture, and the texture id wouldbe stored in texID.

Page 27: 3D Shooting Game

8/7/2019 3D Shooting Game

http://slidepdf.com/reader/full/3d-shooting-game 27/54

The Game WorldFinally, here we describe about the class which is letting us to walk into.We named it as CWorld class.It has the following attributes:All the Objects of the world. This mainly includes the Room Objects. Rooms can beof FRoom, LRoom, RRoom or Broom type.It has a pointer to the current position of the Game Camera: cameraIt has a CGUI class object which lets us to print scores and other stuff onto thescreen.And other miscellaneous objects as per the Game Development.It has the following Methods:void LoadRooms() : It loads all the rooms of the world into the memory. void LoadWorld() : This calls LoadRooms() and also creates some monsters as pernecessity.void Animate(float deltaTime) : This function lets the Preorder Traversal of all theObjects connected to the main Object “house” to be animated.void Draw(CCamera *camera) : This function lets the Preorder Traversal of all theObjects connected to the main Object “house” to be drawn onto the Screen.void Prepare() : This function lets the Preorder Traversal of all the Objectsconnected to the main Object “house” to be prepared for the next GameCycle.void FadeScreen() : This function Fades the screen by blending. We use thisFunction to display Game Over.And Other miscellaneous methods as per the Game Development.

Page 28: 3D Shooting Game

8/7/2019 3D Shooting Game

http://slidepdf.com/reader/full/3d-shooting-game 28/54

The Game CameraAs expected, the CCamera class defines the camera viewing system for theengine and is responsible for determining how you view the world. TheCCamera class is defined as follows:

class CCamera{public:

//CVector oldpos;CVector position; // position of cameraCVector velocity; // velocity of camera CVector acceleration; // acceleration of cameraCVector lookAt; // lookat vector

// up, forward, right vectorsCVector up;CVector forward;CVector right;

// yaw and pitch anglesfloat yaw;float pitch;CCamera();

Page 29: 3D Shooting Game

8/7/2019 3D Shooting Game

http://slidepdf.com/reader/full/3d-shooting-game 29/54

Page 30: 3D Shooting Game

8/7/2019 3D Shooting Game

http://slidepdf.com/reader/full/3d-shooting-game 30/54

The code of void Animate(scalar_t deltaTime) is:

void Animate(scalar_t deltaTime){

if (yaw >= 360.0f)yaw = 0.0f;

if (yaw < 0.0f)yaw = 360.0f;

i pitc > 60.0pitch = 60.0f;

if (pitch < -60.0f)pitch = -60.0f;

float cosYaw = (scalar_t)cos(DEG2RAD(yaw));float sinYaw = (scalar_t)sin(DEG2RAD(yaw));float sinPitch = (scalar_t)sin(DEG2RAD(pitch));

Page 31: 3D Shooting Game

8/7/2019 3D Shooting Game

http://slidepdf.com/reader/full/3d-shooting-game 31/54

float speed = velocity.z * deltaTime;float strafeSpeed = velocity.x * deltaTime;

if (speed > 15.0)speed = 15.0;

if (strafeSpeed > 15.0)strafeSpeed = 15.0;

if (speed < -15.0)speed = -15.0;s ra e pee - .

strafeSpeed = -15.0;

if (velocity.Length() > 0.0)acceleration = -velocity * 1.5f;

velocity += acceleration*deltaTime;

position.x += float(cos(DEG2RAD(yaw + 90.0)))*strafeSpeed;position.z += float(sin(DEG2RAD(yaw + 90.0)))*strafeSpeed;position.x += float(cosYaw)*speed;

position.z += float(sinYaw)*speed;

Page 32: 3D Shooting Game

8/7/2019 3D Shooting Game

http://slidepdf.com/reader/full/3d-shooting-game 32/54

lookAt.x = float(position.x + (cosYaw));lookAt.y = float(position.y + sinPitch);lookAt.z = float(position.z + (sinYaw));

gluLookAt(position.x, position.y, position.z,lookAt.x, lookAt.y, lookAt.z,0.0, 1.0, 0.0);

}

Page 33: 3D Shooting Game

8/7/2019 3D Shooting Game

http://slidepdf.com/reader/full/3d-shooting-game 33/54

Loading MD2 ModelsThis was the toughest part of our project. We learn the concepts of an MD2 Modelfrom various websites. But none taught us completely. Finally we got a clearerpicture of the concept from “OpenGL Game Programming”. Much of the codemodel loading we have used is directly from this book. Thanks to Dave Astle formaking this happen ☺.We named this class as CMD2Model. The CMD2Model is inherited from CObject

Page 34: 3D Shooting Game

8/7/2019 3D Shooting Game

http://slidepdf.com/reader/full/3d-shooting-game 34/54

MD2 Header is:typedef struct{

int ident; // identifies as MD2 file "IDP2"int version; // mine is 8int skinwidth; // width of textureint skinheight; // height of textureint framesize; // number of bytes per frameint numSkins; // number of texturesint numXYZ; // number of pointsint numST; // number of texture int numTris; // number of trianglesint numGLcmds;int numFrames; // total number of framesint offsetSkins; // offset to skin names (64 bytes each)int offsetST; // offset of texture s-t valuesint offsetTris; // offset of triangle meshint offsetFrames; // offset of frame data (points)int offsetGLcmds; // type of OpenGL commands to useint offsetEnd; // end of file

} modelHeader_t;

Page 35: 3D Shooting Game

8/7/2019 3D Shooting Game

http://slidepdf.com/reader/full/3d-shooting-game 35/54

Page 36: 3D Shooting Game

8/7/2019 3D Shooting Game

http://slidepdf.com/reader/full/3d-shooting-game 36/54

Page 37: 3D Shooting Game

8/7/2019 3D Shooting Game

http://slidepdf.com/reader/full/3d-shooting-game 37/54

To Animate the Model we use the concept of Keyframe Interpolation.For instance, suppose you have an animation of a bullet model, where the bullettravels from thePoint (x0, y0, z0) to the point (x1, y1, z1). The modeler can create a keyframeanimation

Sequence of just two frames from the bullet animation: one frame with the bulletlocated at (x0,y0, z0), and the next frame with the bullet located at (x1, y1, z1).Our job as a programmer Would be to then calculate all the points through which thebullet will travel in between (x0, y0,z0) and (x1, y1, z1) for a certain number of framesto pro uce a smoot an mat on o t e trave ng u et.

We use interpolation to determine framesbetween two keyframe. And the formula is:Xi + interpolatePercentage * (Xf - Xi)

Page 38: 3D Shooting Game

8/7/2019 3D Shooting Game

http://slidepdf.com/reader/full/3d-shooting-game 38/54

The more specific Start State to End State are listed belowFrame# Action----------------0-39 idle40-46 running47-60 getting shot but not falling (back bending)

61-66 getting shot in shoulder67-73 jumping74-95 idle96-112 getting shot and falling down

-

123-135 idle136-154 crouch155-161 crouch crawl162-169 crouch adjust weapon (idle)170-177 kneeling dying

178-185 falling back dying186-190 falling forward dying191-198 falling back slow dying

Page 39: 3D Shooting Game

8/7/2019 3D Shooting Game

http://slidepdf.com/reader/full/3d-shooting-game 39/54

Artificial Intelligence for Monsters

The Monsters are made to rotate around a desired path. It may be to move arounda room or to just to act as a watchman at doors.

Our AI design snapshot is something like this:

Page 40: 3D Shooting Game

8/7/2019 3D Shooting Game

http://slidepdf.com/reader/full/3d-shooting-game 40/54

Clockwise Angle Changes: AntiClockwise Angle Changes:

Page 41: 3D Shooting Game

8/7/2019 3D Shooting Game

http://slidepdf.com/reader/full/3d-shooting-game 41/54

Other AI Designs

Page 42: 3D Shooting Game

8/7/2019 3D Shooting Game

http://slidepdf.com/reader/full/3d-shooting-game 42/54

The Player

The player is the interactive part of the game and he can be moved around theworld with the mouse and keyboard.We should keep track of old coordinates to know how far the player should moveand in which direction.

Page 43: 3D Shooting Game

8/7/2019 3D Shooting Game

http://slidepdf.com/reader/full/3d-shooting-game 43/54

Sliding Door

Door slides automatically to open, when the player is near the door and closes as theplayer moves away from it, this is implemented with the help of distance of player from thedoor. If this distance is equal or less than the minimum distance then door opens and whendistance is greater than this minimum distance it closes.

Page 44: 3D Shooting Game

8/7/2019 3D Shooting Game

http://slidepdf.com/reader/full/3d-shooting-game 44/54

ROOMS

The Rooms are nothing but than an modified version of a cube. Having variousways of entrancesEntrance at Front: FROOM

Entrance at Back: BROOMEntrance at Left: LROOMEntrance at Right: RROOM

Each Rooms are made out of a still more basic Unit called as Wall.A CWall produces a Quad with position at the center.If Orientation=0 then it’s parallel to x-axisElse if Orientation=1 then it’s parallel to y-axis

Page 45: 3D Shooting Game

8/7/2019 3D Shooting Game

http://slidepdf.com/reader/full/3d-shooting-game 45/54

Rooms Design:

Page 46: 3D Shooting Game

8/7/2019 3D Shooting Game

http://slidepdf.com/reader/full/3d-shooting-game 46/54

Walls Design:

Page 47: 3D Shooting Game

8/7/2019 3D Shooting Game

http://slidepdf.com/reader/full/3d-shooting-game 47/54

Front Entrance Design:

Page 48: 3D Shooting Game

8/7/2019 3D Shooting Game

http://slidepdf.com/reader/full/3d-shooting-game 48/54

Fog

float fogColor[4] = {0.1, 0.1, 0.1, 0}; // Let's make the Fog Color black too

glFogi(GL_FOG_MODE, GL_EXP2 ); // Set The Fog ModeglFogfv(GL_FOG_COLOR, fogColor); // Set The Fog ColorglFogf(GL_FOG_DENSITY, 0.015f); // Set How Dense Will The Fog BeglHint(GL_FOG_HINT, GL_NICEST ); // Set The Fog's calculation accuracy glFogf(GL_FOG_START, 10); // Set The Fog's Start DepthglFogf(GL_FOG_END, 150); // Set The Fog's End Depth

glEnable(GL_FOG);

Page 49: 3D Shooting Game

8/7/2019 3D Shooting Game

http://slidepdf.com/reader/full/3d-shooting-game 49/54

Page 50: 3D Shooting Game

8/7/2019 3D Shooting Game

http://slidepdf.com/reader/full/3d-shooting-game 50/54

Page 51: 3D Shooting Game

8/7/2019 3D Shooting Game

http://slidepdf.com/reader/full/3d-shooting-game 51/54

Page 52: 3D Shooting Game

8/7/2019 3D Shooting Game

http://slidepdf.com/reader/full/3d-shooting-game 52/54

Page 53: 3D Shooting Game

8/7/2019 3D Shooting Game

http://slidepdf.com/reader/full/3d-shooting-game 53/54

CONLUSION

3D computer games includes the concept of vectors, projections, lightening andtexture mapping.Game is basically divided into these areas:GraphicsInput

Game logic andArtificial intelligenceUser interface and menuing system

Page 54: 3D Shooting Game

8/7/2019 3D Shooting Game

http://slidepdf.com/reader/full/3d-shooting-game 54/54