Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (575) Games in Android Showcase (154) games submitted by our members Games in WIP (623) 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 4017 times) 0 Members and 1 Guest 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
« League of Dukes »

« JGO Overlord »

Medals: 954
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: 374
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

« JGO Bitwise Duke »

Medals: 432
Projects: 2
Exp: 8 years

 « 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

« JGO Bitwise Duke »

Medals: 432
Projects: 2
Exp: 8 years

 « 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: 121
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
« League of Dukes »

« JGO Overlord »

Medals: 954
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

« JGO Bitwise Duke »

Medals: 432
Projects: 2
Exp: 8 years

 « Reply #13 - Posted 2012-12-28 05:07:09 »

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

Myomyomyo.
Riven
« League of Dukes »

« JGO Overlord »

Medals: 954
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

« JGO Bitwise Duke »

Medals: 432
Projects: 2
Exp: 8 years

 « 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

You cannot reply to this message, because it is very, very old.

 BurntPizza (29 views) 2015-04-23 03:42:11 theagentd (33 views) 2015-04-22 16:23:07 Riven (48 views) 2015-04-16 10:48:47 Duke0200 (59 views) 2015-04-16 01:59:01 Fairy Tailz (41 views) 2015-04-14 20:13:12 Riven (43 views) 2015-04-12 21:36:37 bus hotdog (60 views) 2015-04-10 02:39:32 CopyableCougar4 (66 views) 2015-04-10 00:51:04 BurntPizza (69 views) 2015-04-06 22:06:58 ags1 (67 views) 2015-04-02 10:58:48
 theagentd 23x BurntPizza 16x wessles 15x alwex 11x 65K 11x kingroka123 11x kevglass 8x Rayvolution 8x Olo 7x Ecumene 7x ra4king 7x Roquen 7x chrislo27 7x Hanksha 7x Riven 7x KevinWorkman 6x
 How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27
 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