C
C#•3y ago
Costin

Pixel-Perfect collision for tile-based game

So I have a 2D tiled Game that has gravity, and players slides with velocity when moving. the max speed can be -16 to 16 both x and y. What I want is to implement pixel-perfect collision, so that when the player jumps or goes through a one tile air, it goes through instead of avoiding it. It's also multiplayer but I already implemented networking. Both players and tiles are 16x16.
149 Replies
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Costin
CostinOP•3y ago
box2d is too complicated and too big for the simple collision I want in my game. my game will only consist of rects and never have fluid/dynamic mechanics apart from non-rotating rect players. and I don't know how to move objects pixel-by-pixel every frame
TheRanger
TheRanger•3y ago
like sonic the hedgehog games?
Costin
CostinOP•3y ago
kindoff the players slides basically there's a little physics like dragging, gravity, and a velocity but that's all that the game's physics will offer and it's just applicable for players (they don't collide with each other btw)
TheRanger
TheRanger•3y ago
well Sonic does have pixel perfect collision detection
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
TheRanger
TheRanger•3y ago
there's a programming guide on how to implement Sonic physics ,need link?
Costin
CostinOP•3y ago
also there's gonna half tiles which basically has a size of half a normal tile (either 8x16 or 16x8) sure
Costin
CostinOP•3y ago
the max speed that the players can go is -16 to +16 which means 1 full tile per frame I suppose however at normal speed they will only go up to smth like 8. (the max speed is reached only if the player gets boosted either by a specific non-collidable tile, or by a speed boost effect 'potion')
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Costin
CostinOP•3y ago
2. but isn't that gonna lag?
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Costin
CostinOP•3y ago
yeah but imagine detecting 16 pixels for both x and y
TheRanger
TheRanger•3y ago
ur saying to loop through every tile?
Costin
CostinOP•3y ago
no no
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
TheRanger
TheRanger•3y ago
link above does show that start and end point of a tile ur on yeah my bad, it doesnt loop, it immediatly accesses it
Costin
CostinOP•3y ago
I actually have a collision library ready (named JBump), and it gets collisions only at players, but the library doesn't check pixel-by-pixel (but it can detect fast tunnel-traveling or whatever it's named)
TheRanger
TheRanger•3y ago
Sonic did not check pixel by pixel
Costin
CostinOP•3y ago
so what do I do?
TheRanger
TheRanger•3y ago
there is a category called Solid Tiles there, should give you some ideas
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Costin
CostinOP•3y ago
also JBump is able to get collisions by points and lines so do I check collisions for a line from past x to current x?
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Costin
CostinOP•3y ago
yes, JBump knows if you go into a tile or past a tile
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Costin
CostinOP•3y ago
but the thing is that if there's a 1 tile gap hole on the ground or on the wall, and the player goes a bit fast, it can easily avoid it and thus not fall/go through it
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Costin
CostinOP•3y ago
😦 but I don't want to avoid that one tile gap
TheRanger
TheRanger•3y ago
what do you mean?
Costin
CostinOP•3y ago
wait
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
TheRanger
TheRanger•3y ago
O_O
Costin
CostinOP•3y ago
Costin
CostinOP•3y ago
this is how a one block gap looks like
TheRanger
TheRanger•3y ago
ive actually implemented physics from scratch, thanks to some tutorials
Costin
CostinOP•3y ago
if the player jumps and moves towards the wall, it will not go through that vertical air or if the player moves a bit fast, it will not go through the ground hole
TheRanger
TheRanger•3y ago
how big is ur collision box?
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Costin
CostinOP•3y ago
16x16 both players AND tile
TheRanger
TheRanger•3y ago
well you can check if there is an empty tile under the player, then set their speed to 0 and align their position onto that empty block
Costin
CostinOP•3y ago
and how do i check exactly? in the bottom center of the player?
TheRanger
TheRanger•3y ago
yeah
Costin
CostinOP•3y ago
because if I do that, the player will then teleport a bit to the right/left
TheRanger
TheRanger•3y ago
?
Costin
CostinOP•3y ago
so that it can go aligned with that gap the player will teleport if I check in the player's bottom center use your brain
TheRanger
TheRanger•3y ago
checking is just an if condition, why would that teleport the player
Costin
CostinOP•3y ago
because I need to SET the x of the player to be equal to the tile's x, and the y -= 1
TheRanger
TheRanger•3y ago
use player's x speed to predict the position of the player of the next frame then with the predicted position, check if there is an empty tile below that predicted position
Costin
CostinOP•3y ago
so now I would have a line between player's x and (x + speed x)
TheRanger
TheRanger•3y ago
also if im gonna be honest, that annoyed me a bit
Costin
CostinOP•3y ago
excuse me for that
TheRanger
TheRanger•3y ago
what do you need that for?
Costin
CostinOP•3y ago
how do I check if there's a gap
TheRanger
TheRanger•3y ago
i believe i mentioned it here or im not sure what u mean
Costin
CostinOP•3y ago
and what's "predicted position"? is it x + velX?
TheRanger
TheRanger•3y ago
yes
Costin
CostinOP•3y ago
but that's the future x with the current x, and the future x, how do I check if there's a gap?
TheRanger
TheRanger•3y ago
actually you dont really need it 1 min
TheRanger
TheRanger•3y ago
Costin
CostinOP•3y ago
yes but what if the player moves faster?
TheRanger
TheRanger•3y ago
lets say the player moved from left square to right square, on the same frame, before the game renders his max speed is 16 right?
Costin
CostinOP•3y ago
yes
TheRanger
TheRanger•3y ago
then no worries
Costin
CostinOP•3y ago
but that's EXACTLY one tile
TheRanger
TheRanger•3y ago
yeah check the tile below from player's left bottom collision
Costin
CostinOP•3y ago
so it would be the old collision = x + 16, and the new collision = nX but that can make a length of 16 pixels old collision - new collision
TheRanger
TheRanger•3y ago
yeah, u can still check tho
Costin
CostinOP•3y ago
oh ok so now what having (x+16) and nX
TheRanger
TheRanger•3y ago
for example sonic only has 2 collision points, bottom left and bottom right collision box of the player in the picture bottom right would detect a tile bottom left did not detect a tile
Costin
CostinOP•3y ago
the player and tiles has a rect collision
TheRanger
TheRanger•3y ago
ok then you can loop through each pixel of the player's bottom rect atleast one of them will find the gap
Costin
CostinOP•3y ago
so
TheRanger
TheRanger•3y ago
once u find that gap, adjust the player's position to where that gap is
Costin
CostinOP•3y ago
((x+16) - nX) = line (between player's pos and future pos) foreach(int x: line) { jbump.checkPixel(x) } and checkPixel would tell me if there's a collision at that pixel
TheRanger
TheRanger•3y ago
yeah u dont really need a future position btw you can just check after the player moves before the game renders ofcourse
Costin
CostinOP•3y ago
but that would always return no collision as that pixel is where the player would be maybe pixel.y -= 1 anyway
TheRanger
TheRanger•3y ago
what do u mean?
Costin
CostinOP•3y ago
what i said the pixels I am checking are gonna be inside of the player wait no what i mean is pixel.y is = to player.y thus, no collision
TheRanger
TheRanger•3y ago
where exactly is the pivot of the player?
Costin
CostinOP•3y ago
bottom left same as tiles and it extends 16x16 to up right
TheRanger
TheRanger•3y ago
as long as the player is on a tile, there is always a collision
Costin
CostinOP•3y ago
not really the player is ON a tile not in a tile
TheRanger
TheRanger•3y ago
just check the tile below the pixel
Costin
CostinOP•3y ago
so pixel.y - 1
TheRanger
TheRanger•3y ago
yeah
Costin
CostinOP•3y ago
ok, so now I would have a few pixels that doesn't collide like the other pixels. now what do I do, knowing the positions of every non-colliding pixels?
TheRanger
TheRanger•3y ago
this is where the player should be on the gap right?
Costin
CostinOP•3y ago
yes it is all the pixels below the player that do not touch a tile now knowing them, what do I do?
TheRanger
TheRanger•3y ago
change the player's position to fit into that gap tile
Costin
CostinOP•3y ago
so if one pixel doesn't collide, i teleport the player?
TheRanger
TheRanger•3y ago
yes
Costin
CostinOP•3y ago
that's stupid
TheRanger
TheRanger•3y ago
not if player's x speed is 1
Costin
CostinOP•3y ago
but player's x speed is up to 16 what do I do if the speed is 8?
TheRanger
TheRanger•3y ago
yeah player has to pass through the tile before the gap first
Costin
CostinOP•3y ago
I don't understand OH but wait
TheRanger
TheRanger•3y ago
if the player fell before he completely passed the last solid tile he was on, it would look like he teleported so id check if there is a gap at the bottom left pixel of the collision rectangle if player's xspeed is > 0 otherwise id check the bottom right pixel of the collision rectangle if player's x speed is < 0
Costin
CostinOP•3y ago
Costin
CostinOP•3y ago
oh ok
TheRanger
TheRanger•3y ago
what are u trying to show me?
Costin
CostinOP•3y ago
but doesn't that mean that I do not have to check every pixel?
TheRanger
TheRanger•3y ago
its pointless to check every pixel, since the tile is a full block anyway
Costin
CostinOP•3y ago
so I check the player's future position (which is the bottom left corner) and if below that pos there's nothing, the player's velX is set to 0, x = air tile x, and y -= 1?
TheRanger
TheRanger•3y ago
pretty much yes can the player move on air?
Costin
CostinOP•3y ago
and I do that for velX > 0, velX < 0, velY > 0, and velY < 0 respectively? only with an effect there's gravitational effects in-game
TheRanger
TheRanger•3y ago
because if ur holding right, after u teleported to that gap, u might land on the next tile
Costin
CostinOP•3y ago
like boosting which makes the player go up to full speed (16) or like no gravity effect or super gravity effect or negative gravity or wall gravity but I can make that thing not apply if the gravity is 0
TheRanger
TheRanger•3y ago
in order to prevent that, if ur holding right, check if bottom right pixel's x + 1 has a tile there
Costin
CostinOP•3y ago
you mean - 1?
TheRanger
TheRanger•3y ago
is X from left to right or right to left?
Costin
CostinOP•3y ago
origin (0) is left
TheRanger
TheRanger•3y ago
then i mean +1
Costin
CostinOP•3y ago
why
TheRanger
TheRanger•3y ago
u want to check if there is a tile next to the bottom right pixel
Costin
CostinOP•3y ago
so you mean x + 17? afk
TheRanger
TheRanger•3y ago
Costin
CostinOP•3y ago
?
TheRanger
TheRanger•3y ago
this is where the player will be after u teleport him
Costin
CostinOP•3y ago
yes
TheRanger
TheRanger•3y ago
what if you were holding right?
Costin
CostinOP•3y ago
your velX is zeroed, y -= 1, and X = block.x
TheRanger
TheRanger•3y ago
but holding right increases velocity right?
Costin
CostinOP•3y ago
but that only if the player's right corner is still touching a tile yes
TheRanger
TheRanger•3y ago
so he might land on the next tile on the next frame
Costin
CostinOP•3y ago
wdym afk
TheRanger
TheRanger•3y ago
if the player is falling and ur holding right he might land on the tile on his right
Costin
CostinOP•3y ago
not really because he touches the right block
TheRanger
TheRanger•3y ago
ok i guess you implemented that logic
Costin
CostinOP•3y ago
jBump already checks collision, but it doesn't check pixel-perfectly
TheRanger
TheRanger•3y ago
brb atleast 15 minutes i need to do something
Costin
CostinOP•3y ago
ok same @TheRanger done?
TheRanger
TheRanger•3y ago
back
Costin
CostinOP•3y ago
k
TheRanger
TheRanger•3y ago
Did you need something?
Costin
CostinOP•3y ago
yes read up
TheRanger
TheRanger•3y ago
this?
Costin
CostinOP•3y ago
yes
TheRanger
TheRanger•3y ago
i have no idea what jBump is
Costin
CostinOP•3y ago
it's a simple AABB based collision library in java
TheRanger
TheRanger•3y ago
ur coding the game on java?
Costin
CostinOP•3y ago
yeah
TheRanger
TheRanger•3y ago
kinda odd for me to ask in a C# server
Costin
CostinOP•3y ago
doesn't matter it's still C# syntax and the question I am asking (pixel-perfect collision) is not really language based
TheRanger
TheRanger•3y ago
it might be better for you to ask in a game programming server anyway as many people can help you, instead of only one
Costin
CostinOP•3y ago
then why did you try to help me
TheRanger
TheRanger•3y ago
why not? just saying more is better than 1
Costin
CostinOP•3y ago
ok
Want results from more Discord servers?
Add your server