# Vector Programming for Beginners

Jun 21, 2017
Vector Programming for Beginners
• Vector Programming for Beginners

Learn what vectors are and how understanding them will benefit your code

## What is a vector?(top)

A vector is represented by a length and a direction and always describes a movement from one point to another. Most of you will already have heard about vectors or seen some strange arrows representing them. Let's have a look at the vector in Spigot.

## The vector in Spigot(top)

The vector in Spigot can be found in org.bukkit.util (JavaDoc) package and can be created the following ways:

Code (Java):
Vector v = new Vector();  //Creates a vector with length 0 and NO direction
Vector v = new Vector(x, y, z);  //Creates a vector with defined direction and length
Each vector has three values that represent the vector, called X, Y, and Z (the 3 directions). Also important to know is that you can get the length of a vector by calling .length() and subsequently get / set the value of every axis by calling .getX(), .getY(), .getZ(), and .setX(x), .setY , .setZ(z).

## What is the use of a vector in Spigot?(top)

Whenever an entity moves or the target you're are looking at has to be calculated, a vector is used. At the end of this tutorial, you should be able to do both.

Let's explain the possible calculations.

* The blue arrow is always the result of the calculation. This calculation is very basic. You got one arrow and add it to another. Lets code it!

Code (Java):
Vector first = new Vector(1, 3, 2);
Vector second = new Vector(3, -1, 4);

Vector result = first.add(second); // Result is now a Vector of 4, 2, 6

### Multiplication(top) The second simple (and essential) calculation. Just multiply the length of the vector. When the value given is NEGATIVE, the vector switches its direction.

Code:
Code (Java):
Vector v = new Vector(3, 4, 2);

Vector result1 = v.multiply(2);  // Vector of 6, 8, 4
Vector result2 = v.multiply(-1); // Vector of -3, -4, -2
As you see, every value of the vector is being multiplied with the value you specify.

### Normalize(top)

Sets the vectors length to 1. For example, you have a vector (3, 3, 3). Then, its length is the √( (3*3)+(3*3)+(3*3) ) = √(27) = 5.19, so the vector's length is 5.19.

Normalizing now has to divide the vector's length by its length to get the resulting vector. So normalizing calls .multiply(1/5.19) resulting in a vector of (0.57, 0.57, 0.57).

Code:
Code (Java):
Vector v = new Vector(3, 3, 3);

Vector result = v.normalize();  // Returns vector of length 1 and movement of 0.57, 0.57, 0.57

### Crossproduct(top) This calculation returns a vector that is orthogonal to both of the previous vectors. Its length is the area of the light blue parallelogram. This can be used to compare if two vectors are nearly the same (for example, when targeting a player).

Example:
Code (Java):
Vector first = new Vector(1, 2, 3);
Vector second = new Vector(-7, 8, 9);

Vector result = first.crossProduct(second); // Will return vector of -6, -30, 22 Source of example: Wikipedia (didn't want to draw these brackets)

### Midpoint(top)

This is easy! Take one vector, add it to another, and divide by 2. Then you have your midpoint! You have two vectors, the black and the orange one. You add them together and get the green vector. You divide the green vector by 2 or multiply it by 0.5 and get your midpoint.

Code:
Code (Java):
Vector first = new Vector(1, 3, 4);
Vector second = new Vector(4, 3, 1);

Vector midpoint1 = first.midpoint(second); // Vector of 2.5, 3, 2.5
Vector midpoint2 = first.add(second).multiply(0.5); // Vector of 2.5, 3, 2.5
Now you know how to use vectors! Time to practice a bit!

## Example(top)

Let's check if a player targets another location!

Code (Java):
public boolean doesPlayerTarget(Player p, Location target){
//target is the location the player might target

//Check if they are in the same world
if(!target.getWorld().equals(p.getWorld()))return false;

//Let's begin!
//Get the players looking direction as vector and
// shorten it to a length of 1 by using normalize()
Vector look = p.getLocation().getDirection().normalize();

//Get the direction of the target from the player by substracting his location with the target
//Again use normalize() of course, to shorten the value

//Lets build our crossProduct. When the crossProduct's length is 0, the player
//is exactly targeting our target location
//why? because then the parallelogram shown above has an area of 0 :)

Vector cp = direction.crossProduct(look);

//Lets get the length from the vector
double length = cp.length();

//If the length is bigger than 0.1 the player is probably
//Not targeting our location. Choose this value appropriate
return (length < 0.1);
}