90 likes | 196 Views
Physics. Simple – Pong Medium – Rigid bodies F = ma Circles, spheres, rectangles for collisions Complex – Fluids, clothings, explosions, hair. Pong. Velocity is constant Vx = 0.1 Vy = 0.1 Position at time t = Px, Py Position at time t + 1 Px(t+1) = Px(t) + Vx Py(t+1) = Py(t) + Vy.
E N D
Physics • Simple – Pong • Medium – Rigid bodies • F = ma • Circles, spheres, rectangles for collisions • Complex – Fluids, clothings, explosions, hair
Pong • Velocity is constant • Vx = 0.1 • Vy = 0.1 • Position at time t = Px, Py • Position at time t + 1 • Px(t+1) = Px(t) + Vx • Py(t+1) = Py(t) + Vy
Pong • How do I get motion that is not diagonal? • What happens when I hit a wall? • Vx = -Vx (which wall?) • Vy = -Vy • Vx = -Vx, Vy = -Vy (when you hit a corner) • What happens when I hit a paddle?
Pong - Collisions • If Py < Ymin OR Py > Ymax, I hit a wall • If Px < Xmin OR Px > Xmax, I scored/hit a wall Radius = r Ball center compared to X1 - Cx, Y1 r
F = ma • Gravity: F = G * (M1 * M2)/D^2 • G = gravitational constant, D is distance • On earth, assume mass of earth is LARGE so it does not move. Acceleration due to gravity is a constant = 32 ft/sec^2 or 9.8m/sec^2 • V(t) = V (0) t + ½ g t^2 • Vx = Vx + 0 • Vy = Vy + g → choose g to make it look good. • !
Friction • Friction is a function of velocity and mass • Static Friction > Kinetic Friction • Static: when V = 0 • Kinetic: when V > 0 • V = V – friction, if V > friction, else 0
Collisions (again) • Detect Collision • Determine time of collision (why?) • Determine where objects are when they touch • Determine the collision normal • Determine velocity vectors after collision • Determine changes in rotation (advanced:))
Circles/Spheres • If the distance between 2 objects is less than sum of radii • Avoid using square root • (r1+r2)^2 > ((x1 – x2)^2 + (y1 – y2)^2) • If N objects → O(N^2)
Other objects • Collisions are rare • Most of the time objects are NOT colliding • Create a series of filters so that computationally expensive tests are done rarely • Use a grid and only test objects in cells adjacent (touched by) to you • First use bounding circles/spheres, then test further