How to get the angle from 1 point to another

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)

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

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?

Whoopsies, silly typo.

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

is this just some fault with computers? (I understand its not that big of a deal, I just found it interesting)

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.

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:

sorry for not replying :stuck_out_tongue:

i am not using anything except the api that i made myself