Youve picked up this book because you are interested in implementing physics in your programming projects. But why would you want to do that? What can it do for you? And how difficult will it be? This chapter will provide answers to these questions.
Why model real physics?
There are a number of reasons why you might be interested in modeling physics using JavaScript. Here are some of the most common:
To create realistic animation effects
To create realistic games
To build simulations and models
To generate art from code
Let us look at each in turn.
Creating realistic animation effects
Thanks to the HTML5 canvas element, it is now possible to create animations without the need for plug-ins such as Flash. With a little JavaScript and some familiarity with physics, it is also possible to make animations that look and behave like the real thing. For example, suppose you are animating a scene in which someone kicks a ball and it bounces off the ground. You could try to create an animation that mimics the balls behavior, but however hard you might try, it would probably look less than realistic. With just a little bit of coding and some knowledge of elementary physics, you could produce a far more realistic animation. And if, like the authors, you are programmers rather than designers, you might even find it easier! Well show you just how easy it can be in the example at the end of this chapter.
Creating realistic games
Web-based games are extremely popular. As the capabilities of modern web browsers continue to improve, better and more powerful games can be built. Hardware acceleration and 3D support are just two of the emerging developments that have the potential to improve the online gaming user experience dramatically. But apart from performance and appearance, it is equally important for games to feel and look realistic. If a player throws a ball, it should fall according to the law of gravity; if a player fires a torpedo underwater, it should move differently from a ball falling in air. In other words, your game needs to incorporate real physics.
How do you build physics awareness into your games? This book will show you how.
Building simulations and models
A computer simulation or computer model is a program that attempts to imitate certain key aspects of a physical system. Simulations vary in completeness or accuracy, depending on purpose and resources. Lets take a flight simulator program as an example. We would expect a flight simulator designed for training pilots to be much more comprehensive and accurate than one designed for a game. Simulations are extremely common in e-learning, training, and scientific research. In the final chapter of this book, youll build simulationsnamely a submarine, a basic flight simulator, and a model of the solar system. In fact, many of the coded examples throughout the book are simulations, even if generally simpler.
Generating art from code
Generative art has gained popularity in recent years. A lot of fun can be had with some basic physicsfor example, elaborate visual effects and motions can be produced using particles (small graphic objects that you can create and animate with code) and different kinds of forces. These effects can include realistic-looking animation such as smoke and fire, as well as more abstract examples of generative art that can be created using a mixture of algorithms, randomness, and user interaction. Adding some physics in the mix can result in enhanced realism and/or richer effects.
We will explore the world of generative art and provide additional tools and algorithms that can be used to create original and interesting effects such as particle trajectories in complex force fields.
What is physics?
Physics is the most fundamental of the sciences. In a broad sense, physics is the study of the natural laws that govern how things behave. More specifically, it concerns itself with space, time, and matter (defined as any stuff that exists in space and time). One aspect of physics is to formulate general laws that govern the behavior of matter, its interactions, and its motion in space and time. Another aspect is to use these laws to predict the way specific things move and interactfor example, the prediction of eclipses from the laws of gravity or how airplanes are able to fly from the laws of aerodynamics.
Physics is a vast subject, and in a book of this nature we cannot do more than scratch the surface. Fortunately, most of the physics that you will probably need to know falls within a branch known as mechanics , which is one of the easiest to understand. Mechanics governs the way in which objects move and how that motion is influenced by effects in the environment. Because most games and animations include motion, mechanics is clearly of relevance in developing algorithms for making objects behave realistically in code.
Everything behaves according to the laws of physics
Without getting too philosophical, it is fair to say that the laws of physics are truly universal, as far as physicists have been able to observe. What this means is that everything must behave according to physics. This is different from say, the laws of biology, which pertain only to living things. A stone thrown in the air, a planet orbiting the Sun, the workings of the human body, and the operation and motion of a man-made machine must all obey the laws of physics. Moreover, many seemingly diverse phenomena are governed by the same subset of laws. In other words, a single law or group of laws can explain many types of observed facts or patterns of behavior in the physical world. For example, a falling stone and a planet orbiting the Sun both obey the laws of gravity. Another example is that all electrical, magnetic, and radiation phenomena (such as light and radio waves) are governed by the laws of electromagnetism.
The laws can be written as math equations
The great thing is that the laws of physics can be written as mathematical equations. Okay, that may not sound too great if you dont like math! But the point here is that for a law to be useful, it has to be made precise. And math equations are as precise as anything can be. There is no possible ambiguity in how to apply a law that is expressed mathematically, in contrast with the laws that are fought over in courtrooms! Second, this means that centuries of developments in mathematics prove to be applicable to physics, making it possible to solve many physics problems. Third, and what is of most relevance for us: math equations are readily convertible into code.