How can I implement tile collisions with a platformer character without box2d or physics?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
MaxGamz
Party member
Posts: 107
Joined: Fri Oct 28, 2022 3:09 am

How can I implement tile collisions with a platformer character without box2d or physics?

Post 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
User avatar
BrotSagtMist
Party member
Posts: 665
Joined: Fri Aug 06, 2021 10:30 pm

Re: How can I implement tile collisions with a platformer character without box2d or physics?

Post 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.
obey
MaxGamz
Party member
Posts: 107
Joined: Fri Oct 28, 2022 3:09 am

Re: How can I implement tile collisions with a platformer character without box2d or physics?

Post 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.
Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests