# Switching lanes in an endless runner

Some of you probably know the (once) popular mobile games Temple Run, Minion Rush or Subway Surfers. They all have the similarity of a lane system, which allows you to switch your position with a simple swipe to the left/right.

I am creating a game that also consists of three lanes. The only difference is your general movement going from left to right, thus making the lanes on top of each other.

As LibGDX is my preferred 2D games framework, I want to accomplish this task with mostly built-in functionality. Currently, I have two approaches, which both have flaws and resolving any of them would be a major help to me.

1) Using Actions. I could add an moveTo(upperLane.x, upperLane.y, 0.5f) if the player is in the middle, same goes for the lowerLane. But what, if he double swipes down, while being at the top? I would need so build a sequence, after the first actions started, or cancel the current action and start a new, but how to calculate the appropriate time for that large movement, while still "hanging between" two lines?

2) Implement impulses, that are able to add up in positive (up) and negative (down) directions. This would make it possible to move two steps at once, while having an appropriate time and also to move back up, while being in between two lanes, on your way down. Everything about impulses is plain speculation, as I have no further knowledge and don't want to use Box2D only for this purpose.

3) Feel free to give better suggestions! Special thanks to whom can point out the mechanic of one of the above mentioned games, which implement this feature flawlessly!

Number 1 should have a pretty simple fix (pseudocode):

// destination and position are Vector2s
float TRANSITION_SPEED = 0.5f; // How long it takes to move between lanes
float DISTANCE_BETWEEN_LANES = ...; // The distance between two lanes

float distanceToDestination = (destination - position).length();
float percentDistance = distanceToDestination / DISTANCE_BETWEEN_LANES;
float time = percentDistance * TRANSITION_SPEED;
removeAllActions();
moveTo(destination.x, destination.y, time);


This will first remove all actions of the Actor so the different actions don't disturb with each other, then calculate the percent distance of the current position position to the destination lane destination. So if you have traveled 40% of the distance percentDistance would be 0.6 which means it would take 0.6 * 0.5 = 0.3 seconds to travel the remaining distance.

• This is perfect. That's the way to go for me! Commented May 31, 2018 at 7:10