Hi !
Featured games (85)
games approved by the League of Dukes
Games in Showcase (616)
Games in Android Showcase (173)
games submitted by our members
Games in WIP (659)
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 1335 times)
0 Members and 1 Guest are viewing this topic.
Offline 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?

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)
Offline ra4king

JGO Kernel

Medals: 389
Projects: 3
Exp: 5 years

I'm the King!

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

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

Offline 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?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Vladiedoo
« 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:

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

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

JGO Kernel

Medals: 389
Projects: 3
Exp: 5 years

I'm the King!

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

Whoopsies, silly typo.

Offline 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)
Offline 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

float value1 = 0.0001f;
float value2 = 0.0001f;

if (value1 == value2) {
// do something

is a bad bad idea.

Offline 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?  Wink

music and music apps:
Offline sothatsit

Senior Newbie

Projects: 1

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

sorry for not replying Tongue

i am not using anything except the api that i made myself
Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

Coldstream24 (16 views)
2015-09-03 00:41:28

Andrew_3ds (25 views)
2015-09-01 19:08:10

afikri (17 views)
2015-08-31 09:30:22

afikri (24 views)
2015-08-31 09:30:07

afikri (13 views)
2015-08-31 09:27:24

afikri (16 views)
2015-08-31 09:26:40

Roquen (30 views)
2015-08-29 11:30:54

GamerC4 (36 views)
2015-08-22 20:38:50

GamerC4 (33 views)
2015-08-22 20:37:18

GamerC4 (40 views)
2015-08-22 20:37:01
HotSpot Options
by Roquen
2015-08-29 11:33:11

Rendering resources
by Roquen
2015-08-17 12:42:29

Rendering resources
by Roquen
2015-08-17 09:36:56

Rendering resources
by Roquen
2015-08-13 07:40:51

Networking Resources
by Roquen
2015-08-13 07:40:43

List of Learning Resources
by gouessej
2015-07-09 11:29:36

How Do I Expand My Game?
by bashfrog
2015-06-14 11:34:43

List of Learning Resources
by PocketCrafter7
2015-05-31 05:37:30 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‑
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!