Solved Logic Error with vectors and simple math.

Discussion in 'Plugin Development' started by srspore, Sep 21, 2015.

Not open for further replies.
1. Offline

srspore

What I am trying to do: I want the target player (t) to get pushed away from the player (p) the same amount every time. This is the code I have:
Code:
```for (Player t : Bukkit.getServer().getOnlinePlayers()) {
double scale = Math.sqrt(Math.pow((t.getLocation().getX() - p.getLocation().getX()), 2) + Math.pow((t.getLocation().getY() - p.getLocation().getY()), 2) + Math.pow((t.getLocation().getZ() - p.getLocation().getZ()), 2));
if (scale < 0.00001) scale = 0.00001;
double time = 1;
// I multiplied some of the values in attempt to debug, no functional reason other than that.
p.sendMessage("X: " + (int) (t.getLocation().getX() + (t.getLocation().getX() - p.getLocation().getX())/scale*time) + "Y: " + (int) (t.getLocation().getY() + (t.getLocation().getY() - p.getLocation().getY())/scale*time) + "Z: " + (t.getLocation().getZ() + (t.getLocation().getZ() - p.getLocation().getZ())/scale*time));
// this is what I'm sending to the player vvvv
double x = (t.getLocation().getX() + (t.getLocation().getX() - p.getLocation().getX())/scale*time);
double y = (t.getLocation().getY() + (t.getLocation().getY() - p.getLocation().getY())/scale*time);
double z = (t.getLocation().getZ() + (t.getLocation().getZ() - p.getLocation().getZ())/scale*time);
p.sendMessage(t.getName() + ": " + x + " " + y + " " + z);
Vector v = new Vector(x,y,z);
p.sendMessage(t.getName() + ": " + v);
t.setVelocity(v);
}```
This is what gets sent to player "p":"

Sorry it's so messy, I tried to outline the whole numbers in red, not sure if that was helpful
Another thing to note is the location of "srspore" prior to moving along the vector was 72 100 213 meaning the server sent the coordinates of the player, so either I messed up simple math or something is weird in my syntax.
Thank you so much for looking through this mess of code and chat messages!

#1
2. Offline

blablubbabc

// Vector with length 1 pointing from p to t:
Vector dir = t.getLocation().subtract(p.getLocation()).toVector().normalize();
double strength = 2.0D; // some fixed strength
// pushing t in the given direction with the given strength:
t.setVelocity(dir.multiply(strength));

#2
3. Offline

srspore

@blablubbabc Thank you! This is exactly what I wanted! I don't fully understand how it works, if anyone could explain it to me that would be great but that isn't like crucial.

#3
4. Offline

Xerox262

Vector dir = t.getLocation().subtract(p.getLocation()).toVector().normalize(); // This gets the target's location then removes the player's current location from it (means that the closer they are the less power the launch will have)
double strength = 2.0D // This is just how much you want to multiply the original launch by, if you want to send them further increase this (It should stay below 10 or something or you'll get the annoying "Player moved too fast" messages in console)
t.setVelocity(dir.multiply(strength)); // This is the part that actually sends the player in the direction away from the player, you can just remove the word strength and replace it with a number, but it doesn't effect anything

Hope you understand what this means now

P.S. Don't forget to set this thread to solved

#4
5. Offline

#5