Introduction

We hope you will enjoy reading—and using—this book as much as we enjoyed writing it. Creating physics-based simulations is a lot of fun—there is great satisfaction in writing a few lines of code and then seeing them bring inanimate objects to life, behaving as they do in the real world! This is a striking demonstration that the natural world is governed by simple laws, and that we have at least an approximate grasp of those laws. The ability to replicate the real world in a computer program gives us great power as programmers. The aim of this book is to provide you with tools that will make you feel that awesome sense of power. Whether you want to build convincing animations, games that behave realistically, or accurate simulators, you will find herein plenty of tools, examples, and ideas that should be of help.

This book is based closely on our earlier book, Physics for Flash Games, Animation, and Simulations (Apress, 2011). Although the physics content has not changed, the numerous examples in the book have been rewritten from scratch in JavaScript for rendering in an HTML5 canvas. The last two chapters also include some WebGL examples, with the help of the three.js JavaScript library. All the examples are designed for use in a web browser. Although we don’t give any examples of mobile apps, the examples given can be easily adapted for mobile devices.

While the popularity of JavaScript is on the rise, there is currently a lack of resources for creating physics-based animation using JavaScript. The excellent book by Billy Lamberta and Keith Peters, Foundation HTML5 Animation with JavaScript (Apress, 2011), contains good coverage of physics-related topics and provides a great introduction to the subject. It seemed to us that there was also a need for a book that explored physics further and in greater depth, and that catered to more demanding applications such as accurate simulators or more complex game programming. After all, there are several “game physics” books written for other programming languages, such as C++ and Java, so why not JavaScript? This book is meant to fill that gap.

We should make it clear at the outset that this is primarily a book about applying physics to your coding projects; hence we tend to focus less on producing attractive visual effects and more on modeling real physics. Some of the animations in the book may not be very pretty or smooth from a visual perspective, but they do contain a lot of physics you can apply! By the same token, there is little emphasis on writing elegant code—rather we prefer to keep the coding simple and clean, so that the physics content may not be obscured by over-clever programming. The approach we adopt attempts to make serious physics accessible. But although we don’t shy away from going into technical information, with all of the accompanying math, we hope to have done so in a way that is simple and straightforward.

Inevitably, because of space and time restrictions, there are topics that we were not able to include or that we didn’t explore in detail. Nevertheless, the book covers an awful lot of ground, taking you from coding a simple bouncing ball animation in a few lines of code in the first chapter to a highly accurate simulation of the solar system in the final chapter. We hope you enjoy the journey in between!

What this book will (and won’t) teach you

Physics for JavaScript Games, Animation, and Simulations teaches you how to incorporate physics into your programming. It does not teach you how to program. It does not teach you JavaScript. We assume that you have at least some programming experience, preferably with JavaScript (or a similar programming language). And although this book teaches you how to implement physics into your games (as well as other projects), it is not about game programming per se.

We do not assume any previous knowledge of physics, and we assume only basic school-level math knowledge. One chapter is dedicated to explaining some of the more difficult math concepts and tools that you are likely to need in the book. All the physics concepts and knowledge you will need are explained in a self-contained way. Numerous applications are given throughout the book with full source code to illustrate the application of the principles learned.

Overview of this book

This book is divided into four parts:

Part I: “The Basics” (Chapters 14) introduces the necessary background in basic math and physics concepts upon which the rest of the book builds. For completeness, it also covers selected topics in JavaScript that are most pertinent to physics programming.

Part II: “Particles, Forces, and Motion” (Chapters 510) begins by formulating the laws of physics that govern the motion of particles under any type of force (Chapter 5). The next five chapters then apply those laws to make objects move under the action of a variety of forces including gravity, friction, drag, buoyancy, wind, springs, central forces, and many more. The main focus in this section is on simulating the motion of individual particles and other simple objects.

In Part III: “Multi-Particle and Extended Systems” (Chapters 1113), we show you how to model more complicated systems, including multiple interacting particles and extended objects. In these systems the constituent particles and objects may not simply co-exist but also interact with one another, mutually influencing their motion. These interactions include collisions, short-range forces, and long-range forces. This part includes a discussion of particle systems, rigid bodies, and deformable bodies.

Part IV: “Building More Complex Simulations” (Chapters 1416) is devoted to building more complex simulations, where accuracy and/or realism is especially important, not just visual effects. This part includes a discussion of integration schemes and other numerical and technical issues such as scale modeling and 3D. Part IV ends with a chapter that includes some example simulation projects, which the reader is encouraged to build upon.

Source code and examples

All the code for this book can be downloaded from the book’s page on the Apress website: www.apress.com. An up-to-date version of the code will also be maintained on github.com.

We encourage you to modify and build upon the example codes given. We also invite you to share your codes, and have set up the following page on our website where you can find and contribute more code and examples: www.physicscodes.com/jsbook.

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

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