Physics + Vectors References: xyz. Variable frame rates (review) Two options for handling it: –...
-
Upload
junior-sherman -
Category
Documents
-
view
217 -
download
0
Transcript of Physics + Vectors References: xyz. Variable frame rates (review) Two options for handling it: –...
Physics + Vectors
References:• xyz
Variable frame rates (review)
• Two options for handling it:– Option1: Cap frame rates• When moving / rotating express in units / updated• Good: Easy to understand• Bad: Slow machines can't reach the frame-rate
– 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: A little harder to understand
Velocity and Acceleration• 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.
Speed (mph)
Time (hours)
50
3
Velocity and Acceleration, cont.
• What about (abruptly) changing velocity?
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
Velocity and Acceleration, cont.
• we can approximate the area of this curve using (small) discreet time intervals
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() / 1000.0
Velocity and Acceleration, cont.
• What we're really doing is approximating the integral:
• Problem: This just tells us the distance we've moved, not the direction
• Q: What do we need to express distance and direction?
• A: Vectors!
Velocity and Acceleration
• represents a spaceship's position.• represents the ship's velocity (in units/s)• Suppose Δt seconds have passed• (the ship's new position) = ???
=
�⃗� 𝑣
𝑣∗ Δ𝑡
𝑝 ′
change in position due to velocity
Velocity and Acceleration, cont.• Velocity results in a
change in position• Acceleration results in
a change in velocity
Speed (mph)
Time (hours)
50
3
Position-offset (miles)
Time (hours)
25
3
50
75
100
Accel (m/h2)
3
50
𝒑 ′=�⃗�+∆ 𝒕 �⃗�
𝒗 ′=�⃗�+∆ 𝒕 �⃗�
Acceleration and Vectors, cont.• Our spaceship example, continued:– We fire the thrusters while facing in the direction – We hold the thrusters down for k (partial?)
seconds– The thrusters fire with a force of n units/s2
• Q: What is the new velocity of the ship (after these k seconds)
�̂��⃗�
𝑣 𝑛∗𝑘∗�̂�
𝑣 ′𝒗 ′=�⃗�+𝒏∗𝒌∗ �̂�
Gravity
• Gravity produces an accelaration on objects• Assuming we're on the earth, it points
downwards
𝑝1
�⃗�
𝑣1
�⃗�∆ 𝑡𝑣1 ′
𝑣1 ′ ∆ 𝑡
𝑝1 ′
Newton's Laws of Motion
1. The velocity of an object is constant unless acted upon by an outside force
2. For every action there is an equal-magnitude / opposite-direction action.
Simple collisions
• Not physically accurate• When we've looked at dot product, we'll re-
explore this and do proper– elastic collisions– inelastic collisions
• For now: – When two objects collide, impart a force• Equal magnitude, opposite direction on each body• Of a fixed magnitude
A new vector operator
• Not a standard one– Not the correct term, but I'll call it the tensor
product of two vectors.
– Not really any graphical interpretation.– Useful in:• Bouncing (next slide)• Lighting
Boundary collisions• We'll make this more flexible later. For now:– For each wall, generate a unit-length "normal", • Left is [1, 0] Right is [-1, 0]• Up is [0, 1] Down is [0, -1] (in pygame)
– When an object hits a wall add 2 * (.• Or to simulate friction, change the 2 to 1.9 (or similar)
�̂�
(.
2(
�⃗�
Gravity as a Force
• Gravity is a force– Proportional to the mass– But…when we apply the force• We divide by the mass (Newton's 2nd)• So…the acceleration is the same regardless of mass
– Unless there is air resistance (drag)
"Planetary" Gravity
• If we're simulating planets, gravity isn't "down"– Gravity is a force– Depends on the position of all objects • even if they're in another solar system!
– Where s is this object, i is the other object– In the real world, G = 6.6734^10-11 Nm/kg2
Examples / Labs
• #1:– Two bodies, exerting "planetary" gravity on the other.– Try to set up a semi-stable orbit.
• #2:– Create a bunch of circles, subject to gravity and bouncing
off walls• Bouncing off each other
– Click to lay a bomb.• #3:– Asteroids!– Bouncing asteroids– Realistic accelaration