I've been doing extensive research on this topic for the past few days and I can't seem to find an answer for my exact problem.
So, I have a simple game set up where I have the player at 0, 0 with a width of 10x10
player= pygame.Rect(0, 0, 10, 10)
and aside from that, the player has a velocity of x: 0, y: 10, which will make him fall (y is positive because the origin of the screen is at the top left.)
and I have a tile at 0, 100, as shown:
dirt= pygame.Rect(0, 100, 10, 10)
so, how can I handle collision, I already know I can detect it with Rect.colliderect(Rect).
I've tried a few ways, but encountered some problems:
I can't cut the player's velocity to 0 when he hits something and then move him back until he's just touching the object because that still causes the problem of walking, when he walks, I apply +10 velocity on x, but unfortunately, the game still processes that he is falling and colliding and moving sideways, so it just moves him back to where he started.
I'm a beginner, so a simple answer would be appreciated, and I would like to not have to use any third party modules other that pygame if I didn't have to.
Update:
Here is some of the rough test code I have tried:
def sim(obj, time, world):
time= time / 1000
obj.physProp['vel']= (obj.physProp['vel'][0] + (accel[0] * time), obj.physProp['vel'][1] + (accel[1] * time))
if obj.physProp['vel'][1] > terminalY:
obj.physProp['vel']= (obj.physProp['vel'][0], terminalY)
obj.pos= (obj.pos[0] + (obj.physProp['vel'][0] * time) + ((accel[0] / 2) * (time ** 2)), obj.pos[1] + (obj.physProp['vel'][1] * time) + ((accel[1] / 2) * (time ** 2)))
for ID in world:
if obj.getRect().colliderect(world[ID].getRect()) == True:
pass
return (obj.pos, obj.physProp['vel'])