Java-Gaming.org Hi !
 Featured games (90) games approved by the League of Dukes Games in Showcase (754) Games in Android Showcase (229) games submitted by our members Games in WIP (842) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Finding angle between two vectors  (Read 11963 times) 0 Members and 2 Guests are viewing this topic.
Porlus

Junior Devvie

 « Posted 2012-12-27 10: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 10:26:08 »

 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;// Now the magnitudesmag_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

Junior Devvie

 « Reply #2 - Posted 2012-12-27 10: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.
SHC
 « Reply #3 - Posted 2012-12-27 10:45:35 »

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

Riven

« JGO Overlord »

Medals: 1340
Projects: 4
Exp: 16 years

 « Reply #4 - Posted 2012-12-27 10: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!

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Porlus

Junior Devvie

 « Reply #5 - Posted 2012-12-27 10: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

JGO Kernel

Medals: 508
Projects: 3
Exp: 5 years

I'm the King!

 « Reply #6 - Posted 2012-12-27 12:52:43 »

Quick typo check: it's normalize, not noralize

Porlus

Junior Devvie

 « Reply #7 - Posted 2012-12-27 13: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 15: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

Junior Devvie

 « Reply #9 - Posted 2012-12-27 16: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.
theagentd
 « Reply #10 - Posted 2012-12-27 20: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

JGO Kernel

Medals: 138
Projects: 3

You think about my Avatar right now!

 « Reply #11 - Posted 2012-12-27 20:50:12 »

Haha, I just couldn't resist. =P

You actually do
It's the "delta position" between the player and the shooter

See my:
My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Riven

« JGO Overlord »

Medals: 1340
Projects: 4
Exp: 16 years

 « Reply #12 - Posted 2012-12-27 20:50:58 »

Quote
[ 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...

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
theagentd
 « Reply #13 - Posted 2012-12-28 05:07:09 »

That's called a vector...
... an unnormalized vector to be precise! ^^

Myomyomyo.
Riven

« JGO Overlord »

Medals: 1340
Projects: 4
Exp: 16 years

 « Reply #14 - Posted 2012-12-28 05:49:15 »

Dude... 'vector' doesn't imply normalization, not even in the slightest.

Quote from: DeepVoiceNarrator
Quote from: Girl
Look, a car!

Quote from: Guy
... an unwrecked car, to be precise! ^^

Remember this kids: they didn't procreate.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Porlus

Junior Devvie

 « Reply #15 - Posted 2012-12-28 15: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 15:45:57 »

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

Myomyomyo.
Pages: [1]
 ignore  |  Print

 DesertCoockie (20 views) 2018-05-13 18:23:11 nelsongames (68 views) 2018-04-24 18:15:36 nelsongames (65 views) 2018-04-24 18:14:32 ivj94 (749 views) 2018-03-24 14:47:39 ivj94 (79 views) 2018-03-24 14:46:31 ivj94 (595 views) 2018-03-24 14:43:53 Solater (95 views) 2018-03-17 05:04:08 nelsongames (168 views) 2018-03-05 17:56:34 Gornova (378 views) 2018-03-02 22:15:33 buddyBro (1038 views) 2018-02-28 16:59:18
 Java Gaming Resourcesby philfrei2017-12-05 19:38:37Java Gaming Resourcesby philfrei2017-12-05 19:37:39Java Gaming Resourcesby philfrei2017-12-05 19:36:10Java Gaming Resourcesby philfrei2017-12-05 19:33:10List of Learning Resourcesby elect2017-03-13 14:05:44List of Learning Resourcesby elect2017-03-13 14:04:45SF/X Librariesby philfrei2017-03-02 08:45:19SF/X Librariesby philfrei2017-03-02 08:44:05
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org