I’m making a platformer with Slick, so I’ve got a level made up of tiles that the player (currently a green rectangle) can collide into and walk on etc.
I’ve based my collision detection largely on the JNRDev tutorial here:
http://wiki.allegro.cc/index.php?title=Pixelate:Issue_13/jnrdev
I also have gravity working, where when the player is in the air, a gravity float value is added to the player’s y velocity (and multiplied by Slick’s delta value, keeping track of time since the last frame and making values consistent regardless of frame rate) and when the player falls into a platform, the player stops, is repositioned in line with the platform etc.
However, I’ve noticed some jittering behaviour… especially when I let the player’s fall speed get higher. I suspect that’s probably just making it more obvious and it’s happening regardless of that, but w/e. Also of note is that even with the terminal velocity set relatively high, I don’t always notice any jitter, though sometimes it’s very noticeable. I imagine it depends on the exact velocity values at impact and where the colliding tiles are in relation to the player etc.
I’ve drawn a diagram to explain it clearly:
http://img27.imageshack.us/img27/2296/platformjitterdiagram.png
As you can see, what happens is that the player is moving downwards and to the left (or right) at the same time. On colliding with the platform, the player often sharply snaps to the right a few pixels as well as properly landing on the platform surface.
Presumably, since colliding tiles to the left are checked when the player is moving in that direction and repositioned appropriately, and horizontal collisions are checked first… what must be happening is that a collision with the tile below is detected while moving to the left, and as a result the player is undesirably being repositioned to that tile’s right side… which is what the code is meant to do. It’s just of course that it looks terrible and would also potentially interfere with gameplay somewhat.
Like I said though, it doesn’t always appear to happen, and I’m not sure why that is… it could just be that the positioning is very small and unnoticeable, even when it seems as though the player’s side edge is colliding with the center or so of a tile… but that’s probably not important.
Knowing what’s wrong pretty clearly… how do I actually fix it? :persecutioncomplex:
Horizontal collisions need to be addressed, and depending on the layout of the map and the collision, I may want the player’s horizontal movement to be altered rather than the vertical in some cases. I can’t simply avoid checking the player’s sides when moving up/down or going at a certain speed.
I can see roughly what I want to do… but I’m finding it hard to properly define what should be done in an exact methodical way that would work in all situations
Granted, something like this doesn’t need to be a pixel perfect representation of the real world. For instance, in the case of landing on that platform, ignoring the x repositioning altogether and simply raising the player up above the platform would like fine, though realistically part of that leftward movement would have been achieved by traveling through solid rock but no one would notice or care.
Even still, given the various circumstances that might crop up… do I have to alter my collision detection almost entirely and use some sort of gradual pixel-by-pixel testing of at what point the collision occurs when traveling in each axis, and reposition to the point just before colliding? Which is also complicated by my use of floats to store position, but I guess that could be got around…
Any thoughts? This must be a common problem, and I think I had a similar one with a top-down shooter I made in C++.