Tutorials » 3D Math » How to use 3D Vectors

The Untold Engine represents 3D vectors as U4DEngine::U4DVector3n. A U4DEngine::U4DVector3n provides several linear algebraic operations such as:

  • Addition
  • Subtraction
  • Scalar Multiplication
  • Dot Product
  • Cross Product
  • etc

Vector Declaration

There are two ways to declare a 3D Vector. You can declare a 3D vector using the default constructor which creates a vector with x, y and z components equal to zero.

//Declare a vector
U4DEngine::U4DVector3n n;

You can also declare a vector providing the x, y and z coordinates

//A 3D vector with x, y and z coordinates
U4DEngine::U4DVector3n n(1.0,2.0,3.0);

Addition

3D vectors can be added in two different ways:

You can use the following method to add two vectors: U4DEngine::U4DVector3n::operator+()

//1st Method
    
//declare vectors
U4DEngine::U4DVector3n n(1.0,2.0,3.0);
U4DEngine::U4DVector3n v(3.0,2.0,1.0);

//Addition
U4DEngine::U4DVector3n m=n+v;

Or you can use method U4DEngine::U4DVector3n::operator+=() to add two vectors

//2nd Method

//declare vectors
U4DEngine::U4DVector3n p(1.0,2.0,3.0);
U4DEngine::U4DVector3n z(3.0,2.0,1.0);

//Addition
p+=z;

Subtraction

Just like the addition operation, 3D vectors can be subtacted in two different ways.

Two vectors can be subracted by using the method U4DEngine::U4DVector3n::operator-(), as shown below

//1st Method

//declare vectors
U4DEngine::U4DVector3n n(1.0,2.0,3.0);
U4DEngine::U4DVector3n v(3.0,2.0,1.0);

//Subtraction
U4DEngine::U4DVector3n m=n-v;

or the vectors can be subtracted using method U4DEngine::U4DVector3n::operator-=()

//2nd Method

//declare vectors
U4DEngine::U4DVector3n p(1.0,2.0,3.0);
U4DEngine::U4DVector3n z(3.0,2.0,1.0);

//Subtraction
p-=z;

Scalar Multiplication

There are also two ways to multiply a 3D vector by a scalar.

One method uses U4DEngine::U4DVector3n::operator*()

The snippet below shows an example:

//1st Method

//declare vectors
U4DEngine::U4DVector3n n(1.0,2.0,3.0);

//Multiply by a scalar
U4DEngine::U4DVector3n m=n*2.0;

The second method uses

U4DEngine::U4DVector3n::operator*=()

The snippet below shows an example:

//2nd Method

//declare vectors
U4DEngine::U4DVector3n p(3.0,2.0,1.0);

//Multiply by a scalar
p*=2.0;

Dot Product

Dot Product operation is performed in either two ways.

One way to compute the Dot Product is using the following method U4DEngine::U4DVector3n::dot(), as is shown below:

//1st Method

//declare vectors
U4DEngine::U4DVector3n n(1.0,2.0,3.0);
U4DEngine::U4DVector3n p(3.0,2.0,1.0);

//dot product
float d=n.dot(p);

The second method uses the following method:

U4DEngine::U4DVector3n::operator*() The snippet below shows an example:

//2nd Method

U4DEngine::U4DVector3n m(1.0,2.0,3.0);
U4DEngine::U4DVector3n z(3.0,2.0,1.0);

//dot product
float b=m*z;

Cross Product

To obtain the Cross Product between two 3D vectors, you can either use U4DEngine::U4DVector3n::cross()

//1st Method

//declare vectors
U4DEngine::U4DVector3n n(1.0,2.0,3.0);
U4DEngine::U4DVector3n p(3.0,2.0,1.0);

//cross product
U4DEngine::U4DVector3n c=n.cross(p);

Or you can use method U4DEngine::U4DVector3n::operator%()

//2nd Method

U4DEngine::U4DVector3n m(1.0,2.0,3.0);
U4DEngine::U4DVector3n z(3.0,2.0,1.0);

//cross product
U4DEngine::U4DVector3n q=m%z;

Angle between 3D Vectors (in degrees)

You can also obtain the angle between 3D vectors by using method U4DEngine::U4DVector3n::angle()

//declare vectors
U4DEngine::U4DVector3n n(1.0,2.0,3.0);
U4DEngine::U4DVector3n p(3.0,2.0,1.0);

//get angle
float angle=n.angle(p);