Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (515)
Games in Android Showcase (122)
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  
  Get angle/direction from Vector2f?  (Read 1381 times)
0 Members and 1 Guest are viewing this topic.
Offline wessles

JGO Wizard


Medals: 67
Projects: 4
Exp: 3 years


Profile picture isn't relevant.


« Posted 2013-06-12 01:12:28 »

In slick2d, there is a class called vector2f. I hope you know what that is, because it will take years to explain. How do I get the angle that it is going?

Online SHC
« Reply #1 - Posted 2013-06-12 02:27:10 »

In the Slick source of
Vector2f.java
there's a method called
getTheta()
. Here's it's documentation.

1  
2  
3  
4  
5  
/**
 * Get the angle this vector is at
 *
 * @return The angle this vector is at (in degrees)
 */


You can access the source here at https://bitbucket.org/kevglass/slick/src/c0e4b96798d1c5b1969a26cb62eb7489d701ca6f/trunk/Slick/src/org/newdawn/slick?at=default

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 823
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #2 - Posted 2013-06-12 12:14:30 »

The question is that you want to do with your angle. If you need to calculate sin and cos, then you can skip calculating the angle all together.

This is the 'naive' way:
1  
2  
3  
4  
Vector2f vec = new Vector2f(..., ...);
double angle = Math.atan2(vec.y, vec.x);
double cos = Math.cos(angle);
double sin = Math.sin(angle);


This is a nice fast shortcut:
1  
2  
3  
4  
Vector2f vec = new Vector2f(..., ...);
vec = vec.normalize();
double cos = vec.x;
double sin = vec.y;

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline kutucuk
« Reply #3 - Posted 2013-06-12 12:34:03 »

I am sorry to hijack the thread but I think the OP will find the answer to this question useful as well.

1  
2  
3  
Vector2 vec = body.getPosition();
      double angle = MathUtils.atan2(vec.y, vec.x);
      System.out.println("Angles: " + angle);

I tried this as soon as I saw Riven's answer. It gave me something like 1.1726287603378296 and 0.6320657730102539 as output. There are negative values as well. My question is what the unit of the angle we get here is.
I think it's not an ordinary angle. Because if I get angle by vec.angle() [Libgdx], it is different. This is the output when I add vec.angle() to the system output:
Quote
Angles: 0.24124036729335785 or 13.907541
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 823
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #4 - Posted 2013-06-12 12:49:08 »

Radians

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Danny02
« Reply #5 - Posted 2013-06-12 14:23:27 »

the dot product of two vectors is also very usefull, because it is the cos of the angle between the two vectors.(both vectors have to be normalized)

So if the dot is 0 the vectors are orthogonal to each other, if |cos(a)| == 1 then they are parallel and with the sign you can see if they are pointing in the same(+) or different(-) direction
Offline relminator
« Reply #6 - Posted 2013-06-12 14:30:24 »

the dot product of two vectors is also very usefull, because it is the cos of the angle between the two vectors.(both vectors have to be normalized)

So if the dot is 0 the vectors are orthogonal to each other, if |cos(a)| == 1 then they are parallel and with the sign you can see if they are pointing in the same(+) or different(-) direction

If you are trying to do homing missiles, this is a fast way to simulate it.  In fact, you don't need to normalize for it to work.
Offline wessles

JGO Wizard


Medals: 67
Projects: 4
Exp: 3 years


Profile picture isn't relevant.


« Reply #7 - Posted 2013-06-12 18:56:26 »

Thank you all! I will use rivens answer because I already had Velx and vely. I just need the angle so I can rotate the enemy's image. I could still use get x. Thanks for the input!

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.

TehJavaDev (31 views)
2014-10-27 03:28:38

TehJavaDev (26 views)
2014-10-27 03:27:51

DarkCart (40 views)
2014-10-26 19:37:11

Luminem (21 views)
2014-10-26 10:17:50

Luminem (25 views)
2014-10-26 10:14:04

theagentd (31 views)
2014-10-25 15:46:29

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

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

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

lcass (43 views)
2014-10-15 16:18:58
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!