My code is acting differently for negative velocities than it is positive ones
I'm trying to implement platformer physics, the player has velocity in the X direction, the velocity is increased or decreased when the user presses "A" or "D" respectively, or set to 0 when the player collides with a wall.
To simulate friction with the ground, the X Velocity of the player is multiplied with "self.drag" (a float less than 1)
I expected this code to reduce the players X Velocity, over time reducing it neglibly near to 0, without actually reversing the velocity (like subtracting a value would), this would stop the player sliding about uncontrollably when the user isn't imputing movement commands.
This works as intended when moving right, however when moving left it acts differently, when moving to the left the player seems to continue floating for a while before coming to a stop.
Here's the code that takes player input, inside the player class, run each frame:
dx = 0
if pygame.key.get_pressed()[pygame.K_a]:
dx -= self.speed
if pygame.key.get_pressed()[pygame.K_d]:
dx += self.speed
# to slow down horizontal movement
self.vx *= self.drag
# Add change in velocity to total velocity
self.vx += dx
self.vy += dy
Maybe the concept works and I've implemented it incorrectly? There's collision code that may be affecting the velocities in ways I haven't noticed? Does this system work differently for positive and negative velocities?
Thanks! Any help is much appreciated