Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (533)
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 928 times)
0 Members and 1 Guest are viewing this topic.
Offline sothatsit

Senior Newbie


Projects: 1



« Posted 2012-10-09 01: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)
Offline ra4king

JGO Kernel


Medals: 336
Projects: 2
Exp: 5 years


I'm the King!


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

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

Offline sothatsit

Senior Newbie


Projects: 1



« Reply #2 - Posted 2012-10-09 04: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 04: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?
Offline ra4king

JGO Kernel


Medals: 336
Projects: 2
Exp: 5 years


I'm the King!


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

Whoopsies, silly typo.

Offline QuicK

Junior Member





« Reply #5 - Posted 2012-10-09 12: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 13: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
}


is a bad bad idea.

Offline philfrei
« Reply #7 - Posted 2012-10-09 20: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

"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
Offline sothatsit

Senior Newbie


Projects: 1



« Reply #8 - Posted 2012-10-10 02: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.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (22 views)
2014-07-24 01:59:36

Riven (20 views)
2014-07-23 21:16:32

Riven (17 views)
2014-07-23 21:07:15

Riven (20 views)
2014-07-23 20:56:16

ctomni231 (48 views)
2014-07-18 06:55:21

Zero Volt (44 views)
2014-07-17 23:47:54

danieldean (35 views)
2014-07-17 23:41:23

MustardPeter (38 views)
2014-07-16 23:30:00

Cero (53 views)
2014-07-16 00:42:17

Riven (53 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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
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!