Game Programming Basics
Download
Report
Transcript Game Programming Basics
CS 382
GAME DESIGN,
DEVELOPMENT,
AND TECHNOLOGY
PART 3.3
PROGRAMMING FUNDAMENTALS
• DATA STRUCTURES
• BIT PACKING
• OBJECTS-ORIENTED
VS.
COMPONENT-BASED
PROGRAMMING FUNDAMENTALS
SEVERAL STANDARD
DATA STRUCTURES FIGURE PROMINENTLY IN
GAME PROGRAMMING…
VECTORS & MATRICES
FACILITATE THE IMPLEMENTATION OF LINEAR
ALGEBRA FORMULAS IN GRAPHICS AND ANIMATION
DICTIONARIES & HASH TABLES
FACILITATE THE IMPLEMENTATION OF MASSIVE
LOOK-UP TABLES FOR AUDIO AND VISUAL
ASSOCIATIONS
STACKS & QUEUES
MANAGE THE APPLICATION OF AFFINE
TRANSFORMATIONS (TRANSLATIONS,
ROTATIONS, SCALING) AND ENTITY
INTERACTIONS (COLLISIONS,
MULTIPLAYER COMMANDS, ETC.)
PART 3.3
PROGRAMMING FUNDAMENTALS
PAGE 88
DATA STRUCTURE:
LAYERED ISOMETRIC GRID
ONE COMMON GAME DATA STRUCTURE IS THE
MAP, WHICH IS BASED ON AN ARRAY OF CELLS,
EACH OF WHICH HAS AN ASSOCIATED TERRAIN
TYPE, STRUCTURE TYPE, AND POINTERS TO MOBILE
GAME UNITS (TROOPS, CREATURES, ETC.).
Direction
PART 3.3
PROGRAMMING FUNDAMENTALS
PAGE 89
Adjacency Location
North
(x, y-1)
Northeast
(x, y-1)
if y is even
(x+1, y – 1) if y is odd
East
(x+1, y)
Southeast
(x, y+1)
if y is even
(x+1, y+1) if y is odd
South
(x, y+2)
Southwest
(x-1, y+1) if y is even
(x, y+1) if y is odd
West
(x-1, y)
Northwest
(x-1, y-1) if y is even
(x, y-1)
if y is odd
DATA STRUCTURE: PARTICLE SYSTEM
TO MODEL CERTAIN NON-POLYGONAL OBJECTS (E.G., SPLASHING
WATER, EXPLOSIONS, SMOKE), A SYSTEM OF HUNDREDS OR
THOUSANDS OF SMALL PARTICLES MAY BE USED, WITH EACH
PARTICLE HAVING ITS OWN ATTRIBUTES.
FIELD
PART 3.3
PROGRAMMING FUNDAMENTALS
PAGE 90
TYPE
Position
Vector
Velocity
Vector
Color
Vector
Energy
Integer
Size
Float
Transparency
Float
TimeToLive
Integer
DATA STRUCTURE: SUBDIVISION SURFACE
WHEN 3D OBJECTS ARE DISTANT FROM THE
VIEWER, THEIR LEVEL OF DETAIL CAN BE LOW,
WHILE WHEN THEY’RE NEAR THE VIEWER, THE
LEVEL OF DETAIL MUST BE HIGH.
SUBDIVISION SURFACES ARE USED TO EXPAND
A 3D MODEL LACKING DETAILS INTO A MORE
ELABORATE MODEL.
PART 3.3
PROGRAMMING FUNDAMENTALS
PAGE 91
DATA
STRUCTURE
DESCRIPTION
Vertex
(x,y,z) coordinate info
Triangle
Three vertex objects
Triangular Mesh
Array of triangles
Adjacency Matrix
2D table of vertex adjacencies
Triangle Subdivider
Drives triangular mesh subdivision
DATA STRUCTURE:
INTERACTIVE MUSIC SEQUENCER
AN INTERACTIVE
MUSIC SEQUENCER PROVIDES THE INTERACTION OF
COMPUTER GAMES WITH THE IMMERSIVE QUALITIES OF MUSIC.
// Note: The red fields are the
// interactive features.
typedef list< Sequence * > SequencePtrList_t;
typedef list< Track * >
TrackPtrList_t;
typedef list< Voice * >
VoicePtrList_t;
class MusicSequencer_t
{
MusicSequencerState_t
SequencePtrList_t
SequencePtrList_t
TrackPtrList_t
TrackPtrList_t
VoicePtrList_t
VoicePtrList_t
};
State;
ActiveSequencePtrList;
FreeSequencePtrList;
ActiveTrackPtrList;
FreeTrackPtrList;
ActiveVoicePtrList;
FreeVoicePtrList;
class SequenceState_t
{
Tempo_t Tempo;
Volume_t Volume;
};
class Sequence_t
{
SequenceState_t
SequenceState_t
SequenceState_t
SequenceInterpolator_t
TimeUnit_t
TimeUnit_t
CallbackFunc_t
TrackPtrList_t
};
class TrackState_t
{
Volume_t
Volume;
PitchBend_t PitchBend;
Pan_t
Pan;
Effect_t
Effect;
};
PART 3.3
PROGRAMMING FUNDAMENTALS
PAGE 92
State;
BeginState;
EndState;
Interpolator;
TimeElapsed;
TimeStep;
*pCallback;
TrackPtrList;
class Track_t
{
TrackState_t
TrackState_t
TrackState_t
TrackInterpolator_t
Sequence
char
Instrument_t
VoicePtrList_t
};
State;
BeginState;
EndState;
Interpolator;
*pOwner;
*pEvent;
*pInstrument;
VoicePtrList;
class VoiceState_t
{
SynthVolume_t Volume;
SynthPitch_t Pitch;
SynthPan_t
Pan;
SynthEffect_t Effect;
};
class Voice_t
{
VoiceState_t
VoiceState_t
VoiceState_t
VoiceInterpolator_t
Track_t
char
};
CurrentState;
BeginState;
EndState;
Interpolator;
*pOwner;
nKey;
DATA STRUCTURE: SPRING NODE
ONE
METHOD FOR IMPLEMENTING ANIMATED CLOTH IS TO
PLACE INVISIBLE SPRING NODES AT VERTEX LOCATIONS
WITHIN THE CLOTH.
FIELD
TYPE
Position
Vector
Velocity
Vector
Force
Vector
Mass
Float
Neighbor List
Linked list of other spring nodes
Distance List
Parallel list of neighbor distances
Neighbor Count
Integer
ONE
REASONABLE MODEL HAS
EACH NODE CONNECTED TO…
•
ITS NEAREST NEIGHBORS
(VERTICAL
AND HORIZONTAL)
BY STRONG STRETCH SPRINGS
•
ITS DIAGONAL NEIGHBORS BY
WEAKER SHEAR SPRINGS
•
PART 3.3
PROGRAMMING FUNDAMENTALS
PAGE 93
ALL OF ITS NEIGHBORS BY
WEAK BEND SPRINGS
DATA STRUCTURE: BSP TREE
TO EFFICIENTLY DETERMINE THE ORDER IN WHICH POLYGONS SHOULD
BE RENDERED, BINARY SPACE PARTITIONING TREES ARE USED.
IN THIS DOOM-LIKE LAYOUT, THE GAME
THE LAYOUT SURFACES ARE USED TO PERFORM A
BINARY SPACE PARTITIONING (UP TO A CERTAIN DEPTH).
DEVELOPER WANTS THE PLAYER TO SEE
INTO ADJACENT ROOMS (AND SOMETIMES
THROUGH THOSE ROOMS INTO
ADDITIONAL ROOMS), BUT THE 80-ROOM
LAYOUT MAKES DETERMINING WHAT’S
VISIBLE SOMEWHAT PROBLEMATIC.
IN ADDITION, THE OPTICAL CONNECTIVITY IS
DETERMINED BETWEEN THE VARIOUS ROOMS AND
CORRIDORS, USING DOORS AND WINDOWS TO DETERMINE
VIABLE ADJACENCIES.
PROGRAMMING FUNDAMENTALS
PAGE 94
THIS INFORMATION IS THEN COMBINED TO QUICKLY
DETERMINE OVERALL VISIBILITY BETWEEN ROOMS.
NETWORK
BIT PACKING FOR
NETWORK COMPRESSION
struct NetData
{
unsigned char
unsigned long
unsigned short
int
int
};
GAMES
NEED TO SEND
GAME STATE
INFORMATION
SUCH AS
MsgType;
Time;
Element;
XPosition;
YPosition;
//
//
//
//
//
0 - 28
0 - 0xFFFFFFFF
148 - 1153
-100,000 – 100,000
-100,000 – 100,000
struct NetData : public BitPackCodec<5>
{
PkUint<unsigned char, 5>
MsgType;
PkUint<unsigned long>
Time;
PkUint<unsigned short, 148, 1153> Element;
PkUint<int, -100000, 100000>
XPosition;
PkUint<int, -100000, 100000>
YPosition;
};
1
4
4
4
4
byte
bytes
bytes
bytes
bytes
BASIC
POSITION,
STRUCTURE
WITHOUT BIT
PACKING:
136 BITS
VELOCITY,
ACCELERATION,
AND STATUS
FLAGS, BUT
//
//
//
//
//
5 bits
32 bits
10 bits
18 bits
18 bits
STORAGE BYTES
BASIC
ARE OFTEN
STRUCTURE
WITH BIT
PACKING:
BITS
PADDED WITH
83
BITS CONTAINING
NO REAL
INFORMATION.
WHILE
THE USE OF BIT PACKING GREATLY REDUCES THE BANDWIDTH
REQUIREMENTS FOR NETWORK GAMING, IT COMPLICATES DEBUGGING
AND SLOWS DOWN PERFORMANCE (DUE TO THE LINEAR TIME
COMPLEXITY OF THE PACKING AND UNPACKING OPERATIONS).
PROGRAMMING FUNDAMENTALS
PAGE 95
OBJECT-ORIENTED VS.
COMPONENT-BASED
TRADITIONAL
HOW CAN WEAPONS BE MADE
ANIMATABLE?
HOW CAN DOORS AND GEMS BE
MADE DESTRUCTIBLE, BUT NOT
ACTORS AND WEAPONS?
Object
OBJECT
Logical Object
Renderable Object
ORIENTATION USES
AN INHERITANCE
HIERARCHY TO
Particle System
Physics Object
Spawn Point
Trigger Box
DEFINE THE
CHARACTERISTICS
Collectable Object
Animatable Object
OF GAME ELEMENTS.
Actor
COMPONENT-BASED
PROGRAMMING
RELIES ON
AGGREGATE
BEHAVIOR, I.E.,
ASSIGNING EACH
GAME ENTITY A SET
OF BEHAVIORS AND
FUNCTIONALITIES.
PART 3.3
PROGRAMMING FUNDAMENTALS
PAGE 96
Door
Weapon
THE FLEXIBILITY OF
Entity:
Weapon
Rendering
Component
Animating
Component
COMPONENTS AND
THEIR RELIANCE ON
RUN-TIME MESSAGE
Collecting
PASSING
Component
COMPLICATES
DEBUGGING AND
PERFORMANCE.
Animating
Component
Destroying
Component
Entity:
Door
Rendering
Component
Gem
Healthpack