# [RUBY] FINDING COORDINATES ALONG A LINE USING AN ANGLE

## Posts

Pages: 1
I'm writing this here in order to leave a message for those who attempt this in Ruby. Having been hunting for this equation for a while, I though it was important to share, especially since I'm an artist, not a programmer. The formula for this is available online as various programmers help each other out in diverse places--this one comes from "morechilli" at stackoverflow.com on October 15th, 2009--but it comes with a few RGSS quirks.

Given x1 and y1 on any chart, to find x2 and y2 along the same line:

x2 = x1 + (distance * cos(angle))
y2 = y1 + (distance * sin(angle))

RPG Maker is programmed so that cos and sin are accessed using Math.cos and Math.sin, which returns the answer in radians. This threw me for a loop when I tried it, because I had my angle in degrees. Angles must first be converted into radians before they can be used in Math.cos and Math.sin. This is done through the following:

radians = (angle * pi) / 180

This is where we are now:

radians = (angle * pi) / 180
x2 = x1 + (distance * Math.cos(radians))
y2 = y1 + (distance * Math.sin(radians))

Now, for the twist ending. I conceptualized 0-degrees as pointing straight up; 0 degrees was 12 o' clock. But Math.cos and Math.sin consider 0 degrees as flat; instead, 90 degrees is pointing straight up, like the right angle of all of those math problem triangles in high school.

Conceptualizing 0 degrees as up, the final look of the equation is this:

corrected_degrees = (angle + 450) % 360
radians = (corrected_degrees * pi) / 180
x2 = x1 + (distance * Math.cos(radians))
y2 = y1 + (distance * Math.sin(radians))
I'm bumping this topic in order to relay a strange problem with this code. I'd like to ask the help of someone who is more skilled in math than I am.

Currently, this code works, but the movement appears to be offset towards the upper-left. When I am able to move a distance of 1 space, these are the squares accessible to my unit:

I don't understand why this is happening. I can understand why a diagonal movement might be inaccessible due to being two coordinates away, but I don't know why I'm restricted as shown in the image.

----------------------------------------------------------------------------------

The problem has been solved. I needed to execute a ".round" on "x2" and "y2", because certain angles yield move-distances that are slightly below 1.
Pages: 1