Physics for Flash Games, Animation, and Simulations

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

Physics for Flash Games, Animation, and Simulations teaches ActionScript programmers how to incorporate real physics into their Flash animations, games, user interfaces, and simulations.  Introduces Flash physics in an accurate, but approachable way, covering what is required to produce physically realistic simulations (as opposed to animations that look roughly right) Packed full of practical examples of how physics can be applied to your own games and applications Addresses the diverse needs of game developers, animators, artists, and e-learning developers The book assumes a basic knowledge of ActionScript and Flash. However, no previous knowledge of physics is required—only some very basic math skills. The authors present everything from basic principles to advanced concepts, so you'll be able to follow the logic and easily adapt the principles to your own applications. The book builds on your physics knowledge, enabling you to create not only visual effects, but also more complex models and simulations. What you’ll learn Basic math and physics you'll need to incorporate realism into your games, animations and simulations How to incorporate a wide range of forces, including environmental forces such as gravity and friction, and forces due to fluids, such as drag and upthrust How to build a number of realistic simulations, like submarines and flight simulators How to model particle systems and use them for generative art and to create effects, such as smoke   Numerical subtleties, including accuracy and stability of integration schemes and handling boundary conditions properly; and how and when to use approximations and analytical solutions Who this book is for Flash developers interested in incorporating real physics into their games, animations, simulations or generative art projects. Table of Contents Introduction to Physics Programming Selected ActionScript 3.0 Topics Some Math Background Basic Physics Concepts The Laws Governing Motion Gravity, Orbits, and Rockets Contact and Fluid Forces Restoring Forces: Springs and Oscillations Centripetal Forces: Rotational Motion Long-Range Forces Collisions Particle Systems Extended Objects Numerical Integration Schemes Other Technical Issues Simulation Projects

Author(s): Dev Ramtal, Adrian Dobre
Edition: 1
Publisher: friendsofED
Year: 2011

Language: English
Pages: 548
Tags: Библиотека;Компьютерная литература;Flash / Action Script;

Cover......Page 1
Contents at a Glance......Page 3
Contents......Page 531
About the Authors......Page 542
About the Technical Reviewer......Page 543
About the Cover Image Artist......Page 544
Acknowledgments......Page 545
What this book will (and won’t) teach you......Page 546
Source code and examples......Page 547
Why model real physics?......Page 6
Generating art from code......Page 7
Can’t I just use a physics engine?......Page 8
The laws can be written as math equations......Page 9
Programming physics......Page 10
Hence, they can be readily coded up!......Page 11
A simple example......Page 12
Coding up a bouncing ball in 2D......Page 13
Summary......Page 15
Selected ActionScript 3.0 Topics......Page 16
Classes and objects......Page 17
Structure of an AS3.0 class......Page 18
Properties......Page 19
Static methods and properties......Page 20
Variables and constants......Page 21
Data types......Page 22
Operators......Page 25
Math......Page 26
Logic......Page 27
Loops......Page 28
Event listeners and handlers......Page 30
Drag and drop......Page 31
2D coordinates......Page 32
3D in Flash......Page 33
Drawing lines and curves......Page 35
Creating fills and gradients......Page 36
Example: Bouncing ball in a box......Page 37
Using the built-in frame stepping as a clock......Page 39
Using the Timer class......Page 40
Using getTimer() to compute elapsed time......Page 41
Collision detection......Page 43
Distance-based collision detection......Page 44
Summary......Page 46
Some Math Background......Page 47
Building a plotter: the Graph class......Page 48
Plotting functions using the Graph class......Page 49
Straight lines......Page 50
Polynomial curves......Page 51
Things that grow and decay: exponential and log functions......Page 52
Making an object move along a curve......Page 54
Distance between two points......Page 61
Degrees and radians......Page 62
The sine function......Page 63
The tangent function......Page 65
The inverse trig functions......Page 66
Using trig functions for animation......Page 67
What are vectors?......Page 72
Vectors and scalars......Page 73
Adding and subtracting vectors......Page 74
Resolving vectors: vector components......Page 75
Multiplying vectors: Scalar or dot product......Page 78
Multiplying vectors: Vector or cross product......Page 79
Building a Vector class with vector algebra......Page 81
Simple calculus ideas......Page 82
Slope of a line: gradient......Page 83
Rates of change: derivatives......Page 84
Doing sums: integrals......Page 89
Summary......Page 91
Basic Physics Concepts......Page 92
Scientific notation......Page 93
What is a particle?......Page 94
Building a Particle class......Page 95
Moving particles: the Mover class......Page 99
Extending the Particle class......Page 101
Describing motion: kinematics......Page 104
Concepts: displacement, velocity, speed, acceleration......Page 105
Combining vector quantities......Page 108
Equations of motion for uniform acceleration......Page 109
Applying the equations to projectile motion......Page 111
Predicting motion: forces and dynamics......Page 115
The relationship between force, mass, and acceleration......Page 116
Types of forces......Page 117
Combining forces: force diagrams and resultant force......Page 118
Forces in equilibrium......Page 119
An example: object falling under gravity and drag......Page 120
Energy concepts......Page 123
The notion of work in physics......Page 124
The capacity to do work: energy......Page 125
Energy transfer, conversion, and conservation......Page 126
Power......Page 127
Example: a rudimentary “car” simulation......Page 128
Summary......Page 132
The Laws Governing Motion......Page 134
Newton’s first law of motion (N1)......Page 135
Newton’s second law of motion (N2)......Page 136
Newton’s third law of motion (N3)......Page 137
The Forcer class......Page 138
The Forces class......Page 140
A simple example: projectile with drag......Page 141
A more complicated example: floating ball......Page 143
Taking a deeper look at F = ma......Page 145
An example: Fall under gravity and drag revisited......Page 146
The principle of energy conservation......Page 147
An example: Energy changes in a projectile......Page 148
The principle of momentum conservation......Page 151
Example: 1D elastic collision between two particles......Page 153
Summary......Page 155
Gravity......Page 156
Newton’s universal law of gravitation......Page 157
Creating the gravity function......Page 158
Orbits......Page 160
Orbiter class......Page 161
Escape velocity......Page 164
Two-body motion......Page 165
The force of gravity near the Earth’s surface......Page 168
Variation of gravity with height......Page 169
Gravity on other celestial bodies......Page 171
Modeling the thrust of a rocket......Page 172
Building a rocket simulation......Page 173
Summary......Page 179
Contact and Fluid Forces......Page 180
Normal contact forces......Page 181
Friction......Page 182
Example: Sliding down a slope......Page 184
The meaning of pressure......Page 190
Density......Page 191
Variation of pressure with depth in a fluid......Page 192
Upthrust (buoyancy)......Page 193
Archimedes’s Principle......Page 194
Submerged objects......Page 195
Example: Balloon......Page 196
Drag......Page 198
Drag law for low velocities......Page 199
Drag law for high velocities......Page 200
Which drag law should I use?......Page 201
Example: Floating ball......Page 202
Terminal velocity......Page 206
Example: Parachute......Page 208
Lift......Page 211
Example: An airplane......Page 212
Force due to the wind......Page 215
Example: Air bubbles in a steady wind......Page 216
Modeling turbulence......Page 218
Summary......Page 219
Springs and oscillations: Basic concepts......Page 220
Restoring force, damping, and forcing......Page 221
Hooke’s law......Page 222
Creating a basic oscillator......Page 223
Simple harmonic motion......Page 225
Oscillations and numerical accuracy......Page 228
Damping force......Page 232
The effect of damping on oscillations......Page 233
Analytical solutions for oscillations with damping......Page 235
Driving forces......Page 236
Example: A periodic driving force......Page 237
Example: A random driving force......Page 238
Gravity as a driving force: bungee jumping......Page 239
Example: Driving force by user interaction......Page 242
Coupled oscillators: Multiple springs and objects......Page 244
Example: A chain of objects connected by springs......Page 245
Summary......Page 249
Kinematics of uniform circular motion......Page 250
Angular displacement......Page 251
Angular acceleration......Page 252
Period, frequency, and angular velocity......Page 253
Relation between angular velocity and linear velocity......Page 254
Example: A rolling wheel......Page 255
Particles with spin......Page 257
Example: Satellite around a rotating Earth......Page 258
Centripetal acceleration......Page 262
Centripetal force......Page 263
Common misconceptions about centripetal force......Page 264
Example: Revisiting the satellite animation......Page 265
Example: Circular orbits with gravitational force......Page 266
Example: Car moving around a bend......Page 269
Tangential force and acceleration......Page 273
Example: A simple pendulum......Page 274
Summary......Page 278
Particle interactions and force fields......Page 279
From particle interactions to force fields......Page 280
Gravitational field due to a particle......Page 281
Gravity with multiple orbiters......Page 282
Gravity with multiple attractors......Page 284
Particle trajectories in a gravity field......Page 287
Building a simple black hole game......Page 290
Electric charge......Page 297
Coulomb’s law of electrostatics......Page 298
Charged particle attraction and repulsion......Page 299
Electric fields......Page 301
Magnetic fields and forces......Page 304
The Lorentz force law......Page 306
Central forces......Page 308
Gravity with a spring force law?......Page 312
Multiple attractors with different laws of gravity......Page 314
Summary......Page 316
Collisions......Page 318
Collisions and their modeling......Page 319
Elastic bouncing......Page 320
Implementing energy loss due to bouncing......Page 323
Collision detection......Page 324
Repositioning the particle......Page 326
Calculating the new velocity......Page 327
Velocity correction just before collision......Page 329
An example of a ball bouncing off an inclined wall......Page 331
Example of ball bouncing off multiple inclined walls......Page 336
Repositioning the particles......Page 337
Elastic collisions......Page 340
Inelastic collisions......Page 344
Collisions between particles in 2D......Page 346
An example: 2D collisions between two particles......Page 347
Example: multiple particle collisions......Page 350
Example: multiple particle collisions with bouncing......Page 351
Summary......Page 355
Particle Systems......Page 356
Introduction to particle system modeling......Page 357
A simple example: splash effect with particles......Page 358
Creating a particle emitter......Page 361
Creating a smoke effect......Page 364
Creating a fire effect......Page 368
Creating fireworks......Page 370
Particle paths in a force field......Page 376
Building a wormhole......Page 379
Multiple particles under mutual gravity......Page 382
A simple galaxy simulation......Page 386
Summary......Page 391
Rigid bodies......Page 392
Basic concepts of rigid body modeling......Page 393
Modeling rigid bodies......Page 399
Rotational dynamics of rigid bodies......Page 402
Simulating rigid body dynamics......Page 406
Example: a simple wind turbine simulation......Page 409
Example: Rolling down an inclined plane......Page 412
Rigid body collisions and bouncing......Page 419
Example: Simulating a single bouncing block......Page 423
Example: Colliding blocks......Page 427
Mass-spring systems......Page 429
Rope simulation......Page 431
Cloth simulation......Page 435
Summary......Page 437
Numerical Integration Schemes......Page 439
Statement of the problem......Page 440
Characteristics of numerical schemes......Page 442
Types of integration schemes......Page 443
Modifying Forcer to allow for different integration schemes......Page 444
Explicit Euler......Page 445
Semi-implicit Euler......Page 446
Comparing the explicit and semi-implicit Euler schemes......Page 447
Runge-Kutta integration......Page 448
Second-order Runge-Kutta scheme (RK2)......Page 449
Fourth-order Runge-Kutta scheme (RK4)......Page 450
Stability and accuracy of RK2 and RK4 compared with Euler......Page 451
Position Verlet......Page 452
Testing the stability and accuracy of the Verlet schemes......Page 454
Summary......Page 455
Other Technical Issues......Page 456
3D math......Page 457
Creating classes for 3D objects and motion......Page 463
Creating 3D models......Page 465
Example: A rotating cube......Page 466
Integrating with 3D engines......Page 469
A simple example......Page 470
Scaling factors and parameter values......Page 471
Rescaling equations......Page 472
Using Number for calculations......Page 473
Using accurate initial conditions......Page 474
Summary......Page 475
The visual setup......Page 477
Build a submarine......Page 476
The setup code......Page 478
Adding controls and visual effects......Page 479
The full mover code......Page 481
Physics and control mechanisms of aircraft......Page 483
Creating the visual setup......Page 488
Coding up the physics......Page 490
Test flying the simulator......Page 494
Create an accurate solar system model......Page 495
Coding up an appropriate integration scheme......Page 496
Building an idealized single-planet simulation......Page 498
Choosing appropriate scaling factors......Page 500
Obtaining planetary data and initial conditions......Page 501
Basic solar system model......Page 502
Comparing the model results with NASA data......Page 507
Summary......Page 510
A......Page 511
B......Page 512
C......Page 513
D......Page 514
E......Page 515
G......Page 516
L......Page 517
M......Page 518
N......Page 519
O......Page 520
P, Q......Page 521
R......Page 522
S......Page 524
U......Page 526
Y, Z......Page 527