For a personal project, I am attempting to create an environment for reinforcement learning algorithms. Specifically, I want the user (i.e., an artificial intelligent agent, but for intuition you can also think of a human game player) to control a 2-D "point" in some grid. The actions that the user can apply are NORTH, SOUTH, EAST, and WEST.

Pressing NORTH, for instance, should obviously move the particle north. But how do I ensure that if the user keeps applying the NORTH action, that the particle moves faster? And then if the user applies SOUTH, the particle shouldn't reverse immediately but should slow down a bit and then move down, depending on how long the user presses SOUTH. And then if the user presses EAST while the particle is moving south, then the particle should move south-east, and so on.

For the movement part, I am going to follow this tutorial which uses basic trigonometry to determine the resulting movement given the velocity in the x and y directions (see the section "Movement vectors").

However, I'm unsure on how to implement the acceleration, and the tutorial assumes we already have some velocity, but I need to define a reasonable mapping from NORTH, SOUTH, etc., actions to velocity.

I'm not a physicist, so I'm probably missing something obvious. If so, any possible supplemental references would be appreciated. And just to be clear, I'm not looking for a guide on particle collisions and the like; my interest is trying to simulate realistic movement from NORTH, SOUTH, EAST, and WEST user-provided actions.



You need to associate both velocity and position to your particle. Each frame, you update the position using its velocity. But you also update the velocity, based on the keys. At the lack of key presses, the particle will maintain its current velocity. (*)

void update( float dt )
  // accelerate and decelerate particle.
  float scl=1; // acceleration in units/sec/sec.
  if ( n ) vel.y += scl * dt;
  if ( s ) vel.y -= scl * dt;
  if ( e ) vel.x += scl * dt;
  if ( w ) vel.x -= scl * dt;

  // move particle with vel units/sec.
  pos.x = pos.x + vel.x * dt;
  pos.y = pos.y + vel.y * dt;

In physics terms:

  • velocity is the rate with which the position changes per second. (m/s)
  • acceleration is the rate with which the velocity changes per second. (m/s²)

(*) If you want the particle to gradually slow down during lack of key presses, you need to model friction.

  • \$\begingroup\$ Thanks. Do people refer to this as Euler's method or by another name? Not totally clear on the origin of this even after Googling (though I think it's Euler's method). \$\endgroup\$ – ComputerScientist Oct 16 '17 at 0:53
  • \$\begingroup\$ This is indeed Euler Integration. I wouldn't worry too much about the integration method. The code is simple, which is the important part. To get more accuracy, all you have to do is take smaller time-steps. Games rarely need more accuracy than you get with 1/60s or 1/120s time-steps. \$\endgroup\$ – Bram Oct 16 '17 at 3:14
  • \$\begingroup\$ Sure, I'll use this. One thing I might add is angular movement, so given velocity we can move the particle in a more "realistic" way, so instead of adding x and y independently, we change x and y in tandem using basic trigonometry. \$\endgroup\$ – ComputerScientist Oct 16 '17 at 15:18
  • \$\begingroup\$ Wait, never mind, the linked article is about determining x and y given speed and angle, but that's not my exact problem statement. Well then I'll just use the stuff from here then. Thanks. \$\endgroup\$ – ComputerScientist Oct 16 '17 at 15:43

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.