0
\$\begingroup\$

I want to create more realistic physics for my 2D games.

Currently, the character and the other game objects in my games start moving instantly (achieve maximum speed instantly) and stop instantly (achieve 0 speed instantly).

There is no transition between speed levels - resting and moving, moving and resting.

I heard about 'steering behaviors', but am pretty sure they're mostly for designing AI agents' movements.

Should I learn steering behaviors to make more realistic character movements in general? (In the physics-aspect). Is this the way to go?

If the answer is 'yes', could you recommend a good source to learn them?

\$\endgroup\$
  • \$\begingroup\$ The simplest thing to do is probably to use interpolating functions such as the ones shown on easings.net (they don't have implementation code though) \$\endgroup\$ – amitp Mar 27 '14 at 0:23
  • \$\begingroup\$ You start by asking how to make smoother movement. Then answer your own question. Then ask if it's worth you learning and if it's a good choice. Finally you ask an open ended "where to learn" question. -1. \$\endgroup\$ – MichaelHouse Mar 27 '14 at 5:46
  • 1
    \$\begingroup\$ @Byte56 I don't think I understand why you say that the user is answering their own question. Steering is not acceleration, easing out and easing in. It has nothing to do with afaik with those things other than the fact they both have to do with motion. \$\endgroup\$ – wolfdawn Mar 27 '14 at 7:28
5
\$\begingroup\$

What you are asking about is called acceleration. Easing out and easing in. It is achieved by computing the elements current speed before you move it. The simplest way to do that if your goal is only smoothness in motion is to use acceleration.

You do this like this (pseudo code):

p.x = 10.0; // Current character position.
v.x = 0;
maxV.x = 1.0; // Inset different number here.
acc.x = 1.0;  // Insert different number if you like.

if (character_is_moving_in_one_direction_along_x-axis)
  {
    v.x += acc.x;
    if (v.x > maxV.x) v.x = maxV.x;
    if (v.x < -maxV.x) v.x = -maxV.x;
  }
p.x += v.x;
\$\endgroup\$
  • 1
    \$\begingroup\$ And just to add, to make characters slow down on their own, you just have to add friction at some point: acc.x *= 0.99;. This way your character will lose some momentum the moment you no longer accelerate (in this case 1 % per update). \$\endgroup\$ – Mario Mar 27 '14 at 10:20
  • \$\begingroup\$ Thanks for your answer. I'm thinking of how to integrate this in my game, please say if this is a good way to go: When the user presses one of the arrow keys, a flag is set (either 'left', 'right', 'up' or down). During the update() function of the character (happens every cycle), the flags are checked. Depending on the flag, a function like the one you described is called. These functions are part of the 'GameObject' class, and any game object uses them to move, according to the direction it is told to move currently. Is this a good solution? \$\endgroup\$ – Aviv Cohn Mar 27 '14 at 12:11
  • \$\begingroup\$ It makes sense. If you run into trouble (which you probably won't), you may select a concise piece of code and ask about it. In general it is important to remember to slow down the characters when they are on the ground and not moving towards a particular direction. Remember that. if (not_moving_to_any_particular_direction && on_ground) slow_down_vx by 2 * acc.x. Or simply do something like @Mario suggested (only possibly checking if the abs(speed) <= low_speed then speed = 0.0) \$\endgroup\$ – wolfdawn Mar 27 '14 at 12:49
  • \$\begingroup\$ I got an idea about this: As I said, pressing the arrow keys sets flags to true, and releasing keys sets the flags to false. During the update() function, the appropriate functions are called (up==true means jump(), left==true means left(), right==false or left==false means stopMoving() [which will gradually stop the movement, with the technique you described], etc.) I will deal with what you just described like so: When left or right are false, as I said stopMoving() is called, but it will include conditionals to check if character is on the ground or not \$\endgroup\$ – Aviv Cohn Mar 27 '14 at 13:02
  • 1
    \$\begingroup\$ right==false and left==false not or. :) \$\endgroup\$ – wolfdawn Mar 27 '14 at 13:03

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.