Animation and Games Development 242-515, Semester 1, 2014-2015 12. Basic Physics Objective show how physics is used in games programming
Overview 1. Why Physics? Point-based Physics Rigid Body Physics 4. Other Types of Force 5. The World of the Car 6. JBullet Features 7. A Game Physics Textbook
1. Why Physics? Physics can make game worlds appear more natural / realistic But... Often games are not realistic e.g. cars travelling at 200 mph, double jumps of Mario Physics engines are computationally expensive they may slow down the game too much
Engines Commercial Free Havok (Havok.com) Renderware (renderware.com) NovodeX (novodex.com) Free Open Dynamic Engine (ODE) (ode.org) PhysX Bullet (Java version used in jME) Box2D (2D)
Types of Physics Newtonian (this one for games) Classical Quantum Newton’s space and time are absolute Classical Einstein's space-time can be “changed” by matter Quantum Deterministic causality questioned and multiple localities in a single instance
Remember this stuff….. Time A measured duration Speed The distance travelled given some time limit Direction An indication of travel Velocity The rate of change of position over time (has speed and direction) Acceleration The rate of change of velocity Force Causes objects to accelerate (has direction as well as magnitude) Mass The weight of an object? (No! weight depends on gravitational pull) The quantity of matter
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"
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
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.
As Cartoons
Game Physics Two main types of Newtonian physics used in games: point-based, rigid body based Point-based physics used in particle systems, bullet motion… a point's mass is located inside the point a point does not rotate: it has no rotational orientation
2. Point-based Physics Solve Newtonian equations to get the position of a point at time t: Force applied to the point, F(t), causes acceleration Acceleration, a(t), causes a change in the point's velocity Velocity, V(t) causes a change in the point's position
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 + a t s = ½ t (u + v) v2 = u2 + 2 a s s = u t + ½ a t2 u = initial velocity; v = final velocity a = acceleration t = time (seconds) s = distance (meters)
Velocity Vtotal = √(Vx2 + Vy2 + Vz2) for 3D games 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 g: constant acceleration due to gravity (9.81m/s2) Projectile equations of motion: Here, the terms on the right such as g(t-tinit) indicate multiplication by (t-tinit), rather than g being a function of (t-tinit). The perils of inconsistent and contradictory notation exist throughout math and science. See Chapter Zero in Jim Blinn’s book, Notation, Notation, Notation, for further enlightenment related to this fact! *IMPORTANT NOTE: The closed form equations are exact; HOWEVER, they are only exact when the gravitational field is truly uniform, acting in the same direction throughout the gravitational field. In real life, on real planets, this is not the case, since when you move across the surface the direction to the center of mass of the planet changes. (In real life acceleration due to gravity doesn’t even act exactly towards the center of the planet, since the mass distribution of the planet isn’t constant, and fluctuates ever so subtly). But, for objects moving relatively small distances over the surface of large planets, the assumption of constant g is quite accurate. For space craft simulation, where the distance from the object to planet center is much larger than the planet radius, the assumption of constant g breaks down and isn’t at all accurate. The next step up is to use a central force approximation and use conservation of angular momentum to model the orbital motion. Could also just calculate the force due to gravity based on the relative location of two masses, using Newton’s Law of Universal Gravitation (or something more exotic). p() is the position function
Target Practice Projectile Launch Position, pinit Target Under constant gravitional acceleration, projectile path is a parabola 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.
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: F = m a v = ∫a dt x = ∫v dt momentum is conserved
center of mass = point
Rotation A rigid body has an orientation (rotated position in space) Rotation calculations are complicated one reason is that the order of rotation operations is important: x-axis rotation then y-axis rotation ≠ y-axis rotation then x-axis rotation Rotation is not commutative
When a body can rotate the Newtonian motion equations must be extended. new equations are needed for rotational (angular) mass, velocity, acceleration, force, momemtum, etc.
Angular Velocity, ω The angular velocity ω describes the speed of rotation and the orientation of the axis about which the rotation occurs.
Angular Velocity Again Δθ = change in angular displacement Δt = time
Angular Acceleration, α Δω = change in angular velocity Δt = time
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?
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
Uses for Torque The same amount of torque can be applied with less force if the distance from the pivot (fulcrum) is increased.
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
easy (torque is small) hard (torque is large) axes effect
Another definition: moment of Inertia is an object’s resistance to rotating around an axis
Linear Momentum, p Also called translational momentum: the product of the mass and velocity of an object p = m v Linear momentum is a conserved quantity: if a closed system is not affected by external forces, then its total linear momentum cannot change useful for calculating velocity change after collisions
Angular momentum, L Angular momentum is the rotational version of linear momentum. 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 = l x ω
Angular momentum is a conserved quantity: if a closed system is not affected by external torque, then its total angular momentum cannot change useful for calculating rotational change when moments of inertia change L = I1 x ω1 L = I2 x ω2
Linear vs. Angular Linear Concept Angular Version velocity v angular velocity ω acceleration a angular acceleration α F = m a torque T = r x F' = I α mass m moment of inertia I momentum p = m v angular momentum L = I x ω
Changing Coordinate Systems The maths for rigid bodies is simpler if we use a local coordinate system for each body 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.
4. Other Types of Force (Virtual) Springs Damping Friction Aerodynamic Drag …
(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: preventing objects overlapping cloth rendering character animation
Linear Springs
Damping Damping describes physical conditions such as viscosity, roughness, etc.
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. called kinetic friction
5. The World of the Car Cars exist in an environment that exerts forces.
Resistance A car driving down a road experiences two (main) types of resistance. Aerodynamic drag, rolling resistance Once resistance has been calculated, it is possible to calculate the amount of power a car needs to move.
Aerodynamic Drag Projected frontal area of car normal to direction of V Mass density of air Speed of car Drag coefficient: 0.29 – 0.4: sports cars; 0.6 – 0.9: trucks
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: 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 1 horsepower = 550 ft-lbs/s
Horsepower Horsepower needed to overcome total resistance at a given speed (we are working in feet and lbs): Total resistance corresponding to a car’s speed (V) horsepower
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. 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. 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. gravity plays a role If travelling downhill the stopping distance will be greater.
Calculating Skidding Distance Initial speed of the car Acceleration due to gravity Angle of the road Coefficient of friction between wheels and the road. Usually around 0.4
More Calculations Required? In a real driving game a lot more parameters play a role. 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.
6. JBullet Features Rigid Bodies Joints and Spring Constraints Simple shapes, complex geometries Joints and Spring Constraints Dynamics Modeling Integrating forces and torques Collision Detection Physical interactions between objects Ray Tracing Range finders and optical flow Cloth, Soft (deformable) Bodies what we've been talking about in this part the next part
7. A Game Physics Textbook Physics for Game Developers David M Bourg, Bryan Bywalec O'Reilly, April 2013, 2nd ed.