Porlus
|
 |
«
Posted
2012-12-27 11:15:44 » |
|
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. 1 2 3 4 5 6
| 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: 1 2 3 4 5 6 7 8 9
| 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
|
|
|
|
|
SHC
|
 |
«
Reply #1 - Posted
2012-12-27 11:26:08 » |
|
According to your definition 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| v1x = x; v1y = y;
v2x = player_x; v2y = player_y;
dot = v1x * v2x + v1y * v2y;
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
|
|
|
|
Porlus
|
 |
«
Reply #2 - Posted
2012-12-27 11:38:46 » |
|
Thanks for the quick reply.  I've just put in your code and it's stopped the errors, but the angle doesn't look right in the scene.  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.
|
|
|
|
|
Games published by our own members! Check 'em out!
|
|
SHC
|
 |
«
Reply #3 - Posted
2012-12-27 11:45:35 » |
|
Maybe you need to translate the angle. Math.acos returns in radians.
|
|
|
|
Riven
|
 |
«
Reply #4 - Posted
2012-12-27 11:47:12 » |
|
1 2 3 4 5 6 7 8 9 10
| 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; } |
Can anybody spot the error?
Yup!
|
|
|
|
Porlus
|
 |
«
Reply #5 - Posted
2012-12-27 11:55:01 » |
|
Thanks Riven, just corrected that.  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.
|
|
|
|
|
ra4king
|
 |
«
Reply #6 - Posted
2012-12-27 13:52:43 » |
|
Quick typo check: it's nor malize, not noralize 
|
|
|
|
Porlus
|
 |
«
Reply #7 - Posted
2012-12-27 14:07:29 » |
|
Woops, didn't notice that. :x It's all corrected now, but it still makes every enemy shoot at roughly a 45 degree angle.
|
|
|
|
|
theagentd
|
 |
«
Reply #8 - Posted
2012-12-27 16:30:14 » |
|
You don't have two DIRECTION vectors, you have two positions. Try this instead: 1 2
| 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:  Haha, I just couldn't resist. =P
|
Myomyomyo.
|
|
|
Porlus
|
 |
«
Reply #9 - Posted
2012-12-27 17:57:36 » |
|
Ah, that seems to be working better.  There's just one problem with it now, which is that the up-down of the angle seems to be inverted.
|
|
|
|
|
Games published by our own members! Check 'em out!
|
|
theagentd
|
 |
«
Reply #10 - Posted
2012-12-27 21:06:36 » |
|
Ah, that seems to be working better.  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.
|
Myomyomyo.
|
|
|
matheus23
|
 |
«
Reply #11 - Posted
2012-12-27 21:50:12 » |
|
 Haha, I just couldn't resist. =P You actually do  It's the "delta position" between the player and the shooter 
|
|
|
|
Riven
|
 |
«
Reply #12 - Posted
2012-12-27 21:50:58 » |
|
[ One does not simply normalise a position ] You actually do  It's the "delta position" between the player and the shooter  That's called a vector...
|
|
|
|
theagentd
|
 |
«
Reply #13 - Posted
2012-12-28 06:07:09 » |
|
That's called a vector...
... an unnormalized vector to be precise! ^^
|
Myomyomyo.
|
|
|
Riven
|
 |
«
Reply #14 - Posted
2012-12-28 06:49:15 » |
|
Dude... 'vector' doesn't imply normalization, not even in the slightest. Look, a car! ... an unwrecked car, to be precise! ^^ Remember this kids: they didn't procreate.
|
|
|
|
Porlus
|
 |
«
Reply #15 - Posted
2012-12-28 16:35:48 » |
|
Sorry about bumping an old thread. I've got the angle right by negating the y comp.  Thanks everyone for helping.
|
|
|
|
|
theagentd
|
 |
«
Reply #16 - Posted
2012-12-28 16:45:57 » |
|
@Riven: Yeah, just clarifying since most mathematical functions on vectors seem to assume that the vectors are normalized.
|
Myomyomyo.
|
|
|
|