Understanding Vectors and Velocity in Physics and Game Programming
Exploring the concepts of vectors, velocity, and positional vectors in both physics and game programming. Learn about key properties, conversion formulas, and the application of velocity vectors to positional vectors using examples and visual representations.
Download Presentation
![](/assets/img/so-down.gif)
Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
E N D
Presentation Transcript
Physics in Game Physics in Game Programming 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 in a specific direction. speed: mph, fps, pixels per frame angle: degrees, radians speed angle (dir)
Velocity Vector Velocity is the change in position over time in a specific direction. A velocity vector is represented by speed (magnitude) and angle (direction). velocity vector = (speed, angle) speed angle (dir)
Velocity Vector Using a coordinate plane, a velocity vector can be converted into x and y coordinate properties by using the following formula. x = magnitude cos(direction) y = magnitude sin(direction) (6,8) (x, y) Velocity Vector = (6, 8) y speed angle (dir) x (0,0)
Positional Vector A vector can be used to represent a (x, y) coordinate position. Examples A positional vector is technically not a vector because it does not follow the definition of a vector which states that the two quantities of a vector represent a magnitude and a direction. loc (x, y) = (0, 0) loc (x, y) = (5, 0) loc (x, y) = (40, 0) (40,0) (0,0) (5,0) (35,0) (10,0) (15,0) (20,0) (30,0) (25,0)
Using Velocity Velocity: change in position over time in a specific direction. pos(x, y) = (0, 0) positional vector Unit of measurement? MPH FPS Pixels per frame Pixels per second vel(x, y) = (5, 0) velocity vector Apply velocity vector to positional vector: pos(x, y) = (0 + 5, 0 + 0) = (5, 0) (40,0) (0,0) (5,0) (35,0) (10,0) (15,0) (20,0) (30,0) (25,0) Add velocity values to current location (x, y) every cycle.
Using Velocity Velocity: change in position over time in a specific direction. pos(x, y) = (5, 0) positional vector Unit of measurement? MPH FPS Pixels per frame Pixels per second vel(x, y) = (5, 0) velocity vector Apply velocity vector to positional vector: pos(x, y) = (5 + 5, 0 + 0) = (10, 0) (40,0) (0,0) (5,0) (35,0) (10,0) (15,0) (20,0) (30,0) (25,0) Add velocity values to current location (x, y) every cycle.
Using Velocity Velocity: change in position over time in a specific direction. pos(x, y) = (10, 0) positional vector Unit of measurement? MPH FPS Pixels per frame Pixels per second vel(x, y) = (5, 0) velocity vector Apply velocity vector to positional vector: pos(x, y) = (10 + 5, 0 + 0) = (15, 0) (40,0) (0,0) (5,0) (35,0) (10,0) (15,0) (20,0) (30,0) (25,0) Add velocity values to current location (x, y) every cycle.
Using Velocity Velocity: change in position over time in a specific direction. pos(x, y) = (15, 0) positional vector Unit of measurement? MPH FPS Pixels per frame Pixels per second vel(x, y) = (5, 0) velocity vector Apply velocity vector to positional vector: pos(x, y) = (15 + 5, 0 + 0) = (20, 0) (40,0) (0,0) (5,0) (35,0) (10,0) (15,0) (20,0) (30,0) (25,0) Add velocity values to current location (x, y) every cycle.
Acceleration Acceleration: change in velocity over time in a specific direction. angle: degrees, radians Velocity angle (dir)
Acceleration Vector Acceleration is the change in velocity over time in a specific direction. An acceleration vector can be represented by a velocity (magnitude) and an angle (direction). acceleration vector = (velocity, angle) velocity angle (dir)
Acceleration Vector Using a coordinate plane, an acceleration vector can be converted into x and y coordinate properties by using the same formula. x = magnitude cos(direction) y = magnitude sin(direction) (2,3) (x, y) Acceleration Vector = (2, 3) y velocity angle (dir) x (0,0)
Velocity without Acceleration initial position new position velocity vel(x,y) = (25, 0) pos(x,y) = (0, 0) pos(x,y) = (25, 0) (50, 0) (25, 0) (0, 0) (50, 0) (25, 0) (0, 0)
Velocity without Acceleration initial position new position velocity vel(x,y) = (25, 0) pos(x,y) = (25, 0) pos(x,y) = (50, 0) (50, 0) (25, 0) (0, 0) (50, 0) (25, 0) (0, 0)
Velocity with 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)
Velocity with Acceleration Acceleration: change in velocity over time. accel(x,y) mph2 vel(x,y) - mph position(x,y) - feet (5, 0) (5, 0) (0, 0) (5, 0) (0, 0) (4, 0) Displacement formula s = ut + 1/2at2 s = distance, u = initial velocity Miles to Ft Conversion ft = miles * 5280ft 1 second (4, 0) .00069 miles (58, 0)
Velocity with Acceleration Acceleration: change in velocity over time. accel(x,y) mph2 vel(x,y) - mph position(x,y) - feet (5, 0) (5, 0) (5, 0) (0, 0) (5, 0) (10, 0) (0, 0) (4, 0) (15, 0) 2 seconds (15, 0) .00278 miles (58, 0)
Velocity with Acceleration Acceleration: change in velocity over time. accel(x,y) mph2 vel(x,y) - mph position(x,y) - feet (5, 0) (5, 0) (5, 0) (5, 0) (0, 0) (5, 0) (10, 0) (15, 0) (0, 0) (4, 0) (15, 0) (33, 0) 3 seconds (58, 0) (33, 0) .00625 miles
Velocity with Acceleration Acceleration: change in velocity over time. accel(x,y) mph2 vel(x,y) - mph position(x,y) - feet (5, 0) (5, 0) (5, 0) (5, 0) (5, 0) (0, 0) (5, 0) (10, 0) (15, 0) (20, 0) (0, 0) (4, 0) (15, 0) (33, 0) (58, 0) 4 seconds (58, 0) .01111 miles
Velocity with Acceleration pps = pixels per second Acceleration: change in velocity over time. accel(x,y) pps2 (5, 0) vel(x,y) - pps (0, 0) position(x,y) - pixels (0, 0) 0 seconds (0, 0) (50, 0)
Velocity with Acceleration pps = pixels per second Acceleration: change in velocity over time. accel(x,y) pps2 (5, 0) (5, 0) vel(x,y) - pps (0, 0) (5, 0) position(x,y) - pixels (0, 0) (5, 0) Add Add 1 second (5, 0) (50, 0)
Velocity with Acceleration pps = pixels per second Acceleration: change in velocity over time. accel(x,y) pps2 (5, 0) (5, 0) (5, 0) vel(x,y) - pps (0, 0) (5, 0) (10, 0) position(x,y) - pixels (0, 0) (5, 0) (15, 0) Add Add 2 seconds (15, 0) (50, 0)
Velocity with Acceleration pps = pixels per second Acceleration: change in velocity over time. accel(x,y) pps2 (5, 0) (5, 0) (5, 0) (5, 0) vel(x,y) - pps (0, 0) (5, 0) (10, 0) (15, 0) position(x,y) - pixels (0, 0) (5, 0) (15, 0) (30, 0) Add Add 3 seconds (30, 0) (50, 0)
Velocity with Acceleration pps = pixels per second Acceleration: change in velocity over time. accel(x,y) pps2 (5, 0) (5, 0) (5, 0) (5, 0) (5, 0) vel(x,y) - pps (0, 0) (5, 0) (10, 0) (15, 0) (20, 0) position(x,y) - pixels (0, 0) (5, 0) (15, 0) (30, 0) (50, 0) Add Add 4 seconds (50, 0)
Comparison Feet Per Second 0 sec 1 sec 2 sec 3 sec 4 sec Pixels Per Second 0 sec 1 sec 2 sec 3 sec 4 sec (0, 0) (4, 0) (15, 0) (33, 0) (58, 0) (0, 0) (5, 0) (15, 0) (30, 0) (50, 0) Conclusion: Game simulations using pixels can realistically represent real life quantities or situations.
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);
Vector Class In game programming, particularly 3D games, programmer will often use a Vector class to represent forces like velocity, acceleration, friction, gravity, tension, and air resistance. public class Vector { private int x; private int y; public Vector(int x, int y) { this.x = x; this.y = y; } // other methods not shown }
Vector Class In game programming, particularly 3D games, programmer will often use a Vector class to represent forces like velocity, acceleration, friction, gravity, tension, and air resistance. Vector vel = new Vector(2, 0); Vector accel = new Vector(5, 0); Vector gravity = new Vector(0, -2);
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 int vx; // horizontal velocity private int vy; // vertical velocity private int ax; // horizontal acceleration private int ay; // 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 int vx; // horizontal velocity private int vy; // vertical velocity private int ax; // horizontal acceleration private int ay; // 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