250 likes | 265 Views
Game Physics – Part I. Dan Fleck. Rigid Body Dynamics. Kinematics is the study of movement over time Dynamics is the study of force and masses interacting to cause movement over time (aka kinematic changes). Example: How far a ball travels in 10 seconds at 50mph is kinematics
E N D
Game Physics – Part I Dan Fleck Coming up: Rigid Body Dynamics
Rigid Body Dynamics • Kinematics is the study of movement over time • Dynamics is the study of force and masses interacting to cause movement over time (aka kinematic changes). • Example: • How far a ball travels in 10 seconds at 50mph is kinematics • How far the same ball travels when hit by a bat and under the force of gravity is dynamics • Additionally for simplification we’re going to model rigid bodies – ones that do not deform (not squishy) • We can model articulated rigid bodies – multiple limbs connected with a joint Coming up: Bring on calculus
Bring on calculus • Calculus was invented by Newton (and Leibniz) to handle these problems • Newton’s Laws • 1. An object at rest stays at rest and an object in uniform motion stays in the same motions unless acted upon by outside forces (conservation of inertia) • 2. Force = Mass * Acceleration • 3. For every action there is an equal and opposite reaction Coming up: F=ma
F=ma • r=Position, v=Velocity, a=acceleration • Velocity is equal to the change in position over time. • Acceleration is equal to the change in velocity over time. Coming up: Intuitive Understanding
Intuitive Understanding • If every second my position changes by 5m, what is my velocity? • Acceleration is the change in velocity over time. If I am traveling at 5m/s at time t=1, and 6m/s at t=2, my acceleration is 1m/s^s Coming up: Integration
Integration • Integration takes you backwards • Integrating acceleration over time gives you velocity • Intuition: • If you are acceleration at 5m/s^s, then every second you increase velocity by 5. Integrating ‘sums’ up these changes, so your velocity is • What is “C”? • At time t=0, what is velocity? C… so C is initial velocity • So, if you are accelerating at 5m/s^s, starting at 7m/s what is your velocity at time t=3 seconds? Coming up: Integration
Integration • Similarly, integrating velocity over time gives you position • Example: If you’re accelerating at a constant 5m/s^s, then: • So, given you have traveled for 5 seconds starting from point 0, where are you? • Plug in the values: • So, given initial position, initial velocity, and acceleration you can find the new position, velocity. • We will do this every frame, using values from the previous frames. Coming up: Forces
Forces • But wait… how do we find the acceleration to begin with? • Linear momentum is denoted as p which is: • To change momentum, we need a force.Newton says: • So, given a force on a point mass, we can find the acceleration and then we can find position, velocity… whew, we’re done… but….. Coming up: Finding Momentum
Finding Momentum • On a rigid body, we have mass spread over an area • We compute momentum by treating each point on the object discretely and summing them up: • Lets try to simplify this by introducing the center of mass (CM). Define CM as (where M is the total mass of the body): Coming up: Center of Mass
Center of Mass • Using this equation, multiply bothsides by M and take the derivative • Aha.. .now we have total momentum on the right, but what is on the left? • Because M is a constant it comes out of the derivative and then we have change in position over time of the center of mass… or velocity of CM! Coming up: Acceleration of CM
Acceleration of CM • Total linear momentum can be found just using the velocity of the CM (no summation needed!) • So, finally the acceleration of the entire body can be calculated by assuming the forces are all acting on the CM and computing the acceleration of CM Coming up: Partial Summary
Partial Summary • We now know, that given an object’s acceleration we can compute it’s velocity and position by integrating: • And to determine acceleration, we can sum forces acting on the center of mass (CM) and divide by total mass • Current challenge: Integrating symbolically the find v(t) and t(t) is very hard! Remember differential equations? Coming up: Differential Equations
Differential Equations • These equations occur when the dependent variable and it’s derivative both appear in the equation. Intuitively this occurs frequently because it means the rate of change of a value depends on the value. • Example: air friction.. the faster you are going, the more force it applies to slow you down: • f = -v = ma (solve for a) but a is the derivative of v, so • Solving this analytically is best left to you and your differential equations professor Coming up: Numerical Integration of Ordinary Differential Equations (ODEs)
Numerical Integration of Ordinary Differential Equations (ODEs) • Analytically solving these is hard, but solving them numerically is much simpler. Many methods exist, but we’ll use Euler’s method. • Integration is simply summing the area under the curve, and the derivative is the slope of the curve at any point. Euler says: Integrating from t=3 to 5 is summing the y values for that section. t=3 t=5 Coming up: Euler’s Approximation
Euler’s Approximation Numerically integrating velocity and position we get these equations: Euler numerical integration is an approximation (src: Wikipedia) Coming up: Final Summary of Equations
Final Summary of Equations • Sum up the forces acting on the body at the center of mass to get current acceleration • To get new velocity and position, use your current acceleration, velocity, position and numerical integration over some small time step (h) Coming up: Now we can code!
Now we can code! ForceRegistry: stores which forces are being applied to which objects ForceGenerator: virtual (abstract) class that all Forces implement Mainloop for each entry in Registry add force to accumulator in object for each object compute acceleration using resulting total force compute new velocity using acceleration compute new position using velocity reset force accumulator to zero Coming up: ForceRegistry
ForceRegistry √ Coming up: ForceGenerator
ForceGenerator Coming up: ImpulseForceGenerator
ImpulseForceGenerator Warning: This code is actually changing the acceleration, it should just update the forces and the acceleration should be computed at the end of all forces Coming up: DragForce generator
DragForce generator • In order to slow an object down, a drag force can be applied that works in the opposite direction of velocity. • typically a simplified drag equation used in games is: • k1 and k2 are constants specifying the drag force, and the direction is in the opposite direction of velocity. Coming up: DragForce Generator
DragForce Generator Add force to current forces upon the player Coming up: Mainloop – Updating Physics Quantities
Mainloop – Updating Physics Quantities Inside mainloop After the forces have been updated, you must then apply the forces to create acceleration and update velocity and position. Coming up: What’s next?
What’s next? • Other forces • Spring forces – push and pull • Bungee forces – pull only • Anchored springs/bungees • Rotational forces • forces instead of moving the force can also induce rotations on the object • Collisions • Conversion from 2D to 3D Coming up: References
References • These slides are mainly based on Chris Hecker’s articles in Game Developer’s Magazine (1997). • The specific PDFs (part 1-4) are available at:http://chrishecker.com/Rigid_Body_Dynamics • Additional references from: • http://en.wikipedia.org/wiki/Euler_method • Graham Morgan’s slides (unpublished) End of presentation