Hi all. I read in a tutorial that in order to find the angle between two vectors you need to normalise and perform a dot product on them, then provide this dot product to the arc cosine function to get the angle. Hereās my code below and I keep getting NaN errors, which seems to be because the dot product isnāt unit length, although the tutorial hadnāt mentioned this.
Vec2 v1 = new Vec2(x, y);
Vec2 v2 = new Vec2(Main.player.x, Main.player.y);
v1.noralise();
v2.noralise();
double dot = v1.dot(v2);
double ang = Math.acos(dot);
Also here are the normalisation and dot product methods from my Vec2 class:
public double dot(Vec2 v) {
return x * v.x + y + v.y;
}
public void noralise() {
double mag = 1.0 / Math.sqrt(x * x + y * y);
x *= mag;
y *= mag;
}
Can anybody spot the error?
Thanks,
Paul