2
\$\begingroup\$

I am trying to implement acceleration and drag into my top-down 2D game. The player can move using the WASD keys. Acceleration works fine, but the way I handle the directional velocity causes the player to abruptly stop when the movement keys are released instead of coming to a stop. Here is the code I currently have:

EDIT: I officially solved the above issue by implementing an applyForce method and triggering it when the movement keys are pressed as seen here:

    public void applyForce(float angle, float amount)
    {
        Vector2 v = new Vector2((float)(Math.Cos(angle)), (float)(Math.Sin(angle)));
        v.Normalize();
        v = Vector2.Multiply(v, amount);

        velocity = Vector2.Add(velocity, v);
    }

            if (oldKeys.IsKeyDown(Keys.A))
            {
                applyForce((float)Math.PI, CurrentSpeed);
            }
            if (oldKeys.IsKeyDown(Keys.D))
            {
                applyForce(0, CurrentSpeed);
            }
            if (oldKeys.IsKeyDown(Keys.W))
            {
                applyForce((float)Math.PI * 1.5f, CurrentSpeed);
            }
            if (oldKeys.IsKeyDown(Keys.S))
            {
                applyForce((float)Math.PI * 0.5f, CurrentSpeed);
            }

As you may have guessed, the new problem is that when 2 keys are pressed the method is called twice causing the player to move faster than he should be. Are there any effective ways of handling this?

\$\endgroup\$
  • \$\begingroup\$ Someone posted an answer and it wouldn't let me comment on it. I hope I didn't accidentally delete it because it has disappeared. \$\endgroup\$ – Bagofsheep Apr 28 '14 at 4:20
1
\$\begingroup\$

Someone answered the question earlier then either deleted it or I accidentally deleted it (and if so I apologize). Regardless, they pointed out that it was odd that my applyForce method was based on an angle so I fixed that, then used a modified version of their idea to fix my problem.

Here are the two new overloaded versions of applyForce:

    public void applyForce(Vector2 direction, float amount)
    {
        if (direction.Length() > 0)
            direction.Normalize();

        direction = Vector2.Multiply(direction, amount);

        velocity = Vector2.Add(velocity, direction);
    }

    public void applyForce(Vector2 v)
    {
        velocity = Vector2.Add(velocity, v);
    }

And the new movement key code:

            int x = 0, y = 0;

            if (oldKeys.IsKeyDown(Keys.A))
                x = -1;
            if (oldKeys.IsKeyDown(Keys.D))
                x = 1;
            if (oldKeys.IsKeyDown(Keys.W))
                y = -1;
            if (oldKeys.IsKeyDown(Keys.S))
                y = 1;
            if (oldKeys.IsKeyUp(Keys.A) && oldKeys.IsKeyUp(Keys.D))
                x = 0;
            if (oldKeys.IsKeyUp(Keys.W) && oldKeys.IsKeyUp(Keys.S))
                y = 0;

            Vector2 mv = new Vector2(x, y);
            applyForce(mv, CurrentSpeed);
\$\endgroup\$
  • \$\begingroup\$ Stated differently (ie. how I was about to answer): don't applyForce for every key, instead set the direction. Then after all keys call applyForce once with the set direction. \$\endgroup\$ – jhocking Apr 28 '14 at 11:39
  • \$\begingroup\$ Were you the person who answered earlier then deleted the answer? \$\endgroup\$ – Bagofsheep Apr 28 '14 at 17:50
  • \$\begingroup\$ no, was just summarizing your correct answer for other newbies \$\endgroup\$ – jhocking Apr 28 '14 at 18:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.