# Solved How do vectors work?

Discussion in 'Plugin Development' started by Joiner, Aug 7, 2014.

Not open for further replies.
1. Offline

### Joiner

I need to understand how vectors work, how their mechanics work.
I already know how to calculate them (https://forums.bukkit.org/threads/tutorial-how-to-calculate-vectors.138849/) but I can't imagine them.

I can build a new instance with coordinates.
Code:java
`Vector vector = new Vector(int, int, int);`

What do these 3 numbers mean?

I can get a player direction.
Code:java
`Vector direction = Player.getLocation().getDirection();`

Where he's looking is calculated using yaw and pitch, but how is calculated his speed? And how is that speed stored in the vector?

Can you draw a simple picture to explain better a vector?

#1
2. Offline

### TheMcScavenger

Vector(double x, double y, double z)
Construct the vector with provided double components.
Vector(float x, float y, float z)
Construct the vector with provided float components.
Vector(int x, int y, int z)
Construct the vector with provided integer components

#2
3. Offline

#3
4. Offline

### AoH_Ruthless

Joiner
There is no simple way to explain this. The three double or integer values are x y and z coordinates. You have to understand vector algebra to be understand this proficiently.

http://emweb.unl.edu/math/mathweb/vectors/vectors.html
(Edit: While many topics on that tutorial are somewhat advanced and not necessary to know, you can create really cool effects with advanced vectors, and it's better to know more than you think you need)

I tried to put together a little diagram:

#4
5. Offline

### TheMcScavenger

I kinda did. You asked what the three numbers mean, giving you the Javadoc information (clearly showing it's x, y, z) is a pretty good answer. If you still don't understand it afterwards, you shouldn't be here.

#5
6. Offline

### Joiner

AoH_Ruthless Thank you very much.
If a vector associate two locations, why the constructor requires only 3 coordinates and not 6?

#6
7. Offline

### Necrodoom

Joiner it contains the difference of the 2 locations, not the locations themselves. You can apply this vector at any location you want and it wouldn't matter.
See the last paragraph in the picture.

#7
8. Offline

### AoH_Ruthless

TheMcScavenger
Actually, your answer was not adequate at all. The OP doesn't understand the basic concept of a vector and needed assistance wrapping his mind on what they are and what they do. The Documentation does not thoroughly explain what exactly a vector is (it is assumed you have the required Algebra to be using them) and just telling him about integer components offers no help at all.

Joiner
Because a vector is not directly related to the locations themselves. A vector is just a linking line between two locations (the difference between them). If I applied a vector 10 blocks higher or lower, it would be exactly the same, just with a different start and end point. A Vector is nothing more than a line with magnitude and direction. You can have positive and negative vectors and a velocity associated. More information

#8
9. Offline

### Joiner

Necrodoom AoH_Ruthless So this will throw diagonally?
Code:java
`Vector vector = new Vector(1, 2, -1);`

#9
10. Offline

### AoH_Ruthless

Joiner
In essence, yes. What are you trying to do exactly?

#10
11. Offline

### TheMcScavenger

One of his questions was "What do these three numbers mean", meaning that my answer to that question was good enough for him to understand it.

#11
12. Offline

### Joiner

AoH_Ruthless
Nothing. I know how to use them, but I want to understand them, too.
So, the 3 values represent the movement to each direction (X, Y, Z) in blocks. Is the speed calculated automatically? How?

#12
13. Offline

### AoH_Ruthless

TheMcScavenger
No, because the question about the meaning of the three numbers revolved around a lack of Vector understanding in general, not what an integer is.

Joiner
There is no speed in a vector. Vectors are lines with magnitude and direction.

#13
14. Offline

### Joiner

Player.setVelocity(Vector) makes the player move. Movement = Direction (vector) with speed. How is the speed calculated?

#14
15. Offline

### AoH_Ruthless

Joiner

If I do Player#setVelocity(new Vector(1, 2, 1)), they will be propelled one block + in terms of x and z and 'jump' 2 blocks into the air. The vector is not defining the speed, but the the direction in which the player will travel.

#15
16. Offline

### Joiner

AoH_Ruthless So, how is that speed calculated? Where is it defined?

#16
17. Offline

### LucasEmanuel

#17
Garris0n likes this.
18. Offline

### Chiller

Joiner A vector is a direction, speed is a magnitude, if you set player.setVelocity(vector(1, 2, 1)) the player will move 2 m in the y direction, aka jump.

#18
19. Offline

### coasterman10

The speed is the vector itself. When you give them a vector with components (x, y, z), they will move at those speeds in the respective directions.

#19
20. Offline

### mythbusterma

Uh no, they will jump up, slightly to the south-east.

#20
Garris0n likes this.
21. Offline

### unon1100

Vectors used for speed - The player will move the length of the vector in 1 tick.
Vectors for direction - Directions that entities face are the x/y/z distance relative to the entities head. getting the length will return 1.

#21
Joiner likes this.
22. Offline

### Joiner

unon1100 Thank you very much.
So, the player will have a speed of Vector.length() blocks/tick, right?

AoH_Ruthless Why Player.getVelocity().toString() always returns "0.0,0.0,0.0"?

EDIT by Moderator: merged posts, please use the edit button instead of double posting.

Last edited by a moderator: Jun 9, 2016
#22
23. Offline

### AoH_Ruthless

Joiner
Player most likely isn't moving ...

#23
24. Offline

#24
25. Offline

### Necrodoom

Joiner same when player jumps?

#25
26. Offline

### Joiner

Sorry, what do you mean?

#26
27. Offline

### Necrodoom

Joiner when player jumps, velocity is also 0?

#27
28. Offline

### Joiner

I was flying.

Bump.

EDIT by Moderator: merged posts, please use the edit button instead of double posting.

Last edited by a moderator: Jun 9, 2016
#28
29. Offline

Thank you.

#29