Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (511)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Finding angle between two vectors  (Read 3018 times)
0 Members and 1 Guest are viewing this topic.
Offline Porlus

Junior Duke





« Posted 2012-12-27 10:15:44 »

Hi all. Smiley 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
Offline SHC
« Reply #1 - Posted 2012-12-27 10: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;

// 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

Offline Porlus

Junior Duke





« Reply #2 - Posted 2012-12-27 10:38:46 »

Thanks for the quick reply. Smiley I've just put in your code and it's stopped the errors, but the angle doesn't look right in the scene. Sad
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!
Legends of Yore - The Casual Retro Roguelike
Offline SHC
« Reply #3 - Posted 2012-12-27 10:45:35 »

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

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 816
Projects: 4
Exp: 16 years


Hand over your head.


« 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
Offline Porlus

Junior Duke





« Reply #5 - Posted 2012-12-27 10:55:01 »

Thanks Riven, just corrected that. Smiley 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.
Offline ra4king

JGO Kernel


Medals: 350
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 Tongue

Offline Porlus

Junior Duke





« 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.
Offline 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.
Offline Porlus

Junior Duke





« Reply #9 - Posted 2012-12-27 16:57:36 »

Ah, that seems to be working better. Smiley 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!
Legends of Yore - The Casual Retro Roguelike
Offline theagentd
« Reply #10 - Posted 2012-12-27 20:06:36 »

Ah, that seems to be working better. Smiley 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.
Online matheus23

JGO Kernel


Medals: 109
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 Smiley
It's the "delta position" between the player and the shooter Wink

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

JGO Overlord


Medals: 816
Projects: 4
Exp: 16 years


Hand over your head.


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

Quote
[ One does not simply normalise a position ]
You actually do Smiley
It's the "delta position" between the player and the shooter Wink

That's called a vector...

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

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

Myomyomyo.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 816
Projects: 4
Exp: 16 years


Hand over your head.


« 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
Offline Porlus

Junior Duke





« 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. Smiley Thanks everyone for helping.
Offline 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  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (50 views)
2014-10-17 03:59:02

Norakomi (39 views)
2014-10-16 15:22:06

Norakomi (31 views)
2014-10-16 15:20:20

lcass (36 views)
2014-10-15 16:18:58

TehJavaDev (66 views)
2014-10-14 00:39:48

TehJavaDev (65 views)
2014-10-14 00:35:47

TehJavaDev (56 views)
2014-10-14 00:32:37

BurntPizza (72 views)
2014-10-11 23:24:42

BurntPizza (44 views)
2014-10-11 23:10:45

BurntPizza (84 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!