Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (604) Games in Android Showcase (171) games submitted by our members Games in WIP (652) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Getting the angle of an object relative to another  (Read 1599 times) 0 Members and 1 Guest are viewing this topic.
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
theagentd

« JGO Bitwise Duke »

Medals: 525
Projects: 3
Exp: 8 years

 « Reply #1 - Posted 2012-05-12 12:52:52 »

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

Myomyomyo.
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
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
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 ,

h3ckboy
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 ,

h3ckboy

What you said is basically what I've managed to do now
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
ra4king

JGO Kernel

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

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

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

Also why don't you want negative degrees? The work fine everywhere AFAIK
Hey, it works. That's all I need xD Now I'm curious... Let's try changing it! (Darn you!)
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.

 SHC (22 views) 2015-08-01 03:58:20 Jesse (19 views) 2015-07-29 04:35:27 Riven (39 views) 2015-07-27 16:38:00 Riven (21 views) 2015-07-27 15:35:20 Riven (24 views) 2015-07-27 12:26:13 Riven (14 views) 2015-07-27 12:23:39 BurntPizza (35 views) 2015-07-25 00:14:37 BurntPizza (46 views) 2015-07-24 22:06:39 BurntPizza (28 views) 2015-07-24 06:06:53 NoxInc (36 views) 2015-07-22 22:16:53
 theagentd 51x wessles 45x basil_ 35x orangepascal 26x KaiHH 26x ags1 21x Riven 19x mooman219 17x bornander 14x KudoDEV 13x CelestialCreator 11x princec 11x klaus 11x Jesse 10x Zaneris 9x pquiring 9x
 List of Learning Resourcesby gouessej2015-07-09 11:29:36How Do I Expand My Game?by bashfrog2015-06-14 11:34:43List of Learning Resources2015-05-31 05:37:30Intersection Methodsby Roquen2015-05-29 08:19:33List of Learning Resources2015-05-05 10:20:32How 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:21
 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