References: Wikipedia PHYSICS + VECTORS
Why physics? Good for every game developer to know. Physics formulae are often vector-based A good chance to try out math3d! Often thinking "backwards" (from pictures to symbolic algorithms) To show you how math3d is usable in pygame Some cherry-picked topics we'll explore: velocity + acceleration (w/ variable frame-rate) Newton's laws of motion Friction Terminal velocity OVERVIEW
Suppose you are moving at 50mph for 3 hours. How far are you from your original position? A: 150 miles 50 * 3 Express this as a graph We're really calculating the area of this rectangle. VELOCITY AND ACCELERATION Speed (mph) Time (hours) 50 3
What about (abruptly) changing velocity? VELOCITY AND ACCELERATION, CONT. Speed (mph) Time (hours) 50 3 Q: How far have we travelled after these 5 hours? Q (rephrased): In other words, what is the area of the pink area? A: 0.5*(50x1) + (50x3) + 0.5*(50x1) = 200 miles
Two options for handling it: Option1: Cap frame rates When moving / rotating express in units / update Bad: Slow machines can't reach the frame-rate Bad: *Very* painful to modify our fps-cap. Option2: Let computer run as fast as possible When moving / rotating express in units / second The multiply by the time since the last frame Good: Runs on any speed machine Bad: Have an extra multiplication VARIABLE FRAME RATES (REVIEW)
we can approximate the area of this curve using (small) discreet time intervals VELOCITY AND ACCELERATION, CONT. Speed (mph) Time (hours) 50 3 A dT value for one iteration of the game loop. import pygame # … clock = pygame.time.Clock() #... (inside the game loop) dT = clock.tick() /
VELOCITY AND ACCELERATION, CONT.
VELOCITY AND ACCELERATION change in position due to velocity
ACCELERATION
ACCELERATION AND VECTORS, CONT.
Gravity produces an acceleration on objects Assuming we're on the earth, it points downwards GRAVITY
In real life, there's a maximum speed objects can go Theoretically the speed of light. On earth, there's wind resistance. In games, we often impose a terminal velocity More numerically stable. More consistent gameplay experience. A little more like real life. [Develop the formula] TERMINAL VELOCITY
NEWTON'S LAWS OF MOTION
Gravity is a force Proportional to the mass But…when we apply the force We divide by the mass (Newton's 2 nd ) So…the acceleration is the same regardless of mass Unless there is air resistance (drag) Proved by Gallileo around In code: 1.Create a Force vector, F 2.Multiply F by mass. 3.Divide F by mass when applying the force 4.Apply acceleration to velocity as normal (or just skip steps 2 & 3) GRAVITY AS A FORCE
Friction causes an object to slow down Air resistance Contact with ground …… Implemented as an accleration in the opposite direction of movement Complication: "flipping direction" Complication: magnitude (and connection to dt) [Come up with formula on board] FRICTION