Animation and Games Development 242-515, Semester 1, 2014-2015 12. Basic Physics • Objective o show how physics is used in games programming 242-515 AGD: 12. Physics 1 Overview 1. Why Physics? 2. Point-based Physics 3. Rigid Body Physics 4. Other Types of Force 5. The World of the Car 6. JBullet Features 7. A Game Physics Textbook 242-515 AGD: 12. Physics 2 1. Why Physics? • Physics can make game worlds appear more natural / realistic • But... • Often games are not realistic o e.g. cars travelling at 200 mph, double jumps of Mario • Physics engines are computationally expensive o they may slow down the game too much 242-515 AGD: 12. Physics 3 Engines • Commercial o Havok (Havok.com) o Renderware (renderware.com) o NovodeX (novodex.com) • Free o o o o Open Dynamic Engine (ODE) (ode.org) PhysX Bullet (Java version used in jME) Box2D (2D) 242-515 AGD: 12. Physics 4 Types of Physics • Newtonian (this one for games) o Newton’s space and time are absolute • Classical o Einstein's space-time can be “changed” by matter • Quantum o Deterministic causality questioned and multiple localities in a single instance 242-515 AGD: 12. Physics 5 Remember this stuff….. • • • • • • • Time o A measured duration Speed o The distance travelled given some time limit Direction o An indication of travel Velocity o The rate of change of position over time (has speed and direction) Acceleration o The rate of change of velocity Force o Causes objects to accelerate (has direction as well as magnitude) Mass o The weight of an object? (No! weight depends on gravitational pull) o The quantity of matter 242-515 AGD: 12. Physics 6 Newton’s First Law of Motion • Every body (object) continues in its state of rest, or uniform motion in a straight line, unless there is an external force acting on it. • Often called "The Law of Inertia" 242-515 AGD: 12. Physics 7 Newton’s Second Law of Motion • The rate of change of momentum of a body is proportional to the force acting on it, and takes place in the direction of that force. • Simplified – An object’s change in velocity is proportional to an applied force. • An object's mass m, its acceleration a, and the applied force F canbe represented by the formula: F = ma 242-515 AGD: 12. Physics 8 Newton’s 3rd Law of Motion • For every action there is an equal and opposite reaction. • If two objects bump into each other they will react by moving apart. 242-515 AGD: 12. Physics 9 As Cartoons 242-515 AGD: 12. Physics 10 Game Physics • Two main types of Newtonian physics used in games: point-based, rigid body based • Point-based physics o used in particle systems, bullet motion… o a point's mass is located inside the point o a point does not rotate: it has no rotational orientation 242-515 AGD: 12. Physics 11 2. Point-based Physics • Solve Newtonian equations to get the position of a point at time t: o Force applied to the point, F(t), causes acceleration o Acceleration, a(t), causes a change in the point's velocity o Velocity, V(t) causes a change in the point's position 242-515 AGD: 12. Physics 12 Calculation Examples • • • Calculate a new position based on a constant velocity: xt = x0 + (vx * t) yt = y0 + (vy * t) zt = z0 + (vz * t) Velocity is a Vector, (vx, vy, vz), or (vx, vy) in 2D Example, point at (1, 5), velocity of (4, -3) Standard Equations • • • • v=u+at s = ½ t (u + v) v 2 = u2 + 2 a s s = u t + ½ a t2 • • • • u = initial velocity; v = final velocity a = acceleration t = time (seconds) s = distance (meters) 242-515 AGD: 12. Physics 14 Velocity • Velocities are vectors: Vtotal = √(Vx2 + Vy2 + Vz2) for 3D games Vtotal = √(Vx2 + Vy2) for 2D games Acceleration • Calculate a new velocity (vt) based on a constant acceleration: vt = dx/dt = v0 + (a * dt) • Each of these calculations are in one dimension – you must perform similar calculations on y & z axes Forces • Forces are vectors Ftotal = √(Fx2 + Fy2 + Fz2) for 3D games Ftotal = √(Fx2 + Fy2) for 2D games Example: 3D Projectile Motion • Weight of the projectile (a point), W = mg o g: constant acceleration due to gravity (9.81m/s2) • Projectile equations of motion: V(t ) Vinit gt t init 1 2 p(t ) p init Vinit t tinit gt tinit 2 p() is the position function 242-515 AGD: 12. Physics 18 Target Practice V ini t Projectile Launch Position, pinit F = weight = mg Target 3. Rigid Body Physics • The shape (body) has a mass that occupies volume. • We assume that the body never changes shape • The orientation of the body can change over time. 242-515 AGD: 12. Physics 20 Rigid Body Translation • Good news: the maths of rigid body translation is the same as for points, since we can treat the center of mass of the body as a point. • This means we can reuse the maths: oF=ma o v = ∫a dt o x = ∫v dt o momentum is conserved 242-515 AGD: 12. Physics 21 center of mass = point 242-515 AGD: 12. Physics 22 Rotation • A rigid body has an orientation (rotated position in space) • Rotation calculations are complicated o one reason is that the order of rotation operations is important: o x-axis rotation then y-axis rotation y-axis rotation then x-axis rotation ≠ • Rotation is not commutative 242-515 AGD: 12. Physics 23 • When a body can rotate the Newtonian motion equations must be extended. o new equations are needed for rotational (angular) mass, velocity, acceleration, force, momemtum, etc. 242-515 AGD: 12. Physics 24 Angular Velocity, ω • The angular velocity ω describes the speed of rotation and the orientation of the axis about which the rotation occurs. 242-515 AGD: 12. Physics 25 242-515 AGD: 12. Physics 26 Angular Velocity Again • Δθ = change in angular displacement • Δt = time 242-515 AGD: 12. Physics 27 Angular Acceleration, α • Δω = change in angular velocity • Δt = time 242-515 AGD: 12. Physics 28 Applying Force • What happens when you push on a rotating body? • There are two things to consider: translation and rotation. • For translation, we can use F=ma, because the body's center of mass can be treated like a point. • But how does the force affect the body's orientation? 242-515 AGD: 12. Physics 29 Torque, T • Torque is a measure of how much a force acting on an object causes that object to rotate. • T = r x F = r F sin(θ) • Torque is the cross product (x) between the distance vector from the pivot point (O) to the point where force vector F is applied • θ is the angle between r and F 242-515 AGD: 12. Physics 30 Uses for Torque • The same amount of torque can be applied with less force if the distance from the pivot (fulcrum) is increased. 242-515 AGD: 12. Physics 31 Moment of Inertia I • Moment of inertia is the mass property of a rigid body that determines the torque needed for a desired angular acceleration about an axis of rotation. • Moment of inertia depends on the shape of the body and may be different around different axes of rotation. shape effect 242-515 AGD: 12. Physics 32 easy (torque is small) hard (torque is large) axes effect 242-515 AGD: 12. Physics 33 • Another definition: moment of Inertia is an object’s resistance to rotating around an axis 242-515 AGD: 12. Physics 34 Linear Momentum, p • Also called translational momentum: the product of the mass and velocity of an object • p=mv • Linear momentum is a conserved quantity: o if a closed system is not affected by external forces, then its total linear momentum cannot change o useful for calculating velocity change after collisions 242-515 AGD: 12. Physics 35 Angular momentum, L • Angular momentum is the rotational version of linear momentum. o e.g. a tire rolling down a hill has angular momentum • Defined as the cross product of the moment of inertia I and the angular velocity ω. • L=lxω 242-515 AGD: 12. Physics 36 • Angular momentum is a conserved quantity: o if a closed system is not affected by external torque, then its total angular momentum cannot change o useful for calculating rotational change when moments of inertia change 242-515 AGD: 12. Physics L = I1 x ω1 L = I2 x ω2 37 Linear vs. Angular Linear Concept velocity v Angular Version acceleration a angular acceleration α F=ma mass m torque T = r x F' =Iα moment of inertia I momentum p=mv angular momentum L=Ixω 242-515 AGD: 12. Physics angular velocity ω 38 Changing Coordinate Systems • The maths for rigid bodies is simpler if we use a local coordinate system for each body o e.g. place the origin at the centre of mass of the body • But, we also need to transform any global forces into each body's local coordinate system • We also have to transform any local motion back into global coordinates. 242-515 AGD: 12. Physics 39 4. Other Types of Force • • • • • (Virtual) Springs Damping Friction Aerodynamic Drag … 242-515 AGD: 12. Physics 40 (Virtual) Springs • Even if you don't see very many actual springs in a game, there are likely to be many invisible virtual springs at work. • Virtual springs are useful for implementing constraints between objects: o preventing objects overlapping o cloth rendering o character animation 242-515 AGD: 12. Physics 41 Linear Springs Fspring k (l lrest )d Damping Damping describes physical conditions such as viscosity, roughness, etc. Fdamping c((Vep 2 Vep1 ) d )d Static Friction Not Sliding On the Brink Sliding Kinetic Friction • Once static friction is overcome and the object is moving, friction continues to push against the relative motion of the two surfaces. o called kinetic friction 242-515 AGD: 12. Physics 45 5. The World of the Car • Cars exist in an environment that exerts forces. 242-515 AGD: 12. Physics 46 Resistance • A car driving down a road experiences two (main) types of resistance. o Aerodynamic drag, rolling resistance Rtotal Rair Rrolling • Once resistance has been calculated, it is possible to calculate the amount of power a car needs to move. Aerodynamic Drag Mass density of air Projected frontal area of car normal to direction of V Rair (1 / 2)V S pCd 2 Speed of car Drag coefficient: 0.29 – 0.4: sports cars; 0.6 – 0.9: trucks 242-515 AGD: 12. Physics 48 Rolling resistance • Tires rolling on a road experience rolling resistance. This is not friction and has a lot to do with wheel deformation. Simplifying, we can say: Rrolling Cr Coefficient of rolling resistance: Cars ≈ 0.015; trucks ≈ 0.006 – 0.01 Weight of car (assuming four identical wheels) Power • Power is the measure of the amount of work done by a force, or torque, over time. • Mechanical work done by a force is equal to the force * distance an object moves under the action of that force. • Power is usually expressed in units of horsepower o 1 horsepower = 550 ft-lbs/s 242-515 AGD: 12. Physics 50 Horsepower • Horsepower needed to overcome total resistance at a given speed (we are working in feet and lbs): P ( RtotalV ) / 550 horsepower Total resistance corresponding to a car’s speed (V) Engine output The previous equation relates the power delivered to the wheels to reach speed V. The actual power required will be higher due to mechanical loss. Power is delivered to a wheel in the form of torque. Fw Tw / r Force delivered by a wheel to the road to push the car along Torque on the wheel Radius of the wheel Stopping • Stopping distance depends on the braking system and how hard the driver breaks. o the harder the brakes are applied the shorter the stopping distance • If a car skids then the stopping distance depends on the frictional force between the tyres and the road. • If travelling uphill the stopping distance will be shorter. o gravity plays a role • If travelling downhill the stopping distance will be greater. 242-515 AGD: 12. Physics 53 Calculating Skidding Distance Initial speed of the car d s v /[ 2 g ( cos sin )] 2 Acceleration due to gravity 242-515 AGD: 12. Physics Coefficient of friction between wheels and the road. Usually around 0.4 Angle of the road 54 More Calculations Required? • In a real driving game a lot more parameters play a role. o e.g. suspension, variable engine power, road surface • Usually it is easier to use a physics engine, but this still requires an understanding of the forces that you want to model. 242-515 AGD: 12. Physics 55 6. JBullet Features • Rigid Bodies o Simple shapes, complex geometries • Joints and Spring Constraints • Dynamics Modeling what we've been talking about in this part o Integrating forces and torques • Collision Detection the next part o Physical interactions between objects • Ray Tracing o Range finders and optical flow • Cloth, Soft (deformable) Bodies 242-515 AGD: 12. Physics 56 56 7. A Game Physics Textbook • Physics for Game Developers David M Bourg, Bryan Bywalec O'Reilly, April 2013, 2nd ed. 242-515 AGD: 12. Physics 57