<<

Game Engines Introduction Game Engines are software platforms used to create and run video games. One of the reasons that Game Engines exist is because creating games requires a lot of different kinds of complex software. Computer games include video elements, user interface elements, a variety of resources that must be loaded and used, and other complex systems. If each game developer had to create these various software systems for each game, the development time required to create a new game would be too long.

One thing that most software developers do is save old code. If a developer can just plug in software elements that have been previously used and have been tested, then this makes the development cycle shorter. Why write the same code again when a developer can just plug-in already written code.

This same idea is behind game engines. In the early days, game developers saved their old code and systems and just reused them on new games. At some point, the idea was to create modular systems that are more self-contained and easier to reused. This was one of the driving elements for Game Engines.

The other reason for modern Game Engines was that games were being developed for multiple platforms. These include , consoles, hand-held devices, and the Internet. Game developers wanted to sell their games on multiple platforms so they added a game run-time element to game engines. The run-time element was a layer where the game hardware was virtualized. This means that when the code sends a draw command to the hardware, the draw command first goes through a layer that translates it to the appropriate hardware commands.

This hardware layer is called a Game Runtime. This means that Game Engine included a runtime element that runs on top of the hardware and through with the game code executes. Main Elements The main elements of modern Game Engines include:

• Game Development Tools – this includes tools for assembling game elements in a graphical environment and game scripting tools • Game Internal Elements – this includes various elements used by game developers to draw graphics, simulate physics, use artificial intelligence elements, etc. These are blocks of code that can be attached to game elements. Some of these are third party Software Development Kits (SDK’s) that can be plugged into the Game Engine. • Game Runtime Element – this is the runtime code that support multiple hardware for the game. When the game is compiled a target platform is selected and the Game Engine adds the appropriate runtime layer to the game.

The internal elements to a typical game engine are indicated in the following diagram.

As you can see, Game Engines are complex and difficult to create. For this reason, many game companies either create and reuse their own game engines or purchase commercial game engines. Many commercial game engines, when used, include the underlying code in which the game engines was created so game developers can modify the parts of the engine they need for their own games.

The rise of easy-to-use commercial and open game engines is a boon for independent game developers. These developers can take advantage of the game engine to create playable and sometimes successful games.

There are a lot of game engines (https://en.wikipedia.org/wiki/List_of_game_engines). Popular game engines include:

Unity GameMaker CryEngine SDK AppGameKit -x MonoGame ShiVa Engine HeroEngine Common Features Many Game Engines have some common features. These include:

• Graphics editor for game components • Representing game elements as Objects and Properties • Layout tools for Game Levels • Physics elements to simulate physical movement • Scripting elements

The graphical editor allows the developer to edit various graphical features. While Game Engine graphical editors are not as full-featured as dedicated graphical software, they do include enough features to allow some of graphical editing.

Another important feature that most game engines share is how game elements are represented in the game development environment. Most game engines use the Object/Property model to represent game elements. This means that the developer can treat game elements in a simplified manner and use ideas such as inheritance to good effect. This object/property model also works with most game scripting languages since almost all modern programming and scripting languages use the Object/Property model to represent software components.

Many game engines include some kind of layout tools for building sections of the game. This could include tools for arranging game objects and other game elements, create movement paths for objects, and other level design tools.

Since most games include movable objects, one way to ensure that such objects move and react to the environment in a realistic manner is to include various physics elements. This consists of software modules that simulate realistic movement of objects such as gravity, momentum, force, and other physical forces. Since these can be simulated by standard calculations, building in a physics component simplified the game development.

Few Game Engines include a purely graphical development environment. This means that, at some point, the developer will have to use a programming or to control the game. Most Game Engines have their own built-in scripting language (often based on a mainstream language such as C/C++, Java, Python, etc). The Game Engine will used the developer created scripts to the game code along with the various graphic, physics, resources, etc. Basic Game Programming Concepts There are some basic concepts that game developers should be familiar with when developing games. These include:

1. The Game Loop 2. Event structures and callback Methods 3. Sprites and 3D Objects 4. The Game Coordinate System 5. Vectors 6. Game Cameras Game Loop The Game Loop refers to how the game software runs when the game is running. Basically, game software is written to run in a loop. Inside this loop the game software performs a number of things, such as displaying graphics on the screen. Games display animated objects by drawing and redrawing images on the screen. Each time through the game loop the screen is redrawn, which gives the impression of 2D and 3D objects moving across the screen.

The speed of the game loop is typically measured in Frames Per Second (FPS) which is how fast the screen is redrawn. Many games now include FPS rates of 60 or above, which means that the game loop is running at 60 times a second.

Inside the game loop the game software displays graphics on the screen as well as performing other tasks. For most games, the Game Loop includes:

1. Get Input from player and evaluate game state 2. Update game state 3. Redraw the screen 4. Go back to step 1

If the game is running at 60 FPS then these steps are done 60 times a second. Notice that there is more than just redrawing the screen that is happing in the game loop. This includes:

• Getting input from the player such as from the keyboard, mouse, controller, etc. • Evaluating the current state of the game. This would include tracking game elements, positions, sound, etc. • Updating the game state. This would include adding or removing game objects, moving game objects, playing sounds and videos, etc.

To use a simple example, suppose your game has a graphical object displayed on the screen. When the player presses the left arrow key the game should move the object to the left a certain number of pixels. When the player stops pressing the left arrow key the game should stop moving the graphical object. The game loop performing this action might look like:

1. Read the keyboard and get the current position of the object. 2. If the keyboard is the left arrow key then set the position of the object some number of pixels to the left. 3. Redraw the object using its current position on the screen. 4. Go back to 1

There are a number of things happening inside the game loop, not only moving graphical objects. These could include things like, for example, adding 100 pieces of gold if your character sells equipment, sets a new quest based on a dialog with a non-player character, changing the game camera so the player can look in a new direction. All of these will be happening inside the game loop (but not necessarily 60 times a second).

Non-game sometimes have a hard time dealing with the game loop environment. Regular programmers deal with a single process through the code and they typically don’t have to consider running their code inside a loop. The regular code progress is under the control of the . With game programming the game operation and progress is under the control of the game loop. For example, if code is designed to call a method it will be called 60 times a second inside the game loop.

Because game code is running inside a game loop, calling methods inside the loop is not reliable. For this reason, many Game Engines use an Event-based Callback system to link the game code to what happens inside the loop.

With a regular non-game program the code for checking a condition and performing an action would look like:

A. Check condition R B. If R is true then do X C. Else continue with the program

Inside a game loop this does not work as well. The checking for a condition and the performance of X is taking place 60 times a second. This is why gaming code normally uses and Event/Callback system.

An Event/Callback system is a software architecture that contains the following components:

• A method for associating an event with an action. • An event checker that looks for events and puts them in a queue. • A Callback checker that looks at the event queue and, when it sees an event associated with an action, calls the action.

For example, a game that allows the programmer to move an object on the screen by pressing arrow keys would support code that looks like:

A. Associate moving an object the left with pressing the left arrow key B. Associate stopping an object movement with releasing the left arrow key C. Storing all game related events on an event queue. D. Examining each event in the event queue. E. If the event has an associated action, performing the action.

The first steps, A and B, are done before the game loop starts. The other steps, C-E, are inside the game loop. As the game loop runs, the game event checker looks at all events happening inside the game loop. These events could be mouse movements, keyboard input, controller input, objects overlapping in the screen, etc. As each event is examined, those that have an associated action are retrieved from the queue and the action performed. The game developer must figure out what events should cause which actions as the game runs. Sprites and 3D Objects Games can be generally divided into 2D and 3D games. This references how the game camera is positioned and the type of objects and characters in the game. With 2D games the screen is a fixed camera and displays the game as a flat 2D environment. With 3D games the camera moves as the player plays the game. In 2D games the objects on the screen are Sprites (2d images that can be moved around the screen). In 3D games the objects are 3D objects that have and can display different sides to the object.

2D game objects are called Sprites. These are graphic images that are displayed on the screen. Sprites have a width and height and are contained in a rectangular area. Sprites that look like they have a non-rectangular shape normally have a background color that has been rendered transparent so the game background can show through.

3D game objects are normally created with a 3D object creator program such as . This program is used by the designer to create the 3D objects. These 3D objects can then be imported into game engines and displayed when the game runs. When a 3D object is displayed in a game it is displayed on a 3D screen. As the user moves the camera around the scene the various sides of the object are shown on the screen (this is done inside the game loop). The game engine must render the 3D object into a 2D view and show it to the player. 3D designers will export a 3D object as a series of numbers that describe the object as a series of triangles (vertices). These triangles are then drawn on the screen by the game engine, the object texture is mapped onto the object, the lighting is applied to the object, and the entire thing displayed to the player.

A coordinate system is a way to position objects in a specified environment. There are two modes for games, 2D and 3D (actually, there is somethings called 2.5D which is also called an isometric view).

In a 2D system the position of an object is referenced by two values, the x and y value of the object. Typically, using a Cartesian system, the x values are from left to right and y value is from top to bottom. There are a couple of values that must be set before using this kind of positioning system. These are:

The starting point for the x/y values The direction for the x/y values The “home” position of the object

The starting point for the x/y values means where to we start the point for x/y. Some of the options are:

In this coordinate system the position of an object is measured from the center of the screen/scene. The x/y zero position is in the center of the screen. An object at 20, -15 is 20 units to the right of the center and -15 units down from the center.

Another Screen coordinate system is the one used by the OpenGL graphics system:

In this system the origin is the lower left corner of the screen with x increasing as it goes to the right and y increasing as it goes up.

A variation of this system uses the upper left corner of the screen as the origin with the x/y values increasing to the right and down.

One of these coordinate systems will be used to position objects on the screen.

The other piece of information needed to precisely position an object is the origin of the object. That is, where should the system start drawing the object on the screen. Since 2D objects are all contained in boxes we can specify where the object should be drawn from. This allows for precise placement. For example, if the origin of the object is the center of the image, then moving it to a particular position means that the center of the object will be placed on the position. Suppose we have an image that is 20 pixels wide and 20 pixels high. We want to move this object to location 10,40 (x=10 and y=40). Then, when the image is drawn the center is at this location:

If we wanted to move this image next to some other image then we would have to calculate the origin based on the image size.

Position and Vector

In a 2D game, once we know the coordinate system and the image size, we can specify the location of an object by using two numbers, the X position and the Y position. This is normally written as (X,Y) or (10,10). Vectors are also specified with two numbers in an X,Y pattern, but vectors have a completely different meaning.

Position of Object = (12, 45)

Vector of Object = (5, 8)

Despite looking the same, these two values have complete different meanings. A Position is an absolute value and indicates where on the screen an object is placed. A Vector indicates a movement in both the X and Y directions from the objects current position.

How would you indicate movement of objects in a 2D game during the game loop? Vectors are instructions for the game engine to follow. By telling the game engine to move an object 5 units in the positive X direction and 8 units in the positive Y direction we are indicating how to move an object.

Assuming the position of an object is 10, 10 and the Vector is 3, 5 then: (10,10) → (3, 5) → (13, 15)

This can be read, if the object starts at position (10,10) and is moved by (3,5), it ends up at (13,15).

You can also have negative values in Vectors if you want an object to move in a “backwards” direction.

(10,10) → (3, -2) → (13, 8)

An important concept behind vectors is that there is an implied starting and ending point. The two things that a Vector tells us is a Direction and a Magnitude.

A Direction tells us how to move in the x,y coordinate system. A positive value tells us to move in the positive direction and a negative value tells us to move in the negative direction (based on the coordinate system).

The Magnitude tells us how far we moved. The Distance of a move will prove to be very useful and can be calculated with only the Vector. This is done by using the Pythagorean Theorem.

Assume an object at position (50, 25) and we want to move it to (53, 23). This will means a Vector of (3,-2).

The direction of the vector is (3,-2). You can also come up with the Magnitude, which is the distance from the starting point to the ending point. This is done by using a Pythagoras Calculation.

How did we calculate the distance between the starting and ending points by using the Vector? This is done by using the equation: 5 = √32 + −22

You square the X and Y moves and take the square root. This gives you the distance between two points which is called the Magnitude.

There are a number of calculations you can perform on vectors including adding and subtracting:

V = (12, 4) + (-3, 2) (12 + -3, 4 + 2) V is (9,6)

V = (10, 7) – (2, -3) (10 – 2, 7 - - 3) V is (8, 10) ( - - 3 is a positive 3)

A term you might see when working with Vectors is the term “Scalar”. This refers only to the magnitude of a vector. You can use this to calculate new positions and movements.

Suppose you have an object at position (12, 4) and you wish to move it in the direction (3,2) but you want the distance to be 4 times the normal distance. To make this work and get the new position you multiply the vector by the scalar.

V = (3* 4, 2 * 4)

(12, 8)

The new position in the x/y coordinate system is arrived at by adding this vector to the original position: (12 + 12, 4 + 8) (24, 12)

Normalized Vectors We have learned that a Vector is a pair of numbers that contain a direction and that can be used to derive a magnitude. When a vector contains a pair of numbers, such as (10, 5) it indicates that the movement will be 10 units in the positive X direction and 5 units in the positive Y direction. This vector specifies how many units to move but it also indicates a general direction in percentages. That is the ratio of moving 10 by 5. Calculating this movement percentage is called Normalizing a vector. A normalized vector is a pair of numbers, each of which is less than one. You can create a normalized vector by getting the magnitude of a vector and dividing each value of the Vector by the magnitude.

With a vector of (3,4) the magnitude is 5

푋 = √32 + 42 Dividing each vector by the magnitude gives: 3 4 ( , ) 5 5 or (.6 , .8 )

If the vector was (-3, 4) then the normalized vector would be:

(-.6, .8)

To use a normalized vector you would multiply the normalized vector by the movement speed and add it to the regular vector.

A. Start at some position (10, -5). B. Calculate a normalized vector. C. Multiply the normalized vector with the speed the character should move times the delta time. D. Add the value to the starting vector E. Go back to A.