I have a 2D top-down RPG type game, with movement in 8 directions (N/E/S/W/NE/NW/SE/SW) involving both x and y velocity.
I'm finding when I'm moving in the diagonal directions, I'm building both x and y velocity and it's making my character move faster than they do in a straight line along only x or only y. Is there a fix for this, so no matter which of the 8 directions I travel along, it's the same speed?
My code works like this. I initialize my variables and speed:
vel_x, vel_y=0
spd=1
Then each frame in my game loop, I apply drag to reduce the velocity:
vel_x*=0.2
vel_y*=0.2
Then my button presses add/subtract the spd
value from their corresponding velocity direction x/y when pressed.
Lastly, I add the vel_x
to my sprite's x position and same with vel_y
for my sprite's y position.
How would I address this? Would I look at button press actions for when both keys are held, or limits for when vel_x
and vel_y
are 'high' enough in tandem?
I've done the latter and it helps, but I can't make it accurate.
Images: With velocity removed and just +-1 speed increases/decreases. Left is the proposed adjustment to the vertical movement. Right is no adjustment. Is left moving at same speed all the time and right isn't?