WASD movement

Blitz3D Forums/Blitz3D Programming/WASD movement

lonnieh(Posted 2005) [#1]
I've been toying around with moving a sprite around the screen with WASD (A and D for strafing, W and S for forward and reverse) and using the mouses' X axis for the rotation.

However, in my test runs I've noticed that if I'm moving forward and strafing at the same time, the velocity increases to more than the max velocity in a single direction. I've figured the problem out, but I can't figure a way around it. Besides, the way I'm going about it probably isn't the right way :-)

Now.... anyone care to school me how this is done?


The r0nin(Posted 2005) [#2]
You can check for both keys being depressed at the same time. Sort of like: If W is down and D not move 4 units forward. If D is down and not W move 4 units right. If both are down, move (4*cos45) to the right and (4*sin45) up. That way the total distance traveled is always 4 units.


Tom(Posted 2005) [#3]
Tried Sin & Cos?

Hmm, dunno why I used 2D, but the principle is the same :)




lonnieh(Posted 2005) [#4]
Very helpful! Thanks r0nin, and Tom :-D


mrtricks(Posted 2005) [#5]
This is something even AAA games miss - I've got used to running diagonally in games like Perfect Dark to get somewhere quicker!


Tom(Posted 2005) [#6]
yeah the 'semi-crab' run is quickest :P


lonnieh(Posted 2005) [#7]
LOL! You know, I spend too much time gazing in awe that my crappy code even runs! Thats how I recognized it :-D


Rubiks14(Posted 2005) [#8]
you could alse check for both keys being pressed(w and d for example) and cut each speed enought to where it equals the max speed


The r0nin(Posted 2005) [#9]
Uhhh, Rubiks, that's exactly what the advice and code Tom and I gave him did (you have to use sine/cosine/geometry to do so)...


Cruis.In(Posted 2005) [#10]
no hes sayign insert a if speed > max speed, speed = max speed, that way he'll never go over the 4 units.


Rubiks14(Posted 2005) [#11]
oh lol i wasn't paying attention when i wrote that


The r0nin(Posted 2005) [#12]
Wouldn't work, Cruis.In. Let's say you put in a speed <= maxspeed with maxspeed = 4. The you press W to go up. You are now moving at 4 units up. Then, while holding down W, you press D. You should move up and right, but since any speed added to the object takes it above 4 units, you would continue to only move straight up, even though you had both keys down. The ONLY (I repeat ONLY) way to maintain a constant velocity when moving at non-divisible-by-90 angles is to use geometry. Without sine/cosine you have no way of knowing what portion of your maxspeed should be moved in the X direction and what portion should be in the Y, and you can't get around that because a computer monitor is nothing more than a grid of pixels.

So, sorry, but your suggestion would not work...


PowerPC603(Posted 2005) [#13]

you could alse check for both keys being pressed(w and d for example) and cut each speed enought to where it equals the max speed



Divide forward and sideways speed by 1.4142.

Move forward: 3 units
Move sideways: 3 units

Pythagoras:
SqRT((x*x) + (y*y)) = SqRT((3*3) + (3*3)) = 4.2426

->

3/1.4142 = 2.1213

->

SqRT((2.1213 * 2.1213) + (2.1213 * 2.1213)) = 2.9999


The r0nin(Posted 2005) [#14]
That's still using geometry *grin*... just you've done the calculations pre-programming rather than at run-time. It's easier to write one movement function using speed and direction and then sine/cosine the input to move the player (because this preserves the ability to turn the player in smaller increments than 45 degree angles, making the code portable)...