Java-Gaming.org Hi !
 Featured games (90) games approved by the League of Dukes Games in Showcase (778) Games in Android Showcase (231) games submitted by our members Games in WIP (856) 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
 How to get the angle from 1 point to another  (Read 3031 times) 0 Members and 1 Guest are viewing this topic.
sothatsit

Junior Devvie

Projects: 1

 « Posted 2012-10-08 23:34:02 »

i know this probable takes trigonometry or calculus or something complicated but i haven't done anything like that yet...  i haven't even done sin and cos. so how would i go about finding the angle?

E.g.
point1(0,0) and point2(1,1)

that angle would be -45 degrees or 315 degrees

so if you have a formula can you please show it to me and ill be very grateful. this is not a school project by the way

if there is an easier way basicly i want to make an image(of a gun) point towards another image(of a mob)
ra4king

JGO Kernel

Medals: 508
Projects: 3
Exp: 5 years

I'm the King!

 « Reply #1 - Posted 2012-10-09 00:08:56 »

Math.atan2(y2-y1, x2-x1);

sothatsit

Junior Devvie

Projects: 1

 « Reply #2 - Posted 2012-10-09 02:16:22 »

thanks! i havent tested yet but should it be y2-y1?
 Games published by our own members! Check 'em out!
 « Reply #3 - Posted 2012-10-09 02:17:55 »

thanks! i havent tested yet but should it be y2-y1?
Correct, it should indeed be y2-y1.

If you want degrees though, do the following:

 1 `(Math.atan2(y2 - y1, x2 - x1) * (180/Math.PI))`

Are you using Java2D, Slick2D, libGDX, LWJGL or something else?
ra4king

JGO Kernel

Medals: 508
Projects: 3
Exp: 5 years

I'm the King!

 « Reply #4 - Posted 2012-10-09 02:47:07 »

Whoopsies, silly typo.

QuicK

Junior Devvie

 « Reply #5 - Posted 2012-10-09 10:50:42 »

Have any of you ever had an issue with this returning bad angles?

I was messing around with an L-System algorithm, and all of my angles when calculating (angle%45) should be 0.
Some of them return 44.99999 or 90.00001...all of my numbers are floating decimal numbers, and there is no rounding :/

is this just some fault with computers? (I understand its not that big of a deal, I just found it interesting)
DrHalfway
 « Reply #6 - Posted 2012-10-09 11:57:50 »

Yes, floating point numbers are finite, hence rounding errors do occur. This is why doing the following

 1  2  3  4  5  6 `float value1 = 0.0001f;float value2 = 0.0001f;if (value1 == value2) {// do something}`

philfrei
 « Reply #7 - Posted 2012-10-09 18:08:29 »

I thought it curious that floating point is actually fine with numbers that are divisible by powers of 2. For example, 0.125 (2^1/8) is represented pretty cleanly. Makes sense since computers represent numbers in binary.

But decimal points in base 10 are messy, since the conversion to binary often leads to endlessly extending remainders. Maybe "the problem" is that computers don't have 10 fingers and toes?

music and music apps: http://adonax.com
sothatsit

Junior Devvie

Projects: 1

 « Reply #8 - Posted 2012-10-10 00:38:38 »

sorry for not replying

i am not using anything except the api that i made myself
Pages: [1]
 ignore  |  Print

 hadezbladez (350 views) 2018-11-16 13:46:03 hadezbladez (183 views) 2018-11-16 13:41:33 hadezbladez (349 views) 2018-11-16 13:35:35 hadezbladez (85 views) 2018-11-16 13:32:03 EgonOlsen (2179 views) 2018-06-10 19:43:48 EgonOlsen (2221 views) 2018-06-10 19:43:44 EgonOlsen (1379 views) 2018-06-10 19:43:20 DesertCoockie (2012 views) 2018-05-13 18:23:11 nelsongames (1671 views) 2018-04-24 18:15:36 nelsongames (2309 views) 2018-04-24 18:14:32
 Deployment and Packagingby mudlee2018-08-22 18:09:50Java Gaming Resourcesby gouessej2018-08-22 08:19:41Deployment and Packagingby gouessej2018-08-22 08:04:08Deployment and Packagingby gouessej2018-08-22 08:03:45Deployment and Packagingby philfrei2018-08-20 02:33:38Deployment and Packagingby philfrei2018-08-20 02:29:55Deployment and Packagingby philfrei2018-08-19 23:56:20Deployment and Packagingby philfrei2018-08-19 23:54:46
 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