# Solved Calculating Vectors

Discussion in 'Plugin Development' started by CMG, Jul 2, 2014.

Not open for further replies.
1. Offline

### CMG

I'm kinda stuck at a wall here developing a plugin, this is something ive never actually come across when coding with the Bukkit API. Firstly I'm trying to create something similar to a kit ability on the PvP Dojo 2.0 servers. Its called Hedgehog and it stimulates their spikes on their backs popping up to defend themselves. I'm trying to achieve that effect, now before you start saying "I want ready bake code" i don't i just need some sort of explanation. My question is; how would i loop through the players pitch and yaw to launch arrows in every single direction away from the player, the arrow launching is simple, its something like:
Code:java
`Arrow a = (Arrow) p.launchProjectile(Arrow.class)..a.setVelocity...a.setMetadata...`

But i have no idea on how to calculate the vectors, im not looking for p.getLocation().getDirection() it needs to be in every pitch and yaw place possible (in front, behind, left, right and above the player, not down). So could someone help me with some methods or some psuedo code? I have no idea how to handle this.

Here is a video showing off hedgehog:
Hedgehog

If you can't see it that well, im sorry but i didn't want people to see my player name.

#1
2. Offline

### LucasEmanuel

If you want to calculate a vector that launches a player directly away from another player you can use vector subtraction, where one vector is the location of one player and the other vector is the location of the other player. Take a look at my vector tutorial in my signature to find out more.

#2
3. Offline

### CMG

Your tutorial is great! Its just that i have no idea how to loop through the players yaw/pitch, you have any idea on how i can do that?

LucasEmanuel I have definitely achieved something with your tutorial this is my code:
Code:java
`for (int pitch = -9; pitch <= 9; pitch++) {for (int yaw = 9; yaw <= 36; yaw++) {int actualPitch = pitch * 10;int actualYaw = yaw * 10;double pitch1 = ((actualPitch) * Math.PI) / 180;double yaw1 = ((actualYaw) * Math.PI) / 180; double x = Math.sin(pitch1) * Math.cos(yaw1);double y = Math.sin(pitch1) * Math.sin(yaw1); double z = Math.cos(pitch1); p.launchProjectile(Arrow.class, new Vector(x, y, z));}}`

And it launches the exact pattern i want, but it faces is south, is there anyway i can reverse this and make it face up?
Here is a picture:

Now those arrows are symmetrical each side and are pointing to south all the time.

Edit: Updated the code; because the bottom right part wasn't working fixed that now.. (Just added 9 onto the for loop final value for pitch)

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

Last edited by a moderator: Jun 9, 2016
#3
4. Offline

### ZodiacTheories

CMG

//OFFTOPIC

I really want hedgehog on PvPDojo 2.0, I'll trade it for prisoner and monkey?

#4
5. Offline

### CMG

*Nvm* You saw the video but whats your IGN? ZodiacTheories

#5
6. Offline

### ZodiacTheories

CMG

thesamster8, why?

#6
7. Offline

### CMG

Nevermind, just so i know who you are

#7
8. Offline

CMG

PM me yours?

#8
9. Offline

### CMG

Ok, this is the best i could do, the updated code is above and this is the result, a 3D semi - circle that points out like a dome in the south direction, i have spent hours trying to get it to point upwards but i have no luck, can anyone else help me? LucasEmanuel You're amazing with vectors can you help at all ? Im really stuck :/
http://gyazo.com/f9d69b80ec8cb3fcc14e4ae6b55d40bd

#9
10. Offline

### chasechocolate

CMG you shouldn't even have to use trigonometric functions for this, you should be able to do this with just loc.setPitch() and loc.setYaw().

#10
11. Offline

### CMG

chasechocolate Ok, but how would i calculate a vector? Because at the moment when i run the code it just shoots 100+ arrows in a bunch where im looking? Am i still doing it wrong?
Code:java
`for (int pitch = -9; pitch <= 9; pitch++) {            for (int yaw = 9; yaw <= 36; yaw++) {                int actualPitch = pitch * 10;                int actualYaw = yaw * 10;                 final Arrow a = (Arrow) p.launchProjectile(Arrow.class);                a.getLocation().setPitch(actualPitch);                a.getLocation().setYaw(actualYaw);                a.setKnockbackStrength(20);            }        }`

#11
12. Offline

### Rocoty

chasechocolate Tell me how you could do this by only setting the arrow's pitch and yaw? As far as I know, setting the angle of an arrow is redundant as it will be overridden by the arrow's velocity.

#12
13. Offline

### fireblast709

Rocoty it wouldn't even set it.
chasechocolate sounds like overkill to me. Plus it would use trig math either way.

#13
14. Offline

#14
15. Offline

### mkezar

CMG

//Semi OFF-TOPIC

but when your done your hedgehog thingy, could you send me it? It looks really cool

#15
16. Offline

### CMG

Sure man, im just at a wall here, i know hardly anything about vectors/and velocities and how to calculate this sort of situation.

#16
17. Offline

### fireblast709

CMG Assume a certain x, y and z which you calculate using trig math (just some spherical coords, using yaw and pitch). Then all you need to do to get the velocity is creating a new Vector(x, y, z), and perhaps multiply it if you want them to go faster. For the Location, after you call launchProjectile(Arrow.class, velocity), you should be able to teleport them to player.getEyeLocation().add(x, y, z) without any issues, placing them on the sphere around the player, pointing towards the velocity (which is pointing outwards from the player).

#17
18. Offline

### CMG

I got the first bit but how would i go about doing this ' placing them on the sphere around the player, pointing towards the velocity (which is pointing outwards from the player).' ?

#18
19. Offline

#19
20. Offline

### chasechocolate

CMG fireblast709 I was thinking something like this; not too complicated:
Code:java
`Location loc = player.getEyeLocation(); for(int pitch = 0; pitch >= -90; pitch -= 15){  for(int yaw = 180; yaw >= -180; yaw -= 15){    loc.setYaw(yaw);    loc.setPitch(pitch);     Arrow arrow = player.launchProjectile(Arrow.class);     arrow.setVelocity(loc.getDirection());  }}`

#20
21. Offline

### CMG

And thats the same for every single arrow launched? Im really confused on how i can do this, do i still use the original code below or is it something completely different?
Code:java
`    for (int pitch = -9; pitch <= 9; pitch++) {            for (int yaw = 9; yaw <= 36; yaw++) {                int actualPitch = pitch * 10;                int actualYaw = yaw * 10;                double pitch1 = (actualPitch * Math.PI) / 180;                double yaw1 = (actualYaw * Math.PI) / 180;                 double x = Math.sin(pitch1) * Math.cos(yaw1);                double y = Math.sin(pitch1) * Math.sin(yaw1);                 double z = Math.cos(pitch1);                 final Arrow a = (Arrow) p.launchProjectile(Arrow.class,                        new Vector(x, y, z));                a.setKnockbackStrength(20);                a.teleport(p.getEyeLocation().add(new Vector(1.0, 1.0, 1.0)));}}`

Edit : Nvm

chasechocolate wow thanks chase, that works exactly as i thought it would I can't believe i was that stupid.

Ive got to say, thanks so much to everyone who contributed into this(@chasechocolate, fireblast709, LucasEmanuel), finally its working the way i wanted it

However i have one final question, how would i go about slowing the velocity down, if i try .multiply(-1.2) or something wouldn't that just turn it in the opposite direction?

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

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

### fireblast709

Justified
CMG you would multiply it with a number close to zero, like 0.5 (negative numbers will reverse the velocity, like you expected). Do note that the arrow might fall down (pretty much) immediately when the velocity is low!

#22
23. Offline

### LucasEmanuel

By multiplying a vector by o.2 is basically scaling it down to 20% if it was normalized beforehand.

#23
24. Offline

### CMG

fireblast709 LucasEmanuel So would i have to create a repeating task that will keep settings the velocity so it travels relatively slow? because it should pop out fast then stay for a while moving slowly.

#24
25. Offline

### LucasEmanuel

No you don't need a task for that.

#25
26. Offline

### fireblast709

LucasEmanuel Don't forget 'Screw gravity' doesn't work here
CMG The easiest way to determine that is to try it out without a task first. You most likely need to cancel out gravity somehow, which could possible be done by setting the velocity each tick the arrow 'lives'

#26
27. Offline

### LucasEmanuel

It's rather tricky to make something move slowly through air in a straight line without looking laggy/glitchy. There is a really good thread discussing this in the resource section and I recommend taking a look at it.

#27
28. Offline

### CMG

Yeah that's what im trying to do, here's the code im using:
Code:java
`final Vector newLocation = loc.getDirection(); arrow.setVelocity(loc.getDirection().multiply(1.7));arrow.setKnockbackStrength(25);arrow.setMetadata("hegehog",new FixedMetadataValue(Main.get(), "hedge")); final int id = Bukkit.getScheduler().scheduleSyncRepeatingTask(Main.get(), new Runnable() {public void run() {arrow.setVelocity(newLocation.multiply(0.5));}}, 5l, 1l); Bukkit.getScheduler().scheduleSyncDelayedTask(Main.get(),new Runnable() {public void run() {Bukkit.getScheduler().cancelTask(id);arrow.remove();}}, 20l * 4);}`

And this is the effect:

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

### LucasEmanuel

I don't have a link for it, you will have to search.

#29
30. Offline

thanks man

#30