# [Tutorial] Vector for beginners

Discussion in 'Spigot Plugin Development' started by Friwi, May 26, 2015.

Not open for further replies.
1. ### Friwi

Hello,

today I will create this little very basic vector tutorial.

Contents:

• What is a vector?
• The vector in Spigot
• The use of vectors in Spigot
• Vectorcalculations
2. Multiplication
3. Normalize
4. Crossproduct
5. Midpoint
• Example

What is a vector?

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.
Lets have a look at the vector in Spigot.

The vector in Spigot

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

Code (Text):
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 3 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 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?

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

Lets 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 (Text):
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:

The second simple and essential calculation. Just multiply the length of the vector. When the value given is NEGATIVE the vector switches his direction. Code:

Code (Text):
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:

Sets the vectors length to 1. For example you got a vector:

3, 3, 3

Then its length is root of 3*3+3*3+3*3 = root of 27 = 5.19 so the vectors length is 5.19

Normalize now has to divide the vectors length by its length to get the resulting vector.
So normalize calls .multiply(1/5.19) resulting in a vector of:

0.57, 0.57, 0.57

Code:
Code (Text):
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:

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 2 vectors are nearly the same (for example when targeting a player). Example:

Code (Text):
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:

This is easy! Take one vector, add it to another and divide by 2. Then you got your midpoint!

You got 2 vectors. The black and the orange one. You add them and get the green vector. You divide the green vector by 2 or multiply it by 0.5 and get your midpoint.

Code (Text):
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 practise a bit!

Lets check if a player targets another location!

Code (Text):
public boolean doesPlayerTarget(Player myplayer, Location targetloc){
Player p = myplayer;
//This is the location the player might target
Location target = targetloc;

//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
if(length<0.1){
return true;
}else{
return false;
}
}
If there are any questions feel free to replay below here or PM @Friwi!

Keep on learning!

~Friwi

#1
Last edited: May 26, 2015
• Like x 3
• Useful x 3
2. ### ProJoosh

Nice Tutorial, this can help alot of people Just alot to read.

3. ### fujiboy4

I was able to create a simple double jump with this:
Code (Text):
@EventHandler
public void move(PlayerMoveEvent e) {
final Player p = e.getPlayer();

if (p.isOnGround()) {
p.setAllowFlight(true);

} else {

}
}

@EventHandler
public void toggleFlight(PlayerToggleFlightEvent e){
if (p.getGameMode(GameMode.CREATIVE)) {

} else {

p.setVelocity(new Vector(0,1,0));
p.setAllowFlight(false);
e.setCancelled(true);
}
}

4. ### Friwi

Yeah, but this is not much about using vectors I think my example is better

5. ### fujiboy4

I know it is. Just showing how I was able to create a double jump with YOUR vector tutorial.

Ah ok nice

7. ### ZyphiorMC

Thanks for the tutorial, will use this when I re-code my Paintball plugin in about a week. Planning to have Paintball guns and special perks.

• Like x 1