Vector in Game Development - Understand The Basics of Vector Math
Vector in Game Development - Understand The Basics of Vector Math
Vector In Game
25
SHARES
Development:
25
Understand The
Basics Of Vector
0
0
Math
ou know this moment would come, it’s time to speak about math. But
what you’re going to see here is the basic math used for the
development of video games, to be more specific, we’re going to
see how to use vector in game development and all the
https://gamedevelopertips.com/vector-in-game-development/ 1/18
Y
03/03/2020 Vector in game development: Understand the basics of vector math
operations around
this “math”
instrument.
0
In this first post, you’re going to see the basic entity that you’ll
probably find most in any games that you’re going to develop, the
Vector.
https://gamedevelopertips.com/vector-in-game-development/ 2/18
03/03/2020 Vector in game development: Understand the basics of vector math
The arrow’s length is its magnitude and the tip of the arrow indicate
the direction of the vector itself.
with the direction where the force is applied. Same goes for a bullet
25 shot from a gun.
0
0 Have you ever played the game Angry Birds? The force and the
direction applied to the bird can also be described using
vectors. And what about the velocity of a car running in the last
amazing racing game that we built? That also can be described using
vectors. And the list goes on.
https://gamedevelopertips.com/vector-in-game-development/ 3/18
03/03/2020 Vector in game development: Understand the basics of vector math
Scalar vector
A scalar vector is nothing more than the magnitude representation of
25 the vector and is usually written in italics ( e.g v) while vectors are
SHARES
written in boldface ( e.g., v).
25
Use vector to represent a point in space
0 Image we want to describe where an object is placed in our 3D or 2D
0
game, how can we do that? Taken the origin of our 3D world, we can
draw a vector from it to the game object and this is the vector that
describes the position of the game object inside the game world.
If you look at the image you’ll notice that the vector in question is
nothing more than the coordinate of X,Y,Z in a 3D cartesian system
which in our case represent the game world space.
https://gamedevelopertips.com/vector-in-game-development/ 4/18
03/03/2020 Vector in game development: Understand the basics of vector math
Orientation of vectors
25
SHARES
Left-hand system vs right-hand system
25
In the right-hand system (RH), when curling your fingers around the
z-axis with the thumb pointing toward the z positive direction, your
fingers point from the x-axis toward the y-axis.
0
0 In the left system, it is the same method but using the left hand.
It’s important to know which system your engine is using and stick to
that convention when using it.
Vector operations
Let’s take a look at some of the basic operations that we can do with
vectors and how they can be used inside your games.
https://gamedevelopertips.com/vector-in-game-development/ 5/18
03/03/2020 Vector in game development: Understand the basics of vector math
Multiplications by a scalar
Multiplication by a scalar has the effect to change the magnitude of
the vector. We simply multiply each component of the vector with
the scalar value.
25
Addition and subtraction
Adding and subtracting vectors are quite easy Given two vectors A
0 and B, the addition of the 2 is the sum of the single components of the
vector.
0
For example :
https://gamedevelopertips.com/vector-in-game-development/ 6/18
03/03/2020 Vector in game development: Understand the basics of vector math
25
SHARES
25
0
0
Normal vector
A vector is said to be normal to a surface if it is perpendicular to that
surface.
Normalized vector
https://gamedevelopertips.com/vector-in-game-development/ 7/18
03/03/2020 Vector in game development: Understand the basics of vector math
Dot.product
25
SHARES
25
0
0
The Dot Product can be helpful when finding the angle between 2
vectors and by consequence, if the 2 vectors are perpendicular to each
other. If the dot product is zero, then the 2 vectors are perpendicular.
https://gamedevelopertips.com/vector-in-game-development/ 8/18
03/03/2020 Vector in game development: Understand the basics of vector math
25
SHARES
25
0
0
Image from: cdn.tutsplus.com
Cross.product
https://gamedevelopertips.com/vector-in-game-development/ 9/18
03/03/2020 Vector in game development: Understand the basics of vector math
25
SHARES
25
0
0
https://gamedevelopertips.com/vector-in-game-development/ 10/18
03/03/2020 Vector in game development: Understand the basics of vector math
Example:
25
SHARES
25
0
0
With the cross product, we can find C, which is the axes where we can
rotate the tank in our game.
https://gamedevelopertips.com/vector-in-game-development/ 11/18
03/03/2020 Vector in game development: Understand the basics of vector math
25
0 Where β is a value between 0 and 1 where 0 represents the initial
position of the Lerp and 1 as the final position.
0
As you can see from the definition, we’re representing the lerp as a
Vector.
https://gamedevelopertips.com/vector-in-game-development/ 12/18
03/03/2020 Vector in game development: Understand the basics of vector math
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
public Vector3 startPosition;
25
SHARES public Vector3 endPosition;
public float speed = 1.0F;
25
private float startTime;
private float journeyLength;
void Start() {
0
startTime = Time.time;
0 journeyLength = Vector3.Distance(startPosition,
endPosition);
}
void Update() {
float distCovered = (Time.time - startTime) * speed;
float fracJourney = distCovered / journeyLength;
transform.position =
Vector3.Lerp(startPosition,endPosition, fracJourney);
}
}
As you can see, we’ve used vectors to describe the start position and
the end position of the lerp. The
Vector3.Lerp(https://docs.unity3d.com/ScriptReference/Vector3.Lerp.html)
is a function from Unity that executes the LERP operation for you.
Model space
The model space is the reference system used by the object in
question.
Up: is the direction that point towards the top of the object
25
The left or right are the axes that points to its left or right sides
respectively.
0
0 Example with a plane:
World coordinate
A word coordinate is a fixed coordinate in which position, orientation,
and scale of all the elements placed in the game are described.
https://gamedevelopertips.com/vector-in-game-development/ 14/18
03/03/2020 Vector in game development: Understand the basics of vector math
View space
The view space coordinates are often called as the camera
coordinates. These are the coordinates fixed or attached to the game
25 camera.
SHARES
25
0
0
As you can see from the image, you can have a right or left hand
coordinate system attached to the camera.
https://gamedevelopertips.com/vector-in-game-development/ 15/18
03/03/2020 Vector in game development: Understand the basics of vector math
25
SHARES
As you can see from the example, the coordinates of the red box
25 (child) are expressed as a vector from the white box (parent)
position.
Raycasting
0
0
https://gamedevelopertips.com/vector-in-game-development/ 16/18
03/03/2020 Vector in game development: Understand the basics of vector math
Here we want to shoot a projectile and find if we have hit the target
(box). As you can see, the Raycast function takes two vectors as a
parameter. One is the vector “origin” which describes the origin
position of the shot in the game world. The other is the ray direction,
which describes where the projectile is headed.
The other parameters are not important for this lecture, but the first 2
25
SHARES vectors are essential to discuss as they describe the position and the
direction of the shot
25
https://gamedevelopertips.com/vector-in-game-development/ 17/18
03/03/2020 Vector in game development: Understand the basics of vector math
Save this article and come back to visit it every time you need to
remember something about the amazing world of vectors.
SHARE THIS:
(/vector-in-game-development/?share=twitter&nb=1)
25
(/vector-in-game-development/?share=facebook&nb=1)
SHARES
(/vector-in-game-development/?share=google-plus-1&nb=1)
25
0
0
https://gamedevelopertips.com/vector-in-game-development/ 18/18