Flylib.com

Books Software

 
 
 

3.13 Vectors


3.13 Vectors

On paper, vectors look exactly the same as Cartesian coordinates, as both have an x and a y component. However, vertices represent points in space, while vectors represent direction. A vector simply points in a direction, indicating where something must travel. Vectors are useful for games because they can indicate the direction in which objects are moving.

image from book
Figure 3.26

3.13.1 Vector Magnitude

Magnitude can describe the size of an angle, but here it refers to the length of a vector; that is, the linear distance along the diagonal. Calculating the length of a vector is simple; it is the Pythagorean theorem.

image from book
Figure 3.27

3.13.2 Unit Vector

A vector is said to be normalized when it is a unit vector. A unit vector is a vector whose magnitude is 1. It has no length and indicates only direction.

image from book
Figure 3.28



3.14 Conclusion

This chapter covered a broad range of subjects, none of which were examined in quite the detail any of them deserve. Nonetheless, each of them is valuable to gaming, and I have covered many of them insofar as will be immediately relevant to game programming in order to get started. This chapter marks the end of the discussion of mathematics in this book, and I recommend that, as we progress in programming and game development, the reader returns to these chapters as required in order to recap and understand how mathematics is related to gaming concepts.



Chapter 4: C++ and Programming

Overview

The computer is often represented by analogy to be a brain. Unlike a brain, however, a computer cannot think for itself. Whatever intelligence a computer might have, or appear to have, has been set in motion by human operatives. If a computer makes a decision, it is because it has been instructed to do so. When a user is writing a letter in a word processor or arranging a spreadsheet for accounts and decides to print the document, he or she usually does so using a keyboard shortcut or a button click. When the button is clicked, the document prints. Of course, something in between needs to occur-something, when the button is clicked, needs to instruct the computer to print. In fact, considered on a wider scale, the computer is a brain that follows instructions, and everything it does it has to be instructed to do. These instructions are written by programmers. The process by which they do this is called programming, and the language in which they communicate with a computer is called a programming language. C++ is one of many languages, and the most popular choice for games . This chapter begins our journey into the world of C++. Specifically, this chapter explains the following:

  • Basics of C++

  • Integrated development environments

  • Source and header files

  • Visual Studio

  • Code::Blocks

  • Dev C++



4.1 Programming

Programmers are responsible, directly or indirectly, for all of the software we use. Millions across the globe communicate through email, through online messengers, and via website browsers. If it weren't for programmers, none of this would exist. If there were no programmers, those applications would sit there lifelessly doing nothing because there would be no valid instructions telling them what to do. Each time we send an email or message, we take it for granted that it will be sent and received successfully, and all subsequent emails will continue to do so in the future. Each time we write a letter, book a holiday online, or play the latest game, we tend not to think too deeply about how it works and instead we come to expect that it will work, regardless of how. Most users therefore use software all the time without realizing the intricate extents to which instructions are being fed to the computer and how fundamental those instructions are.

This book focuses on the language of C++ in the context of programming games . It follows that C++ is used to send instructions to the computer to determine how a computer should present a game to the user . Hopefully, if we've done our job properly as programmers, it'll be a game that works. In order for a game to work, we must provide the computer with appropriate instructions. We've said the instructions are called programming, but this is more technically called source code , sometimes abbreviated by programmers to either source or code . Let's say we're making a game where a man runs along shooting baddies and can jump from platform to platform. The computer needs to be told how to accomplish this and it will be told how to do so via source code. For example, if the player wishes to jump, the computer must be told how this is to occur. The figure must be launched into the air and then, under the influence of gravity, it must return to the ground. In short, programming is an ambitious task that can be as challenging as it is fascinating.