
This is the solution I have come up with to solve bouncing off slopes.
1) Create a bounding circle around the sprite (black circle).
2) Similarly, create a bounding rectangle the same size as the 'weapon' on the collision mask. All my collisions are done this way.
3) Per-pixel collision between circle and rectangle, if no collision, no bounce.
4) Check all pixels within the map's collision rectangle against the circle. If the pixels can be contained within the circle, store their locations.
5) Using those stored locations, form a best-fit rectangle around them (red lines). From this, find the center of the rectangle.
6) The vector formed between the center of the circle and the circle of that rectangle is the line of incidence (blue line).
7) Reflect the direction vector of the weapon around the incident vector. (black lines)
Woah, that's a long explanation, but its pretty logical (at least to me).
A few other solutions which came up to mind are (briefly)
1) Line map; a text file with a series of 'points' defining lines. From those points I could then find the gradient; if there is a collision (using ray-casting), use some physics to calculate bounce angle. But then I'd need to manually come up with all the points, and read them from the file!
2) Colour map; use RGB values to define gradients (in a collision mask). Most certainly not.
No comments:
Post a Comment