Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (115)
games submitted by our members
Games in WIP (562)
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  
  Getting the angle of an object relative to another  (Read 1296 times)
0 Members and 1 Guest are viewing this topic.
Offline Hazzy

Senior Newbie





« Posted 2012-05-12 11:58:51 »

Wasn't sure which board to post this in but nevertheless. If it needs moving, move it.

I'm working on a game and I need help with some math.
What I'm trying to do is get the angle (direction) of the mouse cursor relative to the object checking the angle.

I've applied what I have learned from class but I'm not sure if I'm doing it quite right here.

1  
boolean direction = Math.atan(yy/xx)/Math.PI*180;


This works mostly as I get an angle returned, and in degrees not radians.
Only the angle passes from 0 to 90 and 0 to -90. Repeating itself at certain angles that would otherwise be something like 200 degrees.
The other problem is xx and yy can be negative, because the values are relative to the object on the screen.

1  
2  
boolean xx = input.x - x;
boolean yy = input.y - y;


I tried by forcing the values to always be positive but the angles were still messed up.

But don't get me wrong, this all works from 0 to 90 degrees flawlessly. Although if I go to around where 360-270 degrees should be, it shows 0 to -90 degrees instead.
It's like a sine wave all the way around: 0 to 90, 90 to 0, 0 to -90, -90 to 0.

I'd just rather this go from 0-360 all the way around. If anyone can help I'll be most thankful Smiley
Offline theagentd
« Reply #1 - Posted 2012-05-12 12:52:52 »

Math.toDegrees(Math.atan2(yy, xx));

Myomyomyo.
Offline pitbuller
« Reply #2 - Posted 2012-05-12 12:53:03 »

http://www.java-gaming.org/topics/angle-of-slope-for-mouse-movement/26409/view.html
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Hazzy

Senior Newbie





« Reply #3 - Posted 2012-05-13 14:34:25 »

Math.toDegrees(Math.atan2(yy, xx));
Thanks, solved half the problem. Got me to 0 to 180 and -180 to 0 all the way around.
Just had to normalize the negativity and subtract from 360 and all works fine now Tongue
Offline h3ckboy

JGO Coder


Medals: 5



« Reply #4 - Posted 2012-05-13 15:05:05 »

I'm sorry if I am not answering your question correctly, but from what I understand you are having domain/range restriction issues (I cannot remember which is the correct one)?

in order to determine if the restriction is obscuring the correct answer, check the x value, if the x value is negative then you have to add or subtract 180 to get what would have been the correct angle.

e.g, if you have a x change of -sqrt(2)/2 (I'm keeping it on the unit circle for simplicity), and a y value of sqrt(2)/2 then you have atan ((sqrt(2)/2)/(-sqrt(2)/2))

this will give you -pi/4 (or -45 degrees) because of the domain restrictions, but you know that it is wrong because the x value is negative, so you simply add/subtract 180 degrees (or pi whichever) to get 3pi/4 or -5pi/4 ( 135 or -225 degrees)

I apologize if the math makes no sense or the notation is ugly (typing math really sucks), but the main gist is this:

if you have a negative x change value (so the bottom part of your tan value) then you must add or subtract(completely preference which you choose) pi/2 (180 degrees) from your final angle to compensate for the restriction.


hope this helps, I apologize if I am completely off-base I am a little fried from studying Tongue,

h3ckboy
Offline Hazzy

Senior Newbie





« Reply #5 - Posted 2012-05-13 15:52:15 »

I'm sorry if I am not answering your question correctly, but from what I understand you are having domain/range restriction issues (I cannot remember which is the correct one)?

in order to determine if the restriction is obscuring the correct answer, check the x value, if the x value is negative then you have to add or subtract 180 to get what would have been the correct angle.

e.g, if you have a x change of -sqrt(2)/2 (I'm keeping it on the unit circle for simplicity), and a y value of sqrt(2)/2 then you have atan ((sqrt(2)/2)/(-sqrt(2)/2))

this will give you -pi/4 (or -45 degrees) because of the domain restrictions, but you know that it is wrong because the x value is negative, so you simply add/subtract 180 degrees (or pi whichever) to get 3pi/4 or -5pi/4 ( 135 or -225 degrees)

I apologize if the math makes no sense or the notation is ugly (typing math really sucks), but the main gist is this:

if you have a negative x change value (so the bottom part of your tan value) then you must add or subtract(completely preference which you choose) pi/2 (180 degrees) from your final angle to compensate for the restriction.


hope this helps, I apologize if I am completely off-base I am a little fried from studying Tongue,

h3ckboy

What you said is basically what I've managed to do now Tongue
I knew the negativity would be a problem but it had to be there for the object to take the direction relative from its position.
The only other thing to do was reverse the Y as computers have it mirrored from what we learn in class. Nothing tricky though.

The finished result:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
         //Get the input x/y and make it relative to the objects position and centre
        xx = inX - x - 32;
         yy = (inY - y - 32)*-1;
         
         //Calculate the direction and convert to degrees
        direction = Math.atan2(yy, xx)/Math.PI*180;
         
         //If the direction is negative, then normalise it
        if (direction < 0){
            direction = direction*-1;
            direction = 360 - direction;
         }

Works fine all around, 0 to 360 degrees.

And I really should be revising maths too but I'm tired. Ironic that my way of having a break involves more maths. (Curse you sine and cosine rules)

Thanks though Cheesy
Offline ra4king

JGO Kernel


Medals: 346
Projects: 3
Exp: 5 years


I'm the King!


« Reply #6 - Posted 2012-05-13 16:43:26 »

Why negate then subtract from 360? Just add 360 to it and that's all Tongue

Also why don't you want negative degrees? The work fine everywhere AFAIK Wink

Offline Hazzy

Senior Newbie





« Reply #7 - Posted 2012-05-13 16:50:29 »

Why negate then subtract from 360? Just add 360 to it and that's all Tongue

Also why don't you want negative degrees? The work fine everywhere AFAIK Wink
Hey, it works. That's all I need xD Now I'm curious... Let's try changing it! (Darn you!)
Offline pitbuller
« Reply #8 - Posted 2012-05-13 18:46:23 »

Where are you using that angle?
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.

radar3301 (9 views)
2014-09-21 23:33:17

BurntPizza (28 views)
2014-09-21 02:42:18

BurntPizza (18 views)
2014-09-21 01:30:30

moogie (20 views)
2014-09-21 00:26:15

UprightPath (27 views)
2014-09-20 20:14:06

BurntPizza (29 views)
2014-09-19 03:14:18

Dwinin (46 views)
2014-09-12 09:08:26

Norakomi (74 views)
2014-09-10 13:57:51

TehJavaDev (102 views)
2014-09-10 06:39:09

Tekkerue (50 views)
2014-09-09 02:24:56
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

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!