## Contents

## 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):

## 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.### Addition(top)

This calculation is very basic. You got one arrow and add it to another. Lets code it!

Code (Java):

*Vector#clone*method.

### 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):

### 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):

### 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):

*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):

**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){

//p is your player

//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 head location

Location head = p.getLocation().add(0, p.getEyeHeight(), 0);

//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

Vector direction = head.subtract(target).toVector().normalize();

//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);

}

//p is your player

//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 head location

Location head = p.getLocation().add(0, p.getEyeHeight(), 0);

//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

Vector direction = head.subtract(target).toVector().normalize();

//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);

}