I am developing a kind of vertical endless running game, where the character has 4 possible lanes to be in. I am using libGDX and want the character to change lanes by adding a horizontal velocity to a Vector2. The position will be upgraded by velocity increments and not by simply changing the position to the next lane. So far I have a working function, but as I increment the horizontal velocity, along the run, it loses accuracy. Let's assume each lane is 50px width.
I am also using a acceleration vector to slow down as the character gets closer to the next lane.
The 2 functions below are about what happens when the screen is touched right or left. The position verification is a boundary condition to avoid the character going off the screen.
public void onClickRight(){
if(!(position.x>=50*2.75)&&isAlive){
velocity.x=350+extraSlideSpeed;
acceleration.x=-500;
}
}
public void onClickLeft(){
if(!(position.x<=50*0.75)&&isAlive){
velocity.x=-350-extraSlideSpeed;
acceleration.x=500;
}
}
The function below is called about 60 times per second, as delta is a very small number and represents time.
public void updateRunning(float delta){
velocity.add(acceleration.cpy().scl(delta));
position.add(velocity.cpy().scl(delta));
displacement.add(velocity.cpy().scl(delta));
if(displacement.x>=50||displacement.x<=-50){
velocity.x = 0;
acceleration.x = 0;
displacement.x = 0;
}
}
I am looking for smarter options as I am not pleased with my own. Help would be appreciated!