Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

The question is not hard, I'm writing a game engine for 2D side scrolling games, however I'm thinking to my 2D side scrolling game and I always come up with the problem of "how should I do collision with the ground". I think I couldn't handle the collision with ground (ground for me is "where the player walk", so something heavily used) in a per-pixel way, and I can't even do it with simple shape comparison (because the ground can be tilted), so what's the correct way? I'know what tiles are and i've read about it, but how much should be big each tile to not appear like a stairs?Are there any other approach?

I watched this game and is very nice how he walks on ground: http://www.youtube.com/watch?v=DmSAQwbbig8&feature=player_embedded

If there are "platforms" in mid air, how should I handle them?I can walk over them but I can't pass "inside". Imagine a platform in mid air, it allows you to walk over it but limit you because you can't jump in the area she fits

Sorry for my english, it's not my native language and this topic has a lot of keywords I don't know so I have to use workarounds Thanks for any answer

Additional informations and suggestions: I'm doing a game course in this period and I asked them how to do this, they suggested me this approach (a QuadTree): -All map is divided into "big nodes" -Each bigger node has sub nodes, to find where the player is -You can find player's node with a ray on player position -When you find the node where the player is, you can do collision check through all pixels (which can be 100-200px nothing more)

Here is an example, however i didn't show very well the bigger nodes because i'm not very good with photoshop :P

alt text

How is this approach?

share|improve this question
    
We are talking an art based sidescroller, rather than a tile based? –  The Communist Duck Dec 4 '10 at 18:40
1  
Just think about all your entities as only being 1D. (when it comes to the floor) They only occupy a line on the ground. That way your guy could get behind bushes and stuff. Just think about everything as if they were paper pop outs in one of those pop out books. You could then make some entities that could occupy space like a wall as well. Wasn't sure if this would suit as an answer, I still don't get exactly what you want, but this is what I think your thinking. –  Michael Coleman Dec 4 '10 at 21:46
    
@The Communist Duck: Yes I think, I don't know it's name –  Fire-Dragon-DoL Dec 7 '10 at 15:49

3 Answers 3

Typically the way I've handled this in the past is by creating metadata for your level that is nothing more than a series of ordered points to define line segments, and doing player collision vs. line segment collision on them. For a tile based game you only have to author data for those tiles. This is better than trying to do pixel tests since it's easy to define how things interact with lines than a series of pixels.

Then it's a simple matter of defining how you want your collision on your character to be. Circles are the easiest. Usually you probably want to do something like a pill shape, though (two circles and a rectangle).

Basically it's a simplified form of the idea of a collision model that a lot of 3D games use, except it's just lines.

share|improve this answer
    
What format do you recommend storing the points? XML? Also, do you go through the entire file checking for lines or only a section of it that your character is near? –  DMan Dec 4 '10 at 21:45
    
Format doesn't matter. Whatever is easiest in your framework. Just check all lines first and if it's too slow then look into optimizations. Maybe store them in a tree. Maybe only check nearby tiles. –  Tetrad Dec 4 '10 at 21:51
    
Let's say we have a small level, no optimizations needed. Do we take the line coordinates (maybe similar to an SVG file?), construct them so they represent lines, then do rectangle-line, circle-line, etc collision checking on them? –  DMan Dec 4 '10 at 22:16
    
That's one way to do it, yes. You can get really good results with traces/sweep tests. –  Tetrad Dec 4 '10 at 22:43
    
Interesting, thank you! –  DMan Dec 5 '10 at 4:49

It doesn't necessarily deal with floating platforms and jumping, but consider the idea of walkable areas as opposed to walkable surfaces. For example here's a forest with a sloping path. You only want the player walking on the light dirt path.

alt text

Now have an invisible bound area that defines the places the player can walk. Here's what it might look like overlaid on the forest path. It would NOT be rendered in the actual game of course. alt text

Then you put your player in the picture, and only allow their origin point (probably base of their feet) to be inside the walk zone.
alt text

This would work pretty well if you let the player click on any spot, and if it was in the walk zone they would walk to that spot.

Game Maker allows you to do something like this.

share|improve this answer
2  
This is called a collision mask (at least in 2D). –  doppelgreener Dec 6 '10 at 20:52
1  
This suggestion, pictures too, are very nice –  Fire-Dragon-DoL Dec 6 '10 at 23:34
    
There is a problem.... what about jump? –  Fire-Dragon-DoL Dec 7 '10 at 1:26
2  
@fire-dragon: Concerning a jump: You could use a vertical offset to your players position. The base Position of the player will always be confined to the collision masked area, while the offset may carry the player's sprite above the collison area temporarly. Since the base position can'T leave the collision area, the player will land on it's inside at all times –  sum1stolemyname Dec 7 '10 at 8:04
1  
Last missing thing: what should I do if I want to jump on a mid-air platform? I'm thinking about preserving a ray that always fall from the upper part to the player... –  Fire-Dragon-DoL Dec 8 '10 at 5:27

Your question is a bit broad, it seems to hint that you are not perfectly comfortable with the programming and/or mathematics involved.

What I describe here is what I after long consideration believe to be easiest for you to understand and implement:

Your world is split into a series of relatively narrow vertical stripes, something like 20 pixels wide each. For each stripe you define a number of lines stretching from one side of the stripe to the other, these lines should be placed to match the surface of the ground in that given area.

Imagine your character represented as two vertical line pieces, one stretching from the top of the character and approximately 75% down, the other from the bottom and approximately 75% up, so they overlap on the middle.

In order to do collision detection you first determine in what stripe the player is located, then you determine the exact vertical position of each line of that stripe at the players exact horizontal location, then you determine if any of the two lines representing the player overlap any of the stripe lines. If only the lower part of the player overlap anything then there is a collision with the ground, and the player needs to be moved up accordingly, if only the upper part of the player overlap something then there is a collision with the roof, and the player needs to be moved down. If both parts collide, or if the moving up or down cause the other part to collide, then there is a horizontal collision and the player must be moved back where he came from.

It's hard stuff to mess with if you are not a skilled mathematician, but I hope you can use it. I'm afraid it doesn't get a lot easier unless you resort to something block based.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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