Movement issue with strafe

Here is my code from CubeWars for player movement:


      public void forward() {
            xpos -= (float) Math.sin(heading * CubeWars.piover180) * speed;
            zpos -= (float) Math.cos(heading * CubeWars.piover180) * speed;                  
      }
      
      public void reverse() {
            xpos += (float) Math.sin(heading * CubeWars.piover180) * speed;
            zpos += (float) Math.cos(heading * CubeWars.piover180) * speed;            
      }
      
      public void turnRight() {
            heading -= turn;
      }
      
      public void turnLeft() {
            heading += turn;
      }
      
      public void strafeRight() {;
            xpos += (float) Math.cos(heading * CubeWars.piover180) * speed;
            zpos -= (float) Math.sin(heading * CubeWars.piover180) * speed;                  
      }
      
      public void strafeLeft() {;
            xpos -= (float) Math.cos(heading * CubeWars.piover180) * speed;
            zpos += (float) Math.sin(heading * CubeWars.piover180) * speed;                  
      }

Looking for suggestions on what to change on strafing to not doulbe players speed. thanks!!!

Are you referring to when a player may be say moving forward and also straffing in a single update? I solved this with Auriga3D by scaling the “speed” factor by a “half movement” value in these cases.

Yes when forward/backward and strafe in a single tick.

[quote]Yes when forward/backward and strafe in a single tick.
[/quote]
Yeah… I actually use a switch statement that works with a movement state that is created by bitwise ops |= and ^= based on the current key state. This makes it quick and easy to detect when straffing and movement is occuring. It also makes things easy in regard to cancelling movement when say strafeleft and straferight are held down simultaneosly.

You probably will want to make your movement functions accept a speed and depending on what your movement state is pass in the unmodified speed variable or a scaled value.

I find it easier to:

  • Process all the keystates, adding their effect into a resultant velocity/force.
  • Normalise the force to length 1.
  • Scale by your required speed.

Simple & works for anything you can throw at it. Sure you’ve got a square root in there, but you’re only going to be doing it once every frame.