Page 1 of 1
How can I implement tile collisions with a platformer character without box2d or physics?
Posted: Thu Apr 20, 2023 1:27 am
by MaxGamz
I've been trying to make a new game on love2d and I came across a problem with using tile maps, I would like to use simple physics for jumping and moving around in my game but I'm confused on how I could implement it for floating platforms.
For context, here is a link to what I used for the physics in my game.
https://love2d.org/wiki/Tutorial:Baseline_2D_Platformer
Re: How can I implement tile collisions with a platformer character without box2d or physics?
Posted: Thu Apr 20, 2023 3:11 am
by BrotSagtMist
I am confused which method you ask for here.
Tiles or floating platform? Booth are fundamentally different.
For Tiled detection we place a bigger grid above the pixels, this grid contains the info on what is wall or floor.
Then we collide by simply testing on what field our object is:
if Grid[math.floor(x/tilesize)][math.floor(y/tilesize)]=="floor" then
Floating platforms are simply checked by their boundaries:
if x>platformx and x<platformx+size and y>platformy and y<platformy+size then
This is a bad method for big maps. But works splendid on small ones.
The method is simply to SAVE the position to a variable, then apply the forces:
velocity=velocity+gravitation*time
position=position+velocity*time
Then do the collision check as above, and if it collides simply revert to the saved position and zero velocity.
That way we stay on ground.
Re: How can I implement tile collisions with a platformer character without box2d or physics?
Posted: Thu Apr 20, 2023 3:18 am
by MaxGamz
BrotSagtMist wrote: ↑Thu Apr 20, 2023 3:11 am
I am confused which method you ask for here.
Tiles or floating platform? Booth are fundamentally different.
For Tiled detection we place a bigger grid above the pixels, this grid contains the info on what is wall or floor.
Then we collide by simply testing on what field our object is:
if Grid[math.floor(x/tilesize)][math.floor(y/tilesize)]=="floor" then
Floating platforms are simply checked by their boundaries:
if x>platformx and x<platformx+size and y>platformy and y<platformy+size then
This is a bad method for big maps. But works splendid on small ones.
The method is simply to SAVE the position to a variable, then apply the forces:
velocity=velocity+gravitation*time
position=position+velocity*time
Then do the collision check as above, and if it collides simply revert to the saved position and zero velocity.
That way we stay on ground.
Oh I meant mostly tiles specifically, I was giving an example of the floating platform because the way I have my code set up for the character to treat the horizontal line in the middle of the screen as the ground and ignore everything else.