0
\$\begingroup\$

Possible Duplicate:
How do I calculate the angle of the slope at a point on a 2D bitmap terrain?

I am planning to make a platform game with my own little engine and I need to know how to detect the angle of a slope. Any help is appreciated.

\$\endgroup\$
1
\$\begingroup\$

You need to know 2 points to calculate a slope, in this example you have p1 and p2 as the two points. You use the Pythagorean theorem to calculate it.

// Delta X and Y
dx = p1.x - p2.x;
dy = p1.y - p2.y;

// Calculate the angle
slope = Math.atan2(dy,dx);

// Convert the angle to degrees
slope = slope * 180 / Math.PI;
| improve this answer | |
\$\endgroup\$
  • 2
    \$\begingroup\$ While your algorithm is correct, it does not make use of the Pythagorean theorem (nor does it need to). You're using the arctangent as it was intended to be used. \$\endgroup\$ – chaosTechnician Dec 27 '12 at 17:21
  • \$\begingroup\$ Thanks, but I also need to know how I would get p1 and p2 \$\endgroup\$ – Chaquator Dec 27 '12 at 19:09
1
\$\begingroup\$

You have two options, and it depends on how you plan to go about your game's collision detection.

If the collision data for your levels can be represented as line segments (or other shapes that can be broken down into line segments), you can use atan2 to find the angle between the endpoints of each line segment as suggested by Thomas.

If your game is tile-based, each tile would likely hold collision data that you've created manually. (E.g. tile type, angle of slope, etc.)

| improve this answer | |
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.