Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5988203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:55:33+00:00 2026-05-22T22:55:33+00:00

I am making a movement engine for my top-down 2D game, but I got

  • 0

I am making a movement engine for my top-down 2D game, but I got stuck trying to solve the following problem:

  • The player can move using the arrow keys, which accelerate you in the respective directions. There is friction so you stop moving after releasing the keys, though not instantly.
  • When you hold two perpendicular keys, you accelerate in this 45° direction the same speed as you would on one axis
  • There is a maximum speed which above you cannot accelerate by walking, this also limits your maximum walking speed, obviously. You can get knocked away, and thus exceed this speed however.
  • If you move faster than max. walkspeed, you can slow down faster if you hold keys in the opposite direction(s)

Pseudocode for the first point, without friction:

gameTick(){

  tempX += LeftKeyHeld ? -1 : 0;
  tempX += RightKeyHeld ? 1 : 0;
  tempY += UpKeyHeld ? -1 : 0;
  tempY += DownKeyHeld ? 1 : 0;
  ratio = 0.71;

  if( |tempX| == |tempY| ) {
    tempX =tempX* ratio;
    tempY =tempY* ratio;
  }
  player.x += tempX;
  player.y += tempY;
}

I can solve the friction (getting the length of the movement vector, reducing it by the friction, projecting it back with the same x:y ratio), however, I can’t wrap my head around getting the maxSpeed done.

I tried a solution of not allowing the player to walk at all when above maxSpeed, but this violates point 4. Also, it had the nasty side-effect, that when you moved at MaxSpeed to the left, and started pressing down, the movement direction didn’t, or barely changed.

Then I started thinking about numerous products, differences and other stuff with the vectors, but I mostly couldn’t follow it anymore or ran into early problems.

So in conclusion, could someone explain a system which fulfills all the points above, or point to an article which explains how a system like this could be implemented? Don’t worry about suggesting something complex, I can grasp even difficult concepts given some time.

Thanks for your help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T22:55:34+00:00Added an answer on May 22, 2026 at 10:55 pm

    Sorry for not letting you think about this for more than a day, but I managed to solve it, and it ain’t two lines of code. (Though still thanks for everyone for the ideas)

    Because I’m lazy and tired, I won’t change it to pseudocode, except the following two methods:

    updateGame(){
      player.walk();
      player.move();
    }
    player.move(){
      player.x += player.speedX
      player.y += player.speedY
    }  
    

    And the code (java):

    public void walk() {
        float tempX = 0;
        float tempY = 0;
        float accelX;
        float accelY;
        float nextSpeedX;
        float nextSpeedY;
        float nextSpeed;
        float speed;
        tempX += walkLeft ? -1 : 0;
        tempX += walkRight ? 1 : 0;
        tempY += walkUp ? -1 : 0;
        tempY += walkDown ? 1 : 0;
    
        if (Math.abs(tempX) == Math.abs(tempY)) {
            tempX = (float) tempX * rat;
            tempY = (float) tempY * rat;
        }
    
        accelX = tempX * (float) runSpeed;
        accelY = tempY * (float) runSpeed;
        speed = (float) Math.sqrt(speedX * speedX + speedY * speedY);
        nextSpeedX = speedX + accelX;
        nextSpeedY = speedY + accelY;
        nextSpeed = (float) Math.sqrt(nextSpeedX * nextSpeedX + nextSpeedY * nextSpeedY);
    
        if (nextSpeed > maxSpeed) { //can't accelerate by running
            if (nextSpeed > speed) {  //wants to accelerate
                if (speed > maxSpeed) {  //the current speed is larger than maximum.
                    float diff = (float)(speed / nextSpeed);
                    float greenX = nextSpeedX*diff;
                    float greenY = nextSpeedY*diff;
                    accelX = greenX-speedX;
                    accelY = greenY-speedY;
                } else { //speed <= maxspeed
                    float diff = (float)(maxSpeed / nextSpeed);
                    float greenX = nextSpeedX*diff;
                    float greenY = nextSpeedY*diff;
                    accelX = greenX-speedX;
                    accelY = greenY-speedY;
                }
            } else { //wants to slow! allow it!
                //acceleration doesn't need to be changed
            }
        } else { //no problem, allow it!
            //acceleration doesn't need to be changed
        }
        speedX += accelX;
        speedY += accelY;
    }
    

    Could be shorter, could be a bit more optimised, but it works. I hope this’ll help anyone who stumbles upon this problem in the future.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am making a top down, rpg-type game - similar to Pokemon, but I
I'm making a game where all movement is grid based. I also wan't to
I'm making a 3D game in XNA using Ox Engine. There is very little
I am making a tile game. Was following http://www.raywenderlich.com/1163/how-to-make-a-tile-based-game-with-cocos2d . Using the whole screen
I'm making a fighting game in Java for a project and trying to get
Currently i'm making a breakout game, and to define objects' location or movement i'm
Hey guys on my 2D game the movement speed varies.. I was making my
I'm making a game which you can see here, if you are on Windows
Making my first steps with NHibernate, I'm trying to have it creating my Tables
Making my first steps in RIA Services (VS2010Beta2) and i encountered this problem: created

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.