Contents

About the Authors

About the Technical Reviewer

Acknowledgments

Introduction

images Part I: The Basics

images Chapter 1: Introduction to Physics Programming

Why model real physics?

Creating realistic animation effects

Creating realistic games

Building simulations and models

Generating art from code

What is physics?

Everything behaves according to the laws of physics

The laws can be written as math equations

Predicting motion

Programming physics

The difference between animation and simulation

The laws of physics are simple equations

Equations can be readily coded up!

The four steps for programming physics

A simple physics simulation example

The physics of a bouncing ball

Coding up a bouncing ball in 2D

Summary

images Chapter 2: JavaScript and HTML5 Canvas Basics

HTML5, the canvas element, and JavaScript

A minimal HTML5 document

The canvas element

Adding JavaScript

The JavaScript debugging console

JavaScript objects

Objects and properties

Functions and methods

Prototypes, constructors, and inheritance

Static properties and methods

Example: a Ball object

JavaScript frameworks, libraries, and APIs

JavaScript language basics

Variables

Data types

Operators

Math

Logic

Loops

Events and user interaction

Event listeners and handlers

User interaction: keyboard, mouse, and touch events

Drag and drop

The canvas coordinate system

The canvas drawing API

The canvas context

Drawing lines and curves

Creating fills and gradients

Animating using the canvas context

Producing animation using code

Using JavaScript timers

Using requestAnimationFrame()

Using getTime() to compute elapsed time

Precalculating motion

Summary

images Chapter 3: Some Math Background

Coordinates and simple graphs

Building a plotter: the Graph object

Plotting functions using the Graph object

Drawing straight lines

Drawing polynomial curves

Things that grow and decay: exponential and log functions

Making an object move along a curve

Finding the distance between two points

Basic trigonometry

Degrees and radians

The sine function

The cosine function

The tangent function

The inverse trig functions

Using trig functions for animation

Vectors and basic vector algebra

What are vectors?

Vectors vs. scalars

Adding and subtracting vectors

Resolving vectors: vector components

Multiplying vectors: Scalar or dot product

Multiplying vectors: Vector or cross product

Building a Vector object with vector algebra

Simple calculus ideas

Slope of a line: gradient

Rates of change: derivatives

Doing sums: integrals

Summary

images Chapter 4: Basic Physics Concepts

General physics concepts and notation

Physical quantities and units

Scientific notation

Things: particles and other objects in physics

What is a particle?

Particle properties

Building a Particle object

Extending the Particle object

Moving particles

Describing motion: kinematics

Concepts: displacement, velocity, speed, acceleration

Combining vector quantities

Describing motion using graphs

Equations of motion for uniform acceleration

Example: Applying the equations to projectile motion

More motion-related concepts: inertia, mass, and momentum

Predicting motion: forces and dynamics

The cause of motion: forces

The relationship between force, mass, and acceleration

Types of forces

Combining forces: force diagrams and resultant force

Forces in equilibrium

Example: Object falling under gravity and drag

Energy concepts

The notion of work in physics

The capacity to do work: energy

Energy transfer, conversion, and conservation

Potential and kinetic energy

Power

Example: A rudimentary “car” simulation

Summary

images Part II: Particles, Forces, and Motion

images Chapter 5: The Laws Governing Motion

Newton’s laws of motion

Newton’s first law of motion (N1)

Newton’s second law of motion (N2)

Newton’s third law of motion (N3)

Applying Newton’s laws

General method for applying F = ma

Coding up motion under any force

The Forces object

A simple example: projectile with drag

A more complicated example: floating ball

Newton’s second law as a differential equation

Taking a deeper look at F = ma

Example: Falling under gravity and drag revisited

The principle of energy conservation

Conservation of mechanical energy

Example: Energy changes in a projectile

The principle of momentum conservation

Example: 1D elastic collision between two particles

Laws governing rotational motion

Summary

images Chapter 6: Gravity, Orbits, and Rockets

Gravity

Gravity, weight, and mass

Newton’s universal law of gravitation

Creating the gravity function

Orbits

The orbits code

Escape velocity

Two-body motion

Local gravity

The force of gravity near the Earth’s surface

Variation of gravity with height

Gravity on other celestial bodies

Rockets

It is rocket science!

Modeling the thrust of a rocket

Building a rocket simulation

Summary

images Chapter 7: Contact and Fluid Forces

Contact forces

Normal contact forces

Tension and compression

Friction

Example: Sliding down a slope

Pressure

The meaning of pressure

Density

Variation of pressure with depth in a fluid

Static and dynamic pressure

Upthrust (buoyancy)

Archimedes’ Principle

Apparent weight

Submerged objects

Floating objects

Example: Balloon

Drag

Drag law for low velocities

Drag law for high velocities

Which drag law should I use?

Adding drag to the balloon simulation

Example: Floating ball

Terminal velocity

Example: Parachute

Lift

Lift coefficients

Example: An airplane

Wind and turbulence

Force due to the wind

Wind and drag

Steady and turbulent flow

Example: Air bubbles in a steady wind

Modeling turbulence

Summary

images Chapter 8: Restoring Forces: Springs and Oscillations

Springs and oscillations: Basic concepts

Spring-like motion

Restoring force, damping, and forcing

Hooke’s law

Free oscillations

The spring force function

Creating a basic oscillator

Simple harmonic motion

Oscillations and numerical accuracy

Damped oscillations

Damping force

The effect of damping on oscillations

Analytical solutions for oscillations with damping

Forced oscillations

Driving forces

Example: A periodic driving force

Example: A random driving force

Gravity as a driving force: bungee jumping

Example: Driving force by user interaction

Coupled oscillators: Multiple springs and objects

Example: A chain of objects connected by springs

Summary

images Chapter 9: Centripetal Forces: Rotational Motion

Kinematics of uniform circular motion

Angular displacement

Angular velocity

Angular acceleration

Period, frequency, and angular velocity

The relationship between angular velocity and linear velocity

Example: A rolling wheel

Example: Satellite around a rotating Earth

Centripetal acceleration and centripetal force

Centripetal acceleration

Centripetal acceleration, velocity, and angular velocity

Centripetal force

Common misconceptions about centripetal force

Example: Revisiting the satellite animation

Example: Circular orbits with gravitational force

Example: Car moving around a bend

Non-uniform circular motion

Tangential force and acceleration

Example: A simple pendulum

Summary

images Chapter 10: Long-Range Forces

Particle interactions and force fields

Interaction at a distance

From particle interactions to force fields

Newtonian gravitation

Gravitational field due to a particle

Gravity with multiple orbiters

Gravity with multiple attractors

Particle trajectories in a gravity field

Building a simple black hole game

Electrostatic force

Electric charge

Coulomb’s law of electrostatics

Charged particle attraction and repulsion

Electric fields

Electromagnetic force

Magnetic fields and forces

The Lorentz force law

Other force laws

Central forces

Gravity with a spring force law?

Multiple attractors with different laws of gravity

Summary

images Part III: Multi-particle and Extended Systems

images Chapter 11: Collisions

Collisions and their modeling

Bouncing off horizontal or vertical walls

Elastic bouncing

Inelastic bouncing

Bouncing off inclined walls

Collision detection with an inclined wall

Repositioning the particle

Calculating the new velocity

Velocity correction just before collision

Example: a ball bouncing off an inclined wall

Example: Ball bouncing off multiple inclined walls

Collisions between particles in 1D

Repositioning the particles

Elastic collisions

Inelastic collisions

Collisions between particles in 2D

Example: 2D collisions between two particles

Example: multiple particle collisions

Example: multiple particle collisions with bouncing

Summary

images Chapter 12: Particle Systems

Introduction to particle system modeling

Creating animated effects using particles

A simple example: splash effect with particles

Creating a particle emitter

Creating a smoke effect

Creating a fire effect

Creating fireworks

Particle animations with long-range forces

Particle paths in a force field

Building a wormhole

Interacting particle systems

Multiple particles under mutual gravity

A simple galaxy simulation

Summary

images Chapter 13: Extended Objects

Rigid bodies

Basic concepts of rigid body modeling

Modeling rigid bodies

Rotational dynamics of rigid bodies

Simulating rigid body dynamics

Example: a simple wind turbine simulation

Example: Rolling down an inclined plane

Rigid body collisions and bouncing

Example: Simulating a single bouncing block

Example: Colliding blocks

Deformable bodies

Mass-spring systems

Rope simulation

Cloth simulation

Summary

images Part IV: Building More Complex Simulations

images Chapter 14: Numerical Integration Schemes, Accuracy, and Scaling

General principles

Statement of the problem

Characteristics of numerical schemes

Types of integration schemes

A simple example to demonstrate different integration schemes

Euler integration

Explicit Euler

Implicit Euler

Semi-implicit Euler

Comparing the explicit and semi-implicit Euler schemes

Why use Euler and why not?

Runge-Kutta integration

Second-order Runge-Kutta scheme (RK2)

Fourth-order Runge-Kutta scheme (RK4)

Stability and accuracy of RK2 and RK4 compared with Euler

Verlet integration

The Position Verlet Method

The Velocity Verlet Method

Testing the stability and accuracy of the Verlet schemes

Tips for achieving accuracy

Choosing an appropriate integration scheme

Using an appropriate timestep

Using accurate initial conditions

Dealing with boundaries carefully

Building scale models

Scaling for realism

A simple example

Choosing units

Scaling factors and parameter values

Rescaling equations

Summary

images Chapter 15: Doing Physics in 3D

3D physics and math

3D versus 2D physics

Vectors in 3D

Forces in 3D

Matrices and rotation

Quaternions

Rotational dynamics in 3D

3D rendering: Introducing WebGL and three.js

Canvas, WebGL, and WebGL frameworks

A quick three.js primer

Simulating particle motion in 3D

A bouncing ball simulation in 3D

A rotating Earth animation

Forces: gravity and orbits in 3D

Simulating rigid body motion in 3D

A rotating cube animation

Applying a force to the rotating cube

Rotating the cube about an arbitrary axis

Summary

images Chapter 16: Simulation Projects

Building a submarine

Brief review of the physics

The visual setup

The setup code

The basic motion code

Adding controls and visual effects

The full code

It’s your turn

Building a flight simulator

Physics and control mechanisms of aircraft

What we will create

Creating the visual setup

Coding up the physics

Implementing the controls

Displaying flight information

Test flying the simulator

It’s your turn

Creating an accurate solar system model

What we will create

The physics

Coding up an appropriate integration scheme

Building an idealized single-planet simulation

Choosing appropriate scaling factors

Obtaining planetary data and initial conditions

Creating a basic solar system model

Incorporating accurate initial conditions

Comparing the model results with NASA data

Animating the solar system simulation

It’s your turn

Summary

Index

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

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