Solved Calculating Vectors

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

Thread Status:
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
    1. Arrow a = (Arrow) p.launchProjectile(Arrow.class)..
    2. a.setVelocity...
    3. 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.
     
  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. :)
     
  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
    1. for (int pitch = -9; pitch <= 9; pitch++) {
    2. for (int yaw = 9; yaw <= 36; yaw++) {
    3. int actualPitch = pitch * 10;
    4. int actualYaw = yaw * 10;
    5. double pitch1 = ((actualPitch) * Math.PI) / 180;
    6. double yaw1 = ((actualYaw) * Math.PI) / 180;
    7.  
    8. double x = Math.sin(pitch1) * Math.cos(yaw1);
    9. double y = Math.sin(pitch1) * Math.sin(yaw1);
    10.  
    11. double z = Math.cos(pitch1);
    12.  
    13. p.launchProjectile(Arrow.class, new Vector(x, y, z));
    14. }
    15. }

    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:

    [​IMG]
    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
  4. Offline

    ZodiacTheories

    CMG

    //OFFTOPIC :p

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

    CMG

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

    ZodiacTheories

    CMG

    thesamster8, why?
     
  7. Offline

    CMG

    Nevermind, just so i know who you are :)
     
  8. Offline

    ZodiacTheories

    CMG

    PM me yours?
     
  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
     
  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().
     
  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
    1. for (int pitch = -9; pitch <= 9; pitch++) {
    2. for (int yaw = 9; yaw <= 36; yaw++) {
    3. int actualPitch = pitch * 10;
    4. int actualYaw = yaw * 10;
    5.  
    6. final Arrow a = (Arrow) p.launchProjectile(Arrow.class);
    7. a.getLocation().setPitch(actualPitch);
    8. a.getLocation().setYaw(actualYaw);
    9. a.setKnockbackStrength(20);
    10. }
    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.
     
  13. Offline

    fireblast709

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

    CMG

  15. Offline

    mkezar

    CMG

    //Semi OFF-TOPIC

    but when your done your hedgehog thingy, could you send me it? :p It looks really cool :p
     
  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.
     
  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).
     
  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).' ?
     
  19. Offline

    fireblast709

    CMG Entity#teleport(eyeLocation.add(new Vector(x,y,z)))
     
  20. Offline

    chasechocolate

    CMG fireblast709 I was thinking something like this; not too complicated:
    Code:java
    1. Location loc = player.getEyeLocation();
    2.  
    3. for(int pitch = 0; pitch >= -90; pitch -= 15){
    4. for(int yaw = 180; yaw >= -180; yaw -= 15){
    5. loc.setYaw(yaw);
    6. loc.setPitch(pitch);
    7.  
    8. Arrow arrow = player.launchProjectile(Arrow.class);
    9.  
    10. arrow.setVelocity(loc.getDirection());
    11. }
    12. }
     
  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
    1. for (int pitch = -9; pitch <= 9; pitch++) {
    2. for (int yaw = 9; yaw <= 36; yaw++) {
    3. int actualPitch = pitch * 10;
    4. int actualYaw = yaw * 10;
    5. double pitch1 = (actualPitch * Math.PI) / 180;
    6. double yaw1 = (actualYaw * Math.PI) / 180;
    7.  
    8. double x = Math.sin(pitch1) * Math.cos(yaw1);
    9. double y = Math.sin(pitch1) * Math.sin(yaw1);
    10.  
    11. double z = Math.cos(pitch1);
    12.  
    13. final Arrow a = (Arrow) p.launchProjectile(Arrow.class,
    14. new Vector(x, y, z));
    15. a.setKnockbackStrength(20);
    16. a.teleport(p.getEyeLocation().add(new Vector(1.0, 1.0, 1.0)));
    17. }
    18. }


    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
  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!
     
  23. Offline

    LucasEmanuel

    By multiplying a vector by o.2 is basically scaling it down to 20% if it was normalized beforehand.
     
  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.
     
  25. Offline

    LucasEmanuel

    No you don't need a task for that.
     
  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'
     
  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.
     
  28. Offline

    CMG

    Yeah that's what im trying to do, here's the code im using:
    Code:java
    1. final Vector newLocation = loc.getDirection();
    2.  
    3. arrow.setVelocity(loc.getDirection().multiply(1.7));
    4. arrow.setKnockbackStrength(25);
    5. arrow.setMetadata("hegehog",
    6. new FixedMetadataValue(Main.get(), "hedge"));
    7.  
    8. final int id = Bukkit.getScheduler().scheduleSyncRepeatingTask(
    9. Main.get(), new Runnable() {
    10. public void run() {
    11. arrow.setVelocity(newLocation.multiply(0.5));
    12. }
    13. }, 5l, 1l);
    14.  
    15. Bukkit.getScheduler().scheduleSyncDelayedTask(Main.get(),
    16. new Runnable() {
    17. public void run() {
    18. Bukkit.getScheduler().cancelTask(id);
    19. arrow.remove();
    20. }
    21. }, 20l * 4);
    22. }

    And this is the effect:


    LucasEmanuel Can you please link me to this thread?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  29. Offline

    LucasEmanuel

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

    mkezar

    thanks man
     
Thread Status:
Not open for further replies.

Share This Page