-1

I'm trying to implement slopes in a 2D game by using this formula y1 = y + (x1 - x) * (v/u) for slopes with random degrees. I need a formula for the other direction (from right to left downwards). This formula y1 = y + (v - (x1 - x)) does not work. Does anyone know the correct formula?

Here is a picture which might help:

enter image description here

Xhen
  • 1
  • 2

1 Answers1

1

It's unclear what is known and unknown in your question. Your picture doesn't help. I'll try guessing.

The answer is little more than high school algebra and trigonometry.

I'll assume you have a starting point (x1, y1), an angle theta of your choosing that is equal to zero in the positive x-direction and increases in the counterclockwise direction, and a distance r that you'd like to travel.

You can calculate the end point (x2, y2) like this:

x2 = x1 + r*cos(theta)
y2 = y1 + r*sin(theta)

This works for all angles. For example, your "up and to the right" for a 45 degree angle (pi/4 radians) is:

x2 = x1 + r*sqrt(2)/2
y2 = y1 + r*sqrt(2)/2

Your "down and to the right for a 225 degree angle is:

x2 = x1 - r*sqrt(2)/2
y2 = y1 - r*sqrt(2)/2
duffymo
  • 305,152
  • 44
  • 369
  • 561