Finding angle between two vectors

Hi all. :slight_smile: 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

According to your definition


v1x = x;
v1y = y;

v2x = player_x;
v2y = player_y;

dot = v1x * v2x + v1y * v2y;

// Now the magnitudes

mag_v1 = sqrt ( v1x*v1x + v2y*v2y );
mag_v2 = sqrt ( v2x*v2x + v2y*v2y );

cosa = (double) (dot/(mag_v1*mag_v2));

angle = acos(cosa);

Hope this psuedo code helps

Thanks for the quick reply. :slight_smile: I’ve just put in your code and it’s stopped the errors, but the angle doesn’t look right in the scene. :frowning:
This is for the enemies in the scene. They’re able to fire projectiles at the player, but in order to do that the angle between the player and enemy must be determined. As it stands they just appear to be firing up from -45 to 45 degrees.

Maybe you need to translate the angle. Math.acos returns in radians.


public double dot(Vec2 v) {
-		return x * v.x + y + v.y;
+		return x * v.x + y * v.y;
	}
	
	public void noralise() {
		double mag = 1.0 / Math.sqrt(x * x + y * y);
		x *= mag;
		y *= mag;
	}

Yup!

Thanks Riven, just corrected that. :slight_smile: Now the dot product method works, but the angle’s still a bit off. I’ve translated it from radians into degrees too and it doesn’t seem to firing in the correct direction.

Quick typo check: it’s normalize, not noralize :stuck_out_tongue:

Woops, didn’t notice that. :x It’s all corrected now, but it still makes every enemy shoot at roughly a 45 degree angle.

You don’t have two DIRECTION vectors, you have two positions. Try this instead:


double dx = x2 - x1, dy = y2 - y1;
angle = Math.atan2(dy, dx);

EDIT2: (x1, y1) = the player’s position, (x2, y2) = the shooter’s position.

EDIT:

http://cdn.memegenerator.net/instances/400x/32525821.jpg

Haha, I just couldn’t resist. =P

Ah, that seems to be working better. :slight_smile: There’s just one problem with it now, which is that the up-down of the angle seems to be inverted.

Just negate dy then.

You actually do :slight_smile:
It’s the ā€œdelta positionā€ between the player and the shooter :wink:

You actually do :slight_smile:
It’s the ā€œdelta positionā€ between the player and the shooter :wink:
[/quote]
That’s called a vector…

… an unnormalized vector to be precise! ^^

Dude… ā€˜vector’ doesn’t imply normalization, not even in the slightest.

[quote=""]

[quote=""]

Sorry about bumping an old thread. I’ve got the angle right by negating the y comp. :slight_smile: Thanks everyone for helping.

@Riven:
Yeah, just clarifying since most mathematical functions on vectors seem to assume that the vectors are normalized.