implementation of an outdoor shooting game szirmay-kalos lászló budapest university of technology...

55
Implementation of Implementation of an Outdoor an Outdoor Shooting Game Shooting Game Szirmay-Kalos László Budapest University of Technology email: [email protected] Web: http://www.iit.bme.hu/~szirmay

Post on 19-Dec-2015

218 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Implementation of an Outdoor Shooting Game Szirmay-Kalos László Budapest University of Technology email: szirmay@iit.bme.hu Web: szirmay

Implementation of an Implementation of an Outdoor Shooting GameOutdoor Shooting Game

Szirmay-Kalos LászlóBudapest University of Technology

email: [email protected]: http://www.iit.bme.hu/~szirmay

Page 2: Implementation of an Outdoor Shooting Game Szirmay-Kalos László Budapest University of Technology email: szirmay@iit.bme.hu Web: szirmay

Demo 1: The objectiveDemo 1: The objective

Page 3: Implementation of an Outdoor Shooting Game Szirmay-Kalos László Budapest University of Technology email: szirmay@iit.bme.hu Web: szirmay

Virtual realityVirtual realityVirtual world

avatar

Userinput

rendering interaction

Page 4: Implementation of an Outdoor Shooting Game Szirmay-Kalos László Budapest University of Technology email: szirmay@iit.bme.hu Web: szirmay

Tasks in gamesTasks in games

Image synthesis from the point of view of the avatar

Control of the avatar through input devices (keyboard, mouse)

Control of the intelligent virtual objects by artificial intelligence algorithms

Simulation of the physical laws (collisions, forces)

Page 5: Implementation of an Outdoor Shooting Game Szirmay-Kalos László Budapest University of Technology email: szirmay@iit.bme.hu Web: szirmay

I/O librariesI/O libraries

rendering

Virtual world

input

simulation

OpenGL

Windows + GLUT

Page 6: Implementation of an Outdoor Shooting Game Szirmay-Kalos László Budapest University of Technology email: szirmay@iit.bme.hu Web: szirmay

I/O managementI/O management

OperatingSystem

WindowsGLUT

main

DisplayFunc

KeyboadFunc

IdleFunc

OpenGLGraphicshardware

application

initializationcallback registration

callbacksSpecialFunc

Page 7: Implementation of an Outdoor Shooting Game Szirmay-Kalos László Budapest University of Technology email: szirmay@iit.bme.hu Web: szirmay

Rendering with OpenGLRendering with OpenGL

Virtual world Viewing transformation

Perspectivetransformation

1325628 1325628

1.2.

clipping visibility: z-buffer display

Page 8: Implementation of an Outdoor Shooting Game Szirmay-Kalos László Budapest University of Technology email: szirmay@iit.bme.hu Web: szirmay

Geometry definitionGeometry definitionglBegin(GL_TRIANGLES);

glColor3f( 1, 1, 0 ); glVertex3f( x11, y11, z11 );glVertex3f( x12, y12, z12 );glVertex3f( x13, y13, z13 );

glColor3f( 0, 1, 0 ); glVertex3f( x21, y21, z21 );glVertex3f( x22, y22, z22 );glVertex3f( x23, y23, z23 );

glEnd();

x11,y11,z11

x12,y12,z12

x13,y13,z13

glColor4f(R,G,B,A)

Page 9: Implementation of an Outdoor Shooting Game Szirmay-Kalos László Budapest University of Technology email: szirmay@iit.bme.hu Web: szirmay

TexturingTexturing

Page 10: Implementation of an Outdoor Shooting Game Szirmay-Kalos László Budapest University of Technology email: szirmay@iit.bme.hu Web: szirmay

Texture mappingTexture mapping

glEnable(GL_TEXTURE_2D);glBindTexture(GL_TEXTURE_2D, texture_id); // which textureglBegin(GL_TRIANGLES);glTexCoord2f(u1, v1); glVertex3f(x1, y1, z1);glTexCoord2f(u2, v2); glVertex3f(x2, y2, z2);glTexCoord2f(u3, v3); glVertex3f(x3, y3, z3);glEnd();glDisable(GL_TEXTURE_2D);

(u1, v1)

(u2, v2)

(u3, v3)

x1,y1,z1

x2,y2,z2

x3,y3,z3

Page 11: Implementation of an Outdoor Shooting Game Szirmay-Kalos László Budapest University of Technology email: szirmay@iit.bme.hu Web: szirmay

GamesGames

avatar

Userinput

rendering interaction

AnimateIt(dt), DrawIt() ControlIt(dt),InteractIt()

Page 12: Implementation of an Outdoor Shooting Game Szirmay-Kalos László Budapest University of Technology email: szirmay@iit.bme.hu Web: szirmay

Game objectsGame objects ControlIt:

– Interacts, thinks and applies his available controls (e.g. runs, shoots)

InteractIt – Looks at the states of other objects, checks collisions

AnimateIt: – Moves to its new position according to the elapsed time

DrawIt: – Draws itself onto the screen

Page 13: Implementation of an Outdoor Shooting Game Szirmay-Kalos László Budapest University of Technology email: szirmay@iit.bme.hu Web: szirmay

Simulation loopSimulation loop ( (Game loopGame loop))

void IdleFunc( ) { // idle call back float old_time = time; time = glutGet( GLUT_ELAPSED_TIME ); float dt = time - old_time;

avatar -> ProcessInput( ); world -> Control( dt ); world -> Animate( dt );

glClearColor(0, 0, 0, 0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); avatar -> SetCameraTransform(); world -> Draw( ); glutSwapBuffers( );}

dt

Page 14: Implementation of an Outdoor Shooting Game Szirmay-Kalos László Budapest University of Technology email: szirmay@iit.bme.hu Web: szirmay

Data structure of the virtual worldData structure of the virtual world

Game Objects are dynamic (killing) Game objects are different (heterogeneous collection) Parent-child relationships (owner-weapon)

terrain skyavatar

Enemy1

Enemy2

world

bulletexplosion

Join: új elem hozzávételeWeapon

Page 15: Implementation of an Outdoor Shooting Game Szirmay-Kalos László Budapest University of Technology email: szirmay@iit.bme.hu Web: szirmay

TerrainTerrain

Complex geometry– Height field

Complex texture No Control No Animation Collision detection, Lifts objects

Page 16: Implementation of an Outdoor Shooting Game Szirmay-Kalos László Budapest University of Technology email: szirmay@iit.bme.hu Web: szirmay

Terrain geometryTerrain geometry

z

x,yx

yz

z = height(x,y) Height function definition:Discrete samples + linear interpolation

Page 17: Implementation of an Outdoor Shooting Game Szirmay-Kalos László Budapest University of Technology email: szirmay@iit.bme.hu Web: szirmay

Discrete samples = B&W ImageDiscrete samples = B&W Image

Height map Triangle mesh

Page 18: Implementation of an Outdoor Shooting Game Szirmay-Kalos László Budapest University of Technology email: szirmay@iit.bme.hu Web: szirmay

Reducing the number of Reducing the number of triangles: Level of detailtriangles: Level of detail

Page 19: Implementation of an Outdoor Shooting Game Szirmay-Kalos László Budapest University of Technology email: szirmay@iit.bme.hu Web: szirmay

Height-field texturing: Height-field texturing: projecting the top viewprojecting the top view

y

x

u

v

Page 20: Implementation of an Outdoor Shooting Game Szirmay-Kalos László Budapest University of Technology email: szirmay@iit.bme.hu Web: szirmay

Terrain improvement: Terrain improvement: Detail mapDetail map

Page 21: Implementation of an Outdoor Shooting Game Szirmay-Kalos László Budapest University of Technology email: szirmay@iit.bme.hu Web: szirmay

Terrain collision Terrain collision detectiondetection

if (height(x,y) > z)

Collision!

Walking on the terrain:

Position(x, y) = (x, y, height(x,y) + legsize)

z

x,y

Page 22: Implementation of an Outdoor Shooting Game Szirmay-Kalos László Budapest University of Technology email: szirmay@iit.bme.hu Web: szirmay

Bi-linear Height field interpolationBi-linear Height field interpolationfloat Height( float x, float y ) { x += wwidth/2; y += wlength/2; x = x / wwidth * w; y = y / wlength * l; int X = (int)x, Y = (int)y; float h1 = height_field[Y * w + X] * wheight; float h2 = height_field[Y * w + X+1] * wheight; float h3 = height_field[(Y+1) * w + X] * wheight; float h4 = height_field[(Y+1) * w + X+1] * wheight; float xd = x - X; float yd = y - Y; float hx1 = h1 + xd * (h2 - h1); float hx2 = h3 + xd * (h4 - h3); return (hx1 + yd * (hx2 - hx1)); }

Page 23: Implementation of an Outdoor Shooting Game Szirmay-Kalos László Budapest University of Technology email: szirmay@iit.bme.hu Web: szirmay

SkySky

Image that is textured onsomething: sphere

Sky geometry: dome, sphere No Control No Animation No collision detection

Page 24: Implementation of an Outdoor Shooting Game Szirmay-Kalos László Budapest University of Technology email: szirmay@iit.bme.hu Web: szirmay

Definition of surfaces as Definition of surfaces as triangle meshes: Tessellationtriangle meshes: Tessellation1. Find a parametric equation of a sphere:

x(u,v) = x0 + r cos 2u sin v y(u,v) = y0 + r sin 2u sin vz(u,v) = z0 + r cos v u,v [0,1]

2. Select points in the unit rectangle

Page 25: Implementation of an Outdoor Shooting Game Szirmay-Kalos László Budapest University of Technology email: szirmay@iit.bme.hu Web: szirmay

GLU Quadrics: SphereGLU Quadrics: SphereGLUquadricObj * quadric; // definitionquadric = gluNewQuadric( );gluQuadricTexture(quadric, GL_TRUE);

// drawglBindTexture(GL_TEXTURE_2D, sky_texture_id);gluSphere(quadric, sky_radius, 32, 20);

Page 26: Implementation of an Outdoor Shooting Game Szirmay-Kalos László Budapest University of Technology email: szirmay@iit.bme.hu Web: szirmay

EnemyEnemy

Animated geometry– Defined by keyframes

organized as clips Stand, run, attack, die,

– + mesh animation Textures (animated) Artificial intelligence Collision detection

Page 27: Implementation of an Outdoor Shooting Game Szirmay-Kalos László Budapest University of Technology email: szirmay@iit.bme.hu Web: szirmay

Keyframe animation: runningKeyframe animation: running

Page 28: Implementation of an Outdoor Shooting Game Szirmay-Kalos László Budapest University of Technology email: szirmay@iit.bme.hu Web: szirmay

Inbetweening: Computation of Inbetweening: Computation of frames from keyframesframes from keyframes

keyframest

Nonlinear interpolation

linear interpolation

Page 29: Implementation of an Outdoor Shooting Game Szirmay-Kalos László Budapest University of Technology email: szirmay@iit.bme.hu Web: szirmay

What and how to interpolateWhat and how to interpolate High quality animation:

– Newton’s laws: the second derivative of a motion curve is proportional to the force, which acts through an elastic mechanism: interpolation curve is C2

– Even the interpolated frames should meet physical constraints: Bone animation

Games:– Linear interpolation– Interpolate the vertices of the mesh:

Mesh deformation

Page 30: Implementation of an Outdoor Shooting Game Szirmay-Kalos László Budapest University of Technology email: szirmay@iit.bme.hu Web: szirmay

Example for bone animationExample for bone animation

Page 31: Implementation of an Outdoor Shooting Game Szirmay-Kalos László Budapest University of Technology email: szirmay@iit.bme.hu Web: szirmay

Cyclic walkCyclic walk

Page 32: Implementation of an Outdoor Shooting Game Szirmay-Kalos László Budapest University of Technology email: szirmay@iit.bme.hu Web: szirmay

Mesh Mesh morphingmorphing::t= 0

t= 1

Two neighboring keyframes

Time: t

Current vertex positions

Linear interpolation for

each vertex

Page 33: Implementation of an Outdoor Shooting Game Szirmay-Kalos László Budapest University of Technology email: szirmay@iit.bme.hu Web: szirmay

Running as mesh morphingRunning as mesh morphing

+ position animation:

position += velocity * dt

Page 34: Implementation of an Outdoor Shooting Game Szirmay-Kalos László Budapest University of Technology email: szirmay@iit.bme.hu Web: szirmay

Motion definitionMotion definition

Keyframes are organized into clips Keyframes are designed off line and stored

in a file: MD2, MD3, etc. file formats Typical clips in a game:

– Run, stand, attack, die, pain, salute, crouch, wave, point, taunt, etc.

Page 35: Implementation of an Outdoor Shooting Game Szirmay-Kalos László Budapest University of Technology email: szirmay@iit.bme.hu Web: szirmay

ClipsClips

Stand40 keyframes

Run5 keyframes

Salute11 keyframes

Page 36: Implementation of an Outdoor Shooting Game Szirmay-Kalos László Budapest University of Technology email: szirmay@iit.bme.hu Web: szirmay

Motion controlMotion control

AI stateAI machine

Keyframeanimation

Clip = start, stop keyframe

Keyframesstored in an

MD2 file

time: t

Vertex positions of a triangle mesh

Page 37: Implementation of an Outdoor Shooting Game Szirmay-Kalos László Budapest University of Technology email: szirmay@iit.bme.hu Web: szirmay

Artificial Intelligence of an EnemyArtificial Intelligence of an Enemy

Dont Care Escape

Chase Attack

Dying

Dist < 4 &&Avatar_angle > 60

Dist < 4 &&Avatar_angle < 40

Collisionwith the bullet

Dist < 1

Dist > 1

Dist > 6Avatar_angle < 20

Avatar_angle

Avatar

Page 38: Implementation of an Outdoor Shooting Game Szirmay-Kalos László Budapest University of Technology email: szirmay@iit.bme.hu Web: szirmay

TextTexturinguring

Page 39: Implementation of an Outdoor Shooting Game Szirmay-Kalos László Budapest University of Technology email: szirmay@iit.bme.hu Web: szirmay

BulletBullet

Geometry: sphere Textured Not intelligent Physical animation

Page 40: Implementation of an Outdoor Shooting Game Szirmay-Kalos László Budapest University of Technology email: szirmay@iit.bme.hu Web: szirmay

Physical animation Physical animation of the bulletof the bullet

t

t+dt

acceleration = (0, 0, -g)velocity += acceleration * dtposition += velocity * dt

force, acceleration

velocity

Page 41: Implementation of an Outdoor Shooting Game Szirmay-Kalos László Budapest University of Technology email: szirmay@iit.bme.hu Web: szirmay

The bullet is flyingThe bullet is flying: : AAnimate, nimate, DDrawrawvoid Bullet::AnimateIt( float dt ) { acceleration = Vector(0,0,-g); speed += acceleration * dt; position += speed * dt;}

void Bullet::DrawIt( ) { glPushMatrix( ); glTranslate(position.x, position.y, position.z); glBindTexture(GL_TEXTURE_2D, bullet_texture); gluSphere(quadric, 1.0, 16, 10); glPopMatrix( );}

Page 42: Implementation of an Outdoor Shooting Game Szirmay-Kalos László Budapest University of Technology email: szirmay@iit.bme.hu Web: szirmay

Collision detection between Collision detection between two slow objectstwo slow objects

dist = obj1.position - obj2.positionmin = obj1.BoundingRadius() + obj2.BoundingRadius()if (dist.Length() < min) Collision!

given t

Problem with fast objects

t

t + t

Page 43: Implementation of an Outdoor Shooting Game Szirmay-Kalos László Budapest University of Technology email: szirmay@iit.bme.hu Web: szirmay

Bullet collision detectionBullet collision detectionBullet::InteractIt( GameObject * obj ) { if (obj->GetType() == TERRAIN) { Mountain * terrain = (Mountain *)obj; if (terrain->Height(position.x, position.y)> position.z) { KillIt(); world -> Join(new Explosion(position)); } } if ( obj->GetType() == AVATAR || obj->GetType() == ENEMY ) { if (“bounding spheres overlap”) { KillIt(); obj -> KillIt( ); world -> Join(new Explosion(position)); } }}

Page 44: Implementation of an Outdoor Shooting Game Szirmay-Kalos László Budapest University of Technology email: szirmay@iit.bme.hu Web: szirmay

Collision detection with fast objectsCollision detection with fast objects: :

ray-tracingray-tracing

velocity

rel_velocity = velocity - vel2ray: position + rel_velocity·t

If (ray intersects bounding sphere first AND tintersect < dt) Collision!

hit_object = world->Intersect(position,velocity,t);

ship1 ship2 avatar space sun

world

bulletexplosion

position

vel2

Page 45: Implementation of an Outdoor Shooting Game Szirmay-Kalos László Budapest University of Technology email: szirmay@iit.bme.hu Web: szirmay

BillboardsBillboards Single semi-transparent texture on an

oriented quadrilateral

pos

pos

QUAD

QUAD

Tmodell Tview Tperspectivexyz

XYZ

positionorientation

camera positioncamera orientation

Page 46: Implementation of an Outdoor Shooting Game Szirmay-Kalos László Budapest University of Technology email: szirmay@iit.bme.hu Web: szirmay

ExplosionExplosion Seemingly complex, random

structure Similar look from all directions Collection of billboards

Particle system

Page 47: Implementation of an Outdoor Shooting Game Szirmay-Kalos László Budapest University of Technology email: szirmay@iit.bme.hu Web: szirmay

Particle SystemsParticle Systems

position: position += velocity * dtvelocity: velocity += acceleration * dtacceleration: acceleration = force / weight

lifetimeage: age += dt; if (age > lifetime) Kill();

size, dsize: size += dsize * dt;weight, dweight: weight += dweight * dtcolor, dcolor: color += dcolor * dt

global force field(smoke)

randominitialvalues

Page 48: Implementation of an Outdoor Shooting Game Szirmay-Kalos László Budapest University of Technology email: szirmay@iit.bme.hu Web: szirmay

Explosion settingsExplosion settings

position = Rand( center, 0.1 );lifetime = Rand( 2.0, 1.0 );

size = 0.001; dsize = Rand( 1.0, 0.5 ) / lifetime / 2.0;

velocity = Rand( CVector(0, 0, 0), 0.4 );acceleration = Rand( CVector(0, 0, 0), 0.4 );

color = Rand( Color(1, 0.5, 0, 1), Color(0, 0.5, 0, 0) );dcolor = Color(0, -0.5, 0, -1) / lifetime / 2;

Rand

Page 49: Implementation of an Outdoor Shooting Game Szirmay-Kalos László Budapest University of Technology email: szirmay@iit.bme.hu Web: szirmay

AvatarAvatar

Keyboard controls its behavior:– ProcessInput

Its position and orientation will be the camera position and orientation before rendering– SetCameraTransform

Page 50: Implementation of an Outdoor Shooting Game Szirmay-Kalos László Budapest University of Technology email: szirmay@iit.bme.hu Web: szirmay

Avatar :: SetCameraTransformAvatar :: SetCameraTransformAvatar :: SetCameraTransform( ) { glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(position.x, position.y, position.z,

position.x + head.x, position.y + head.y, position.z + head.z,

up.x, up.y, up.z);}

eye

lookat

up = [0, 1, 0]

Page 51: Implementation of an Outdoor Shooting Game Szirmay-Kalos László Budapest University of Technology email: szirmay@iit.bme.hu Web: szirmay

Keyboard controlKeyboard control

KeyboardFuncSpecialFunc

input

IdleFunc:GameLoop

virtualworld

IsSpace, IsLeft, IsRight, IsUp, IsDown

Page 52: Implementation of an Outdoor Shooting Game Szirmay-Kalos László Budapest University of Technology email: szirmay@iit.bme.hu Web: szirmay

Game engineGame engine

GameObjectposition, velocity, accelerationControlIt(float dt )AnimateIt(float dt)InteractIt( GameObject * o) DrawIt( )IntersectIt(Ray r, float& t)

MemberControl, Animate, DrawInteract, Intersect, Join

TexturedObject

next

AvatarProcessInput()

SetCameraTransform()

TextureLoad( char * fname)

BillBoardDrawIt()

ParticleSystemEmit(int n)

Particle

GLUTWindow

GameEngineDisplayFunc

IdleFuncKeyPress

world

avatar

500 C++ lines

Page 53: Implementation of an Outdoor Shooting Game Szirmay-Kalos László Budapest University of Technology email: szirmay@iit.bme.hu Web: szirmay

Shooting gameShooting game

TexturedObjectAvatar ParticleSystem

SkyDrawIt

TerrainDrawIt

AnimateIt

SelfProcessInput

ControlItInteractIt

ExplosionControlIt

EnemyDrawIt

InteractItControlIt

GameEngine

SpaceGame

350 C++ lines

Page 54: Implementation of an Outdoor Shooting Game Szirmay-Kalos László Budapest University of Technology email: szirmay@iit.bme.hu Web: szirmay

Indoor games?Indoor games?

No terrain Building

– Cannot be represented by a height field– Textured triangle mesh

Collision: without checking all triangles Fast visibility: quickly eliminating the potentially

invisible objects

Page 55: Implementation of an Outdoor Shooting Game Szirmay-Kalos László Budapest University of Technology email: szirmay@iit.bme.hu Web: szirmay

Thanks for Thanks for your audienceyour audience