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
Demo 1: The objectiveDemo 1: The objective
Virtual realityVirtual realityVirtual world
avatar
Userinput
rendering interaction
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)
I/O librariesI/O libraries
rendering
Virtual world
input
simulation
OpenGL
Windows + GLUT
I/O managementI/O management
OperatingSystem
WindowsGLUT
main
DisplayFunc
KeyboadFunc
IdleFunc
OpenGLGraphicshardware
application
initializationcallback registration
callbacksSpecialFunc
Rendering with OpenGLRendering with OpenGL
Virtual world Viewing transformation
Perspectivetransformation
1325628 1325628
1.2.
clipping visibility: z-buffer display
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)
TexturingTexturing
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
GamesGames
avatar
Userinput
rendering interaction
AnimateIt(dt), DrawIt() ControlIt(dt),InteractIt()
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
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
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
TerrainTerrain
Complex geometry– Height field
Complex texture No Control No Animation Collision detection, Lifts objects
Terrain geometryTerrain geometry
z
x,yx
yz
z = height(x,y) Height function definition:Discrete samples + linear interpolation
Discrete samples = B&W ImageDiscrete samples = B&W Image
Height map Triangle mesh
Reducing the number of Reducing the number of triangles: Level of detailtriangles: Level of detail
Height-field texturing: Height-field texturing: projecting the top viewprojecting the top view
y
x
u
v
Terrain improvement: Terrain improvement: Detail mapDetail map
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
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)); }
SkySky
Image that is textured onsomething: sphere
Sky geometry: dome, sphere No Control No Animation No collision detection
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
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);
EnemyEnemy
Animated geometry– Defined by keyframes
organized as clips Stand, run, attack, die,
– + mesh animation Textures (animated) Artificial intelligence Collision detection
Keyframe animation: runningKeyframe animation: running
Inbetweening: Computation of Inbetweening: Computation of frames from keyframesframes from keyframes
keyframest
Nonlinear interpolation
linear interpolation
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
Example for bone animationExample for bone animation
Cyclic walkCyclic walk
Mesh Mesh morphingmorphing::t= 0
t= 1
Two neighboring keyframes
Time: t
Current vertex positions
Linear interpolation for
each vertex
Running as mesh morphingRunning as mesh morphing
+ position animation:
position += velocity * dt
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.
ClipsClips
Stand40 keyframes
Run5 keyframes
Salute11 keyframes
Motion controlMotion control
AI stateAI machine
Keyframeanimation
Clip = start, stop keyframe
Keyframesstored in an
MD2 file
time: t
Vertex positions of a triangle mesh
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
TextTexturinguring
BulletBullet
Geometry: sphere Textured Not intelligent Physical animation
Physical animation Physical animation of the bulletof the bullet
t
t+dt
acceleration = (0, 0, -g)velocity += acceleration * dtposition += velocity * dt
force, acceleration
velocity
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( );}
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
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)); } }}
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
BillboardsBillboards Single semi-transparent texture on an
oriented quadrilateral
pos
pos
QUAD
QUAD
Tmodell Tview Tperspectivexyz
XYZ
positionorientation
camera positioncamera orientation
ExplosionExplosion Seemingly complex, random
structure Similar look from all directions Collection of billboards
Particle system
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
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
AvatarAvatar
Keyboard controls its behavior:– ProcessInput
Its position and orientation will be the camera position and orientation before rendering– SetCameraTransform
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]
Keyboard controlKeyboard control
KeyboardFuncSpecialFunc
input
IdleFunc:GameLoop
virtualworld
IsSpace, IsLeft, IsRight, IsUp, IsDown
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
Shooting gameShooting game
TexturedObjectAvatar ParticleSystem
SkyDrawIt
TerrainDrawIt
AnimateIt
SelfProcessInput
ControlItInteractIt
ExplosionControlIt
EnemyDrawIt
InteractItControlIt
GameEngine
SpaceGame
350 C++ lines
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
Thanks for Thanks for your audienceyour audience