Java-Gaming.org Hi !
 Featured games (83) games approved by the League of Dukes Games in Showcase (538) Games in Android Showcase (132) games submitted by our members Games in WIP (600) 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 1034 times) 0 Members and 1 Guest are viewing this topic.
sothatsit

Senior Newbie

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: 356
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

Senior Newbie

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: 356
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?

"It's after the end of the world! Don't you know that yet?"
sothatsit

Senior Newbie

Projects: 1

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