Problem making a sidescroller.

Official forum for the Chipmunk2D Physics Library.
Post Reply
the pizza boy
Posts: 5
Joined: Thu Apr 24, 2008 9:03 pm
Contact:

Problem making a sidescroller.

Post by the pizza boy »

In my side scrolling game I have just a player and some square platforms. The player is a 13x18 square and the platforms are 32x32. I have got the player to move around and jump just fine, but I noticed a problem. When moving across a line of blocks, the character will sometimes become stuck at a point where one block ends and another begins. When this happens, moving the other way a bit and then moving back will sometimes let you pass over it. I can't figure out why this is happening and I am puzzled as to why it isn't a consistent bug. Does anyone have any ideas about what is causing it? I will answer any questions about my game's code or post requested parts of it. (Because I think this will be asked, I am moving the character by directly editing the velocity, but I have also tried ApplyForce and ApplyImpulse and surface_v, and they all give the same problem with randomly getting stuck.)
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Problem making a sidescroller.

Post by slembcke »

The issue is that Chipmunk allows a small amount of overlap for objects in contact. As the player slides along, they if they are overlapping the corner of the next ground section by a smaller amount, then the wrong separating axis is chosen. By decreasing cp_collision_slop (look in the docs) you can reduce but not eliminate the problem.

A better solution would be to either bevel the edges of the player collision shape or add a circle to the bottom. I actually prefer the latter, as having a second shape allows you to set it's surface velocity separately. Objects that fall on top of the character won't slide off then. Also, a circle looks much better as an approximation of legs anyway.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
mcc
Posts: 27
Joined: Sun Mar 30, 2008 9:00 pm
Contact:

Re: Problem making a sidescroller.

Post by mcc »

The way I have resolved this in the game I am making is that when I am adding blocks to the space, I check to see if I have any sections of contiguous blocks. So if I have five floor blocks right next to each other, within chipmunkspace this will actually be represented by one long piece that happens to be five blocklengths long.

Also something that may help is to add a debug mode, wherein you copy the code from the "moonbuggy" demo that places a visible dot at the point of each persistent contact. I did this and this is how I was able to figure out the problem when I first encountered it, because you can clearly see when the moving object gets "stuck" that it's doing so at the seam between two blocks. This has come in handy at other times too.
the pizza boy
Posts: 5
Joined: Thu Apr 24, 2008 9:03 pm
Contact:

Re: Problem making a sidescroller.

Post by the pizza boy »

@slembcke
I have actually tried both setting cp_collision_slop to 0 and rounding the edges of my character, but I haven't tried them both together. If combining them fails, I will try using a circle for feet. Thanks for the idea.

@mcc
I thought of doing this, but it wouldn't work in certain situations in my game where I may need one block in the line to fall away after the character steps on it. I can't actually get the moonbuggy tutorial to compile, but I will try to code my own way to draw the collision points (I am using SDL to render at the moment).
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests