Tile-Based Platformer Collisions

Blitz3D Forums/Blitz3D Programming/Tile-Based Platformer Collisions

ClayPigeon(Posted 2010) [#1]
Alright, I gotta admit, it's kinda funny that I was about to create a new topic for this and coincidentally, there just happened to be a topic near the top asking about the same thing I am. I still didn't find what I was looking for because my problem is a little more complicated. I'm making a 2D tile-based platformer. I made a level editor dedicated to the development of this game, that creates .lvl files that contain the data for levels. It only contains collision information, but not image tiles. I have the player being controlled with basic 2D verlet integration. The player's x or y velocity can not exceed 4 pixels per frame. Each collision tile on the screen is 32x32 pixels. "player_rx" and "player_ry" are the current room the player is in (it's not relevant to the problem). This is my code to find out if the player has collided with any block:
collided = False
For y = 0 To 23
	For x = 0 To 31
		If tile(player_rx,player_ry,player_x/32,player_y/32) = 1 And RectsOverlap(player_x-12,player_y-12,24,24,x*32,y*32,32,32)
			collided = True
		EndIf
	Next
Next


It sets the var "collided" to false, then parses through all the tiles on the screen, checking whether the tile is occupied by a block and whether the player has collided with them. If it has, it sets collided to true, and that's that - it's colliding. So, what's been driving me crazy that past few days is how to perform the collision response. I got really close once, by moving the player by it's x and y velocities, then (if there's a collision), inch the player towards it old x and y position until there's no collision anymore. But, that always ended up freezing up when one of the player's velocities was 0 because it got stuck in an infinite repeat loop. Can anyone help me out with this? (Let me know if I'm not supplying enough info.)


_Skully(Posted 2010) [#2]
If you are using verlet integration then moving the particular verlet out of the collision zone should cause a response for the other verlets that are part of that equation. The player should likely be a centre verlet that is influenced by "feeler" verlets.

Since verlet uses its last position to determine velocity it should be somewhat workable but you may have to dampen the response slightly (add friction) so it doesnt fly around.

not sure that helps you.


Who was John Galt?(Posted 2010) [#3]
There is a much quicker way. First figure out which terrain tiles overlap with the player's image algorithmically, then check for actual collisions with only those tile images.

http://www.blitzmax.com/Community/posts.php?topic=26783#280252


ClayPigeon(Posted 2010) [#4]
I'm not worried about pixel perfect collisions. I have bounding box collisions with the box of the player (24x24) and the collision tiles. All the collision tiles are 32x32 squares. I'm trying to figure out the collision response.


Warner(Posted 2010) [#5]
Instead of responding to a collision after moving your object, maybe it is easier to check if a collision will occur before moving your object? If a collision would occur, then don't move the object.


ClayPigeon(Posted 2010) [#6]
Oh, I forgot to say that. I've tried both prevention and correction, and neither of them seem to work right. (of course, they're not real methods, I'm sort of improvising) Also, if I tell it not to move the object, I end up with a big gap between the wall and the player. I don't want a code you put in the keyboard/player movement events, because that's where I affect the verlet.


ClayPigeon(Posted 2010) [#7]
Alright, this is my most successful attempt yet:

No crashes, sudden jerks of the player's position, everything's great except for one fact - collisions slow the player's movement to what looks like one pixel per frame instead of stopping them. I can't figure out why it's doing that! RAAAAGGGEE!!!


Nate the Great(Posted 2010) [#8]
me and a friend were redoing my circleman game and we came across issues like this. It is probably best to use a physics engine (I dont think there is one for b3d unfortunately) but the key is patience and knowing when to start fresh it took us 2 days and starting fresh three times over google wave to get all collisions in the game perfect.


_Skully(Posted 2010) [#9]
Funny that platformer stuff has come up twice now

I do have the TileMaxPlusEd demo I put out ages ago

run fullscreen2
click the lower window to add atomatons

The way I'm doing collisions here is that I've wrapped the image in a polygon. When checking for collisions, each polygon point is checked to see if it collides, if it does, its moved back along its normal until it no longer collides. A proportionate amount of movement is applied to the sprite to move it away in the opposite direction. Have a look and let me know how it works for you.


ClayPigeon(Posted 2010) [#10]
Sorry, I need to brush up on my proofreading! I'm an IDIOT. You see where I'm referencing the tile in the array? that shouldn't be
player_x/32,player_y/32
it should be
x,y
...
Well it works perfectly without bugs, and I even got flawless jump-through platforms! Sorry, guys.


Warner(Posted 2010) [#11]
Ah, glad that this is solved then. I thought as a reference, I'd post a link to my archive entry on this: http://www.blitzmax.com/codearcs/codearcs.php?code=2374
Good luck with your platformer!