What's the best way to approach angled floors in a 2D side scroller?

Started by
3 comments, last by ascorbic 18 years, 10 months ago
Well the topic is pretty much the question. Suppose I want to implement a 2D world such that floor can have various degrees of angel to it, but I want the in game sprites to be able to move naturally across it. What would be the best way to acheive this? I assume I could to a per-pixel check against the sprite's bounding box, which would keep it on the floor, but how would I know I need to move say 45 degrees up and to the right, instead of just getting stopped all together by a single pixel? or for that matter 45 degrees down and to the right, instead of moving to the right and then falling a single pixel? or am I just over analyzing this? Any examples or tutorials would be appreciated. Thanks.
Advertisement
Generally speaking, the approach is:

- Test whether any parts of the object are inside the wall/floor
- If they are, work out by how much and in what direction
- Move the object by that amount
- It should now be wholly outside the floor/wall, but unfortunately might be inside another one, so you might have to repeat the process.

In practice, if the object moves slightly inside the floor/wall, it doesn't matter too much, so you might only want to do that process once per frame per object pair (even though, under some circumstances, the object will still be slightly inside one wall/floor)

Personally I'd model the sprite either as a rectangle, circle, or several rectangles and circles - because the collision detection is much easier for them.

--

Oh yes, one other thing, I'd be wary of posting "What's the best X...?" threads, because they're rather subjective.

Also because doing things the "best" way, is not always best. I mean, define what you mean by "best".

If by "best" you mean "most straightforward", the way I highlighted is pretty good.

Mark
No, you are not over analyzing it. This is a very difficult problem, one that i've been trying to work out for the better part of a year. I still do not have a flexible solution. The jist of it though is to have the bottom center of the player bounding box "snap" to the grade of the floor. You'll notice that the inside corner will actually penetrate the floor polygon. That's okay. Lots of oldschool favorites like Super Mario Bros. and Super Metroid all pull this trick off the same way. You can make it look less hokey by putting "shrubbery" on the ground that partially obscures the fact that the player's leading foot is actually inside the floor geometry.

The problem is that if you have a realistic polygon collision system like i have where polys are supposed to repell each other when they intersect, the concept of having the player penetrate the floor by some small degree on purpose violates the whole system of keeping polygons separate. See my point? So floor-walking is basically going to have to be an exception to the rest of the system -or- you have to change the whole system.

The other really obvious solution is simply to not have sloped grounds. I know it's a bit cheap, but it is the easiest solution.

A somewhat helpful tutorial can be found here
Well if this was based on a tile based system you could simply store the 'repel' information in that tile. For example, a 45 degree angled tile would store the vector [.7, .7] (I realize that's not 100% accurate). When an object collides with the tile (on a pixel level), you push the colliding object in that direction.

But the obvious problem here is that you would need a very limited amount of possible angles, or else you'll spend years figuring out all these abitrary angles.

So instead maybe, in your level editor, define a bunch of line segments that outline your level. In-game, do a test for closest point on the line, and see if the object is colliding with it. If it is, get the normal coming off the line, and push the object back along it.

I actually did something very similar to the second approach in this little demo, except the lines aren't drawn in a level editor, but instead in real time by the player. (Yoshi's Touch and Go ripped me off =)

Hope this helps,
Matt Hughson
__________________________________[ Website ] [ Résumé ] [ [email=contact[at]matthughson[dot]com]Contact[/email] ][ Have I been Helpful? Hook me up! ]
I was thinking that you could do a collision check between the sprite and the tile layout. If the feet are touching a tile numbered (by you) as "slanted" you could adjust the y-coordinate as well as the x-coordinate when your sprite moves. But once again you are kind of limited to the angles you could use. Of course this is just theory in my head, but I think it could work.

I do remember reading about this on the web. Super Metroid for SNES and MC KIDS for NES were examples that I remember seeing. I think if you google "Super Metroid" "tiles" "slanted tiles" or something like that you will find what you are looking for.
.

This topic is closed to new replies.

Advertisement