selfmade collision system doesn't work well
Posted: Sat Aug 03, 2019 3:42 pm
Hello!
I'm new to programming with LÖVE (but not with programming in general, however I consider myself not that good. I still have a lot to learn).
After getting to know Lua and LÖVE I decided to create a little platformer game to exercise.
At first I followed a platformer tutorial but after a bit I started going my own way.
So a couple of days ago I tried to implement my own collision system (I didn't want to use libraries like box2D).
It's a very simple one. Basically: My world is divided in tiles. Every tile has a specific property (solid, air, etc).
If the player moves and touches a solid block then I set the player to the previous position and update the velocity.
However after implementing this the collision is very glitchy. I think that this has to do with the dt value passed to the update function.
For my collision system to work, the collisions must be calculated pixel per pixel. So I calculate the new position, check the coordinate and if the new position is colliding with a solid tile then I stop velocity and I set the position to the previous position.
But I think when the collisions are calculated, the player moves multiple pixels (so the previous position is far too different)
Atleast that's what I think is wrong. I already searched the forums and I tried different solutions listed on those topics (rounding the coordinates, accumulating dt,...) but it didn't fix anything (maybe I did something wrong)
I hope you guys can point me to the right direction.
And for anyone reading my code: I tried to apply OOP in lua but I am not sure if I did it right (I tried to use a metatable in my renderer and it seems to work). If you have any remarks on my code, please tell! And sorry if made any mistakes writing this, english isn't my first language.
I'm new to programming with LÖVE (but not with programming in general, however I consider myself not that good. I still have a lot to learn).
After getting to know Lua and LÖVE I decided to create a little platformer game to exercise.
At first I followed a platformer tutorial but after a bit I started going my own way.
So a couple of days ago I tried to implement my own collision system (I didn't want to use libraries like box2D).
It's a very simple one. Basically: My world is divided in tiles. Every tile has a specific property (solid, air, etc).
If the player moves and touches a solid block then I set the player to the previous position and update the velocity.
However after implementing this the collision is very glitchy. I think that this has to do with the dt value passed to the update function.
For my collision system to work, the collisions must be calculated pixel per pixel. So I calculate the new position, check the coordinate and if the new position is colliding with a solid tile then I stop velocity and I set the position to the previous position.
But I think when the collisions are calculated, the player moves multiple pixels (so the previous position is far too different)
Atleast that's what I think is wrong. I already searched the forums and I tried different solutions listed on those topics (rounding the coordinates, accumulating dt,...) but it didn't fix anything (maybe I did something wrong)
I hope you guys can point me to the right direction.
And for anyone reading my code: I tried to apply OOP in lua but I am not sure if I did it right (I tried to use a metatable in my renderer and it seems to work). If you have any remarks on my code, please tell! And sorry if made any mistakes writing this, english isn't my first language.