# Solved Vector

Discussion in 'Spigot Plugin Development' started by Mystic_Mark_, Jul 23, 2018.

1. I'm trying to spawn particles in a line from point a to point b, This is working fine but i also need the for loop to start at 0. But if i set him to 0.0 the vectors turn in (NaN, NaN, NaN) how can i fix this?

Code (Text):
Location from = point.as.getLocation().clone();
Location to = as.getLocation().clone();

Vector dir = to.clone().subtract(from).toVector();
double distance = dir.length();

Bukkit.broadcastMessage(distance + "");
for (double i = 0.01D /* This number needs to be 0*/; i <= distance; i += 0.1) {
dir.multiply(i);

from.add(dir);
from.getWorld().spawnParticle(Particle.HEART, from.getX(), from.getY(), from.getZ(), 10);
//Bukkit.broadcastMessage(dir.toString());
from.subtract(dir);
dir.normalize();

2. ### Kilian544

Know your maths, you cannot multiply by "0". You dufus!

• Funny x 1
3. ### JustRayz

You can, but you get 0. Dufus.

4. ### jetp250

The NaN is because you're first multiplying by 0 (thus setting x/y/z to 0) and then normalizing. What normalizing does, it takes the length of the vector and divides the components with the length, like this:
Code (Java):
double length = Math.sqrt(x*x + y*y + z*z);
x /= length;
y /= length;
z /= length;
But because the x, y and z are 0, the length is 'sqrt(0*0 + 0*0 + 0*0)' = sqrt(0) = 0. this then means you're dividing 0 by 0, which is the definition of NaN in java.

For a solution, since you know the step size (0.1 in this case), just normalize the direction before the loop, and multiply it by the step size (0.1), and then just add the dir vector to the location each iteration. Don't multiply it, don't normalize it, it's already what you want.

Edit: also, this way, don't subtract the dir from the location. Otherwise you'd always spawn the particle at the same location. So this:
Code (Java):
for( ... ) {
from.add(dir);
// spawn particle at 'from'
}
is enough for the loop.

• Winner x 1
5. ### Kilian544

Whoops I meant dividing, normalizing uses dividing.

6. Thank you did'nt think of that. Problem solved!