Java-Gaming.org Hi !
 Featured games (91) games approved by the League of Dukes Games in Showcase (756) Games in Android Showcase (229) games submitted by our members Games in WIP (842) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 How to get the angle from 1 point to another  (Read 2885 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?
 « 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?

sothatsit

Junior Devvie

Projects: 1

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

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

 DesertCoockie (54 views) 2018-05-13 18:23:11 nelsongames (86 views) 2018-04-24 18:15:36 nelsongames (76 views) 2018-04-24 18:14:32 ivj94 (761 views) 2018-03-24 14:47:39 ivj94 (93 views) 2018-03-24 14:46:31 ivj94 (645 views) 2018-03-24 14:43:53 Solater (106 views) 2018-03-17 05:04:08 nelsongames (187 views) 2018-03-05 17:56:34 Gornova (428 views) 2018-03-02 22:15:33 buddyBro (1088 views) 2018-02-28 16:59:18
 Java Gaming Resourcesby philfrei2017-12-05 19:38:37Java Gaming Resourcesby philfrei2017-12-05 19:37:39Java Gaming Resourcesby philfrei2017-12-05 19:36:10Java Gaming Resourcesby philfrei2017-12-05 19:33:10List of Learning Resourcesby elect2017-03-13 14:05:44List of Learning Resourcesby elect2017-03-13 14:04:45SF/X Librariesby philfrei2017-03-02 08:45:19SF/X Librariesby philfrei2017-03-02 08:44:05
 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