Physics in Game Programming Basic Physics Review Vector: a quantity that has two independent properties: magnitude and direction. Examples of vectors in nature are velocity, acceleration, momentum, force, electromagnetic fields, and weight. Velocity: change in position over time. speed: mph, fps, pixels per frame angle: degrees, radians speed angle (dir) Basic Physics Review Vector: a quantity that has two independent properties: magnitude and direction. Examples of vectors in nature are velocity, acceleration, momentum, force, electromagnetic fields, and weight. Velocity: change in position over time. (6,8) y speed angle (dir) (0,0) x (x, y) Vector = (6, 8) Using trig the quantities can be converted from one format to another. Velocity Example Velocity: change in position over time. vel(x, y) = (5, 0) Unit of measurement? • MPH • FPS • Pixels per frame • Pixels per second vx = 5 vy = 0 (0,0) (5,0) (10,0) (15,0) (20,0) (25,0) (30,0) (35,0) (40,0) Add velocity values to current location (x, y) every cycle. Acceleration Acceleration: change in velocity over time. angle: degrees, radians Velocity angle (dir) Acceleration Vector: a quantity that has two independent properties: magnitude and direction.. Acceleration: change in velocity over time. (6,8) velocity y angle (dir) (0,0) x (x, y) Vector = (6, 8) Using trig the quantities can be converted from one format to another. Velocity without Acceleration initial velocity new velocity vel(x,y) = (0, 0) vel(x,y) = (50, 0) (0, 0) (50, 0) (0, 0) (50, 0) Acceleration Acceleration: change in velocity over time. accel(x,y) – mph2 (5, 0) vel(x,y) - mph (0, 0) position(x,y) - feet (0, 0) 0 seconds (0, 0) (58, 0) Acceleration Acceleration: change in velocity over time. accel(x,y) – mph2 (5, 0) (5, 0) vel(x,y) - mph (0, 0) (5, 0) Displacement formula s = ut + 1/2at2 s = distance, u = initial velocity position(x,y) - feet (0, 0) (4, 0) Miles to Ft Conversion ft = miles * 5280ft 1 second (4, 0) .00069 miles (58, 0) Acceleration Acceleration: change in velocity over time. accel(x,y) – mph2 (5, 0) (5, 0) (5, 0) vel(x,y) - mph (0, 0) (5, 0) (10, 0) position(x,y) - feet (0, 0) (4, 0) (15, 0) 2 seconds (15, 0) .00278 miles (58, 0) Acceleration Acceleration: change in velocity over time. accel(x,y) – mph2 (5, 0) (5, 0) (5, 0) (5, 0) vel(x,y) - mph (0, 0) (5, 0) (10, 0) (15, 0) position(x,y) - feet (0, 0) (4, 0) (15, 0) (33, 0) 3 seconds (33, 0) .00625 miles (58, 0) Acceleration Acceleration: change in velocity over time. accel(x,y) – mph2 (5, 0) (5, 0) (5, 0) (5, 0) (5, 0) vel(x,y) - mph (0, 0) (5, 0) (10, 0) (15, 0) (20, 0) position(x,y) - feet (0, 0) (4, 0) (15, 0) (33, 0) (58, 0) 4 seconds (58, 0) .01111 miles Acceleration Acceleration: change in velocity over time. accel(x,y) – pps2 (5, 0) vel(x,y) - pps (0, 0) pps = pixels per second position(x,y) - pixels (0, 0) 0 seconds (0, 0) (50, 0) Acceleration Acceleration: change in velocity over time. accel(x,y) – pps2 (5, 0) (5, 0) Add vel(x,y) - pps (0, 0) (5, 0) pps = pixels per second Add position(x,y) - pixels (0, 0) (5, 0) 1 second (5, 0) (50, 0) Acceleration Acceleration: change in velocity over time. accel(x,y) – pps2 (5, 0) (5, 0) (5, 0) Add vel(x,y) - pps (0, 0) (5, 0) (10, 0) pps = pixels per second Add position(x,y) - pixels (0, 0) (5, 0) (15, 0) 2 seconds (15, 0) (50, 0) Acceleration Acceleration: change in velocity over time. accel(x,y) – pps2 (5, 0) (5, 0) (5, 0) (5, 0) Add vel(x,y) - pps (0, 0) (5, 0) (10, 0) (15, 0) pps = pixels per second Add position(x,y) - pixels (0, 0) (5, 0) (15, 0) (30, 0) 3 seconds (30, 0) (50, 0) Acceleration Acceleration: change in velocity over time. accel(x,y) – pps2 (5, 0) (5, 0) (5, 0) (5, 0) (5, 0) Add vel(x,y) - pps (0, 0) (5, 0) (10, 0) (15, 0) (20, 0) pps = pixels per second Add position(x,y) - pixels (0, 0) (5, 0) (15, 0) (30, 0) (50, 0) 4 seconds (50, 0) Comparison Feet Per Second (0, 0) (4, 0) (15, 0) (33, 0) (58, 0) 4 seconds Pixels Per Second (0, 0) (5, 0) (15, 0) (30, 0) (50, 0) 4 seconds Conversion Formula1 I began this lesson by defining a vector as a quantity that has two independent properties: magnitude and direction. I pointed out that with some basic trigonometry these two properties could be converted into x and y coordinate properties. Below is the formula for converting the properties magnitude and direction into x and y properties. dx = magnitude • cos(direction) dy = magnitude • sin(direction) dx = magnitude * Math.cos(Math.toRadians(direction)); dy = magnitude * Math.sin(Math.toRadians(direction)); Conversion Formula2 Here is the formula for converting a vector represented by x and y properties into magnitude and direction properties. 𝑑𝑖𝑟𝑒𝑐𝑡𝑖𝑜𝑛 = atan 𝑑𝑦, 𝑑𝑥 𝑚𝑎𝑔𝑛𝑖𝑡𝑢𝑑𝑒 = 𝑑𝑥 ∙ 𝑑𝑥 + 𝑑𝑦 ∙ 𝑑𝑦 Pythagorean Theorem direction = (int)Math.toDegrees(Math.atan2(dy, dx)); magnitude = Math.sqrt(dx*dx + dy*dy); Vectors We Don’t Need Know Stinking Vectors Since a vector is simply a way to represent two quantities, it turns out that we don’t even need to use vectors in our game programming. Instead of representing velocity as a vector we can just create two variables named vx and vy to represent velocity. Instead of representing acceleration as a vector we can create two variables named ax and ay to represent acceleration. private private private private int int int int vx; vy; ax; ay; // // // // horizontal velocity vertical velocity horizontal acceleration vertical acceleration Summary Velocity and Acceleration are two very important properties in game programming because they allow you to animate objects in a way that mimics real life. Fortunately we can represent these properties in games without having to use a lot of advanced math but still having a precision that is close enough to give the illusion that these properties behave just like their real world counterparts. private private private private int int int int vx; vy; ax; ay; // // // // horizontal velocity vertical velocity horizontal acceleration vertical acceleration Summary To add velocity and acceleration to an object in a game simply add the acceleration values to the velocity values and then add the velocity values to the object’s (x, y) position during each time cycle of the game. private int x = 0; // horizontal position private int y = 0; // vertical position private int vx = 5; // horizontal velocity private int vy = 5; // vertical velocity private int ax = 2; // horizontal acceleration private int ay = 2; // vertical acceleration -----------------------------------------------------------vx = vx + ax; vy = vy + ay; // add accel to vel x = x + vx; y = y + vy; // add vel to current position
© Copyright 2025 Paperzz