Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

[Resolved] Angle calculation from two points

So yeah, since pictures say more than 1000 words...

scron8.png


So you can see on this very-much-time-spend-into-sketch that it's about point 1 and point 2 connected with each other. What I have are x and y values of both points. What I would like you to help with is the angle between the imaginatory line between those two points and the x-axis of point 1... or rather the syntax to calculate it relatively easy ^^

Thanks in advance for your help here.
 
Math.atan2((y2 - y1),(x2 - x1)) * 90 / Math::PI

This is based on your description, as the picture isn't making a lot of sense.
Also based on the assumption that you meant 'halfway' between the line & the x axis
 
Well, I think it does make sense, but yeah... I guess I'll try to explain it better, since your code doesn't give me the correct values there... at all, that is.

So yeah, I dunno if you're familiar with architectural programs like AutoCAD, but they commonly use a 360 degree system to create lines by input. So for example if you have a line from 0,0 to 1,1 you could as well say you want a line with the lenght of (square root of 2) in the direction 315° (degree counting starts with 0° at 3 o'clock, aka where my gray line is in the picture).

So yeah, I want it the other way round, if you want to put it like that... i have my line defined by the two points, and need to find out the angle I'd need to create the line like that. For my picture, you can see it should be 310° for the blue line there (that looks more than actual 315° now that i think about it, but yeah, it's just a sketch), and 75° for the red one.

I hope you know what I'm talking about, since I really don't think I can explain it otherwise... but as far as I can tell, it's the code's fault, not mine...
 
Blue,

I'm VERY familiar with CAD systems. (I work for Siemens. (NX, Unigraphics))

In a CAD system, a line from 0,0 to 1,1 is 45°.

If you want a line the bisects the angle between your line & the x axis, it would be at 22.5°, or 202.5°.

I don't see where you're getting the 315°. (or the 310° in your drawing). Or the 75°

Oooooooh, I think I see it now. You just want the angle of the line relative to the x-axis, but reversed. (measured clockwise instead of counter clockwise.)

a = Math.atan2((y2 - y1),(x2 - x1)) * 180 / Math::PI
a = a > 0 ? 360 + a : -a
 
Heh, now I get you there as well... damn misunderstandings. And yeah, I messed the clockwise-counterclockise stuff up then, I guess, my apologies... it's been a while since I worked with CAD, so yeah... it didn't really need to go counter-clockwise, but it doesn't matter either way...
So well, it kind of works now, with the exception that you seemed to mess up the second line there... I changed it to a = a < 0 ? 360 + a : a to make it work there... the other one returned abnormally high values ^^

Thanks for your help there, it's very apprechiated ^^
 
yep, you're right. Assuming you wanted it flipped (I assumed because in RMXP the Y axis points down), it should have been:

a = a > 0 ? 360 - a : -a

But I think you have it right now, as the Y axis is arbitrary as long as the input & output agree. i.e. we tend to visualize Y as pointing up, but if we're using mirrored input, the mirrored output is right.

Be Well
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top