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.
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.
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;
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.)