Introduction

Who Is This Book For?

Math and Physics for Programmers, Second Edition, is for many different people. If you thumb through the pages, you can gain a sense of the topics covered and the presentation of the material. The discussions of mathematics, physics, biology, and other topics are meant to help you understand how such topics can assist you as a programmer who is interested in developing games. But even if your interests don’t center on games, it is hoped that you can find that when the material is presented in the context of games, it is more fun and easier to understand.

Generally, to benefit from this book, it will help a great deal if you know how to program in a language such as Java, JavaScript, Python, ActionScript, Lingo, C/C++, Java, Visual Basic, Perl, or C#. These are some of the languages, in any event, the authors have played with over the years. This can even extend to the scripting languages for MATLAB, Mathematica, or Maple. Any grounding will do, since the code examples in the book are presented as pseudocode, allowing you to put what you learn to work in contexts that are familiar to you.

This book is for you if you are curious about how mathematics can be applied to programming projects. While the focus of the book is on the use of mathematics in game contexts, it remains that what applies to games applies to a multitude of other things as well. Generally, to prime the pump a little, have you considered how a program based on neural nets or genetics might be used for a game? How about maze theory and tessellations? And what about linear algebra and understanding how vectors and matrices can be used to create and manipulate graphical objects? On the other hand, how do you get from basic arithmetic learned in middle or high school to a level of math that allows you to work with 3-D objects in sophisticated programming contexts? Or suppose you went through a lot of this at one point and are just looking for a refresher? If any of these questions catches your attention, then it is likely that this book is worth considering.

What Is This Book About?

Obviously, as the title suggests, this book is about math and physics for programming. It is not a textbook, however. Nor does it provide step-by-step procedures (in most cases) for doing the thing it presents. Certainly, in many instances, you can copy the pseudocode examples and alter them for your own purposes. But the book has been written to be read, to serve as a guide to what can be done with mathematics and physics in the context of game development. From this, it is hoped that you will gain a sense of the scope and depth of the possibilities.

The topics covered in the book fall into four broad categories:

  • Essential topics. You are likely to encounter these topics in everyday life. They are the foundation concepts underlying the whole of mathematics. Most of these are covered in Part I of the book. They concern the basic principles of numbers, algebra, and geometry. These are also dealt with in the first chapters of Parts II, III, and IV, where the basics of physics and 3-D mathematics are discussed. As much as possible, essential topics are covered in depth, using derivations and code samples.

  • Advanced topics. These are drawn from complex areas of mathematics and physics. While you are less likely to meet them on a day-to-day basis, they are necessary for a complete understanding of the subject and to solve other problems. Such topics include calculus (Chapter 6), complex physics concepts (Part III), and detailed 3-D math, especially the workings of 3-D renderers (Part IV). Advanced topics are covered in a more discursive fashion, with explanations of the principles and terminology, a few examples, and some derivations of important results.

  • Applied topics. With applied topics, you will look at how essential concepts can be applied in more complex situations. Portions of the book that involve applied topics include most of Part II, as well as one or two chapters in Parts III and IV. In these chapters, you’ll look at a few particular examples of the topic, exploring them in depth, with detailed code samples and derivations of mathematical results, but we will not attempt to cover the whole topic. However, the examples given should help you tackle similar problems yourself. You will try a range of different methods for achieving the same goals.

  • Extension topics. These are more advanced or obscure areas of the subject, particularly those that can be used to illustrate some broader principles. Every effort is made to present things in a simple and engaging manner. Most of these crop up in Part V, although some of Parts III and IV fit into this category, too. The aim is to make you comfortable with the essential concepts and terminology of the topic without worrying too much about the fine detail. A full explanation of any one of these topics would require another book—or many books—but after reading through the chapters, you should at least know what it is you are trying to achieve and where to look for the solutions. These chapters have fewer code examples and equations, with more of an emphasis on verbal explanations and images.

Naturally, these categories overlap to some extent, and every chapter has an element of all of them, but this explanation should make the overall philosophy of the book clearer. The principal aim of the book is to leave you understanding the mathematical principles and concepts involved in programming—not to know everything there is to know on any one subject. It is hoped that by reading this book you will understand the concepts well enough to know what to look for when you encounter some new problem. In this respect, it is a book of mathematical techniques, physical principles, examples of the math in action, and general concepts and terminology.

Each chapter ends with one or more programming exercises for you to try yourself. You’ll find many examples of the code on the book’s companion website. For the most part, the examples are available in chapter files, but you’ll find the longer examples under their specific names. Everything has been placed and indexed in HTML files, so you should find accessing examples to be an easy, intuitive activity.

How Should I Use This Book?

Math and Physics for Programmers is presented as a cumulative sequence. Most chapters make reference to topics covered earlier. In this respect, it is worthwhile to at least quickly skim over the whole book. Even if you are fairly confident with basic mathematics and physics, there are likely to be some parts of the early sections that you are unsure about.

Mathematics is a practical subject, and unless you have worked through a problem, you are unlikely to really understand it. Toward this end, it is worthwhile to at least take on the challenges listed in the exercises for the chapters. In fact, don’t stop with the exercises alone. Think about how you might generalize and incorporate your work into a larger project. Can you think of a game that uses the principles you have learned? Can you think how to improve an earlier game by incorporating these new elements? As a programmer, you are in a much better position to really come to grips with the principles behind what you are learning.

Finally, don’t get scared. People are unreasonably afraid of some of these subjects even though they are perfectly able to tackle far more complex tasks (like arguing about international politics down at the pub). If you don’t understand something, first read it again. If it still doesn’t make sense, then think about why—is there some terminology you don’t understand? See if the glossary refreshes your memory, or find an earlier chapter where the concept is explained. Try a practical example, draw a little diagram, or see if you can rephrase it. If all else fails, skip over it, and see if a later explanation throws some light on it. But don’t do that too often—that is how problems start.

About the Pseudocode

The aim of this book is to introduce you to essential mathematics, not to teach good programming style. You don’t need to get bogged down in the specifics of programming languages, implementation, or user interfaces, or (except very briefly) in graphics or sound. So you will find few references to variables, objects, arrays, or sprites. Instead, you will look at particular functions and algorithms, which perform particular tasks. Because they are entirely mathematical, they should be easy to implement in whatever language you are using and can be treated as black boxes that just do what you want. This does not mean that the same mathematics could not be implemented better or faster by integrating it with the rest of your code or by making use of shortcuts in the language you use. In fact, in the vast majority of cases, this is very possible.

To avoid worrying about programming style, all code examples in this book are presented as pseudocode. This term means different things to different people, but in this book it designates fairly detailed versions of the code, written in a human-readable language. The pseudocode is essentially a programming language written for a human interpreter rather than a computer.

Occasionally, some parts of the code are left out in order to concentrate on the important points. The sections that are left out are indicated by a double bar (||). There are also frequent comments, preceded by a double slash (//).

Most of the code examples on the book’s companion website were composed in Lingo. This is a fairly flexible, high-level, uncompiled language that employs untyped variables and can be used in both an object-oriented and a procedural style.

Whenever it is possible, we avoid specifics about variables, arrays, and so on, but they are sometimes necessary. The book uses the Lingo convention of saying the first element of an array (list) A is A[1], rather than A[0] as most other languages have it.

How to Locate the Code Samples

As noted already, the code samples are in many instances pseudocode, which means, of course, that you cannot expect to copy and paste them into a compiler and have them run.

Most of the code samples are presented in HTML documents. They are sorted in two ways:

  • In chapter folders. If the code pertains to themes presented in one chapter only, then you will find it by accessing one or more HTML files stored in a folder with the same name as the chapter. In many instances, you will find a file named the same as a chapter, such as Chapter 7.html. In other instances, however, you will find differently named files, and you can use your browser to inspect these for programs that pertain to the chapter.

  • From a main index. A second approach is likely to prove more useful and is recommended above the first. In a folder called Indexed Programs, access the index.html file. This file will help you to access specific chapter themes and weave together the variant themes that are presented throughout the book. In this way, you can both reinforce your understanding of specific themes presented in the book and gain a sense of how the themes are tied together.

Since the index.html file might prove a little scant when you first open it, here is a list of the links you will see and where they will connect you:

  • Castlib tileScroller. Connects you with a number of programs relating to the later chapters of the book, which deal with tiling (Chapter 23), searches, and optimization.

  • Castlib math. Gives you access to programs relating to the first 18 chapters of the book but with specific reference to the math operations in these chapters.

  • Castlib mazes. Links you to programs relating to Chapters 23, 24, and 25, which deal with mazes and AI.

  • Castlib pool. Provides access to files that relate to the pool game, which is first presented in Chapter 11 but is referred to in discussions in several chapters that follow.

  • Castlib simple3D. Connects you to programs that relate to 3-D graphics. This discussion formally begins in Chapter 17, but you then return to it in different contexts for all subsequent chapters.

Note that the term Castlib is used to group items. A cast refers to a group of objects in a game that are related to common tasks. In this context, use of the term is more or less a way of saying that sample code is grouped according to themes. Get used to opening the index.html file as you read. After a time, you will acquaint yourself with the vocabulary items and visit code samples from various chapters as you go.

Errors, Omissions, and Comments

In a book of this length and scope, it’s inevitable that errors have been missed. Most of these, it is hoped, will be minor, but it’s possible that some will be more dramatic. In either case, comments can be submitted to the publisher and apologies, from the start, are offered.

Companion Website Downloads

You may download the companion code sample files from www.courseptr.com/downloads. Please note that you will be redirected to our Cengage Learning site.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.111.183