Basic simulation and evaluation

MadKnight
1,126 views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

What is a vector?

Vectors in games are used to store things like position, velocity, or direction. For example, we can store position of a pod in a vector, velocity in another vector, and a 3rd vector for pod's direction. Vector itself is just an x;y coordinate, but it can be both an absolute coordinate or a relative vector, depends on how you use it. so for an easier understanding we will have 2 types - type Point and type Vector. Points are just some coordinates in space while vectors are usually some direction and a length stored as a relative x;y coordinate. Relative vectors are usually used to do something with absolute points

Grid

The position vector is just a point stored as x;y that indicates us where is the pod. The velocity vector is a relative vector that shows us what distance would the pod move in one game turn if it won't change: its length indicates the pod's speed, and its direction indicates the direction the pod is flying in. The direction vector indicates in what direction the pod will accelerate using its thrust. The direction's length is 1.

3D

Vector addition

Since relative vectors are just some "displacements", we can "apply" these displacements on other vectors by adding one to another. But we can't add a point to another point because they're absolute coordinates and don't have any kind of "displacement", so if you do this, you won't get anything that makes sense in the result

To add vectors together, you just add each component together separately. For example:

1
22
23
27
28
29
30
struct Vector {...}
// {...}
Vector a = new Vector(2, 5);
Vector b = new Vector(3, 7);
System.Console.WriteLine(a + " + " + b + " = " + (a + b));
// {...}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Why do we want to add vectors together? One of the most common applications in games for vector addition is physics integration. Any physically-based object will likely have vectors for position, velocity, and acceleration. For every frame (usually 1/60th of a second), we have to integrate these vectors -- that is, add the velocity to the position, and the acceleration to the velocity.

Let's consider the example of Mario jumping. He starts at position (0,0). As he starts the jump, his velocity is (1,3) -- he is moving upwards quickly, but also to the right. His acceleration throughout is (0,-1), because gravity is pulling him downwards. Here is what his jump looks like over the course of seven more frames. The black text specifies his velocity for each frame.

Vector Addition

We can walk through the first couple frames by hand to see how this works.

For the first frame, we add his velocity (1,3) to his position (0,0) to get his new position (1,3). Then, we add his acceleration (0,-1) to his velocity (1,3) to get his new velocity (1,2).

We do it again for the second frame. We add his velocity (1,2) to his position (1,3) to get (2,5). Then, we add his acceleration (0,-1) to his velocity (1,2) to get (1,1).

Usually in games the player controls a character's acceleration with the keyboard or gamepad, and the game calculates the new velocity and position using physics integration (via vector addition). Fun fact: this is the same kind of integration problem that you solve using integral calculus - we are just using an approximate brute-force approach. I found it much easier to pay attention to calculus classes by thinking about physical applications like this.

Vector subtraction

Subtraction works in the same way as addition -- subtracting one component at a time. Vector subtraction is useful for getting a vector that points from one position to another. For example, let's say the player is standing at (1,2) with a laser rifle, and an enemy robot is at (4,3). To get the vector that the laser must travel to hit the robot, you can subtract the player's position from the robot's position. This gives us:

1
22
23
27
28
29
30
struct Vector {...}
// {...}
Vector a = new Vector(10, 7);
Vector b = new Vector(3, 1);
System.Console.WriteLine(a + " - " + b + " = " + (a - b));
// {...}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Vector subtraction

Scalar-vector multiplication

When we talk about vectors, we refer to individual numbers as scalars. For example, (3,4) is a vector, 5 is a scalar. In games, it is often useful to multiply a vector by a scalar. For example, we can simulate basic air resistance by multiplying the player's velocity by 0.9 every frame. To do this, we just multiply each component of the vector by the scalar. If the player's velocity is (10,20), the new velocity is:

1
22
23
27
28
29
30
31
struct Vector {...}
// {...}
Vector a = new Vector(0.25f, 0.5f);
float b = 4;
System.Console.WriteLine(a + "*" + b + " = " + (a*b));
// {...}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content