Friday, March 30, 2018

2d - How to correctly implement player acceleration caused by player input


In my game I implement movement in the Player class like so:


// called from Player's update() method, which is called from the gameloop.
void move(){
velocity.x += acceleration.x;
velocity.y += acceleration.y;
position.x += velocity.x;

position.y += velocity.y;
}

Keyboard input affects the acceleration directly, for example:


// in Player, called from main gameloop.
void update(){
if(playerInput.rightPressed()) (acceleration.x += 2);
// .. etc
move();
}


I think so far this is standard (if not, please say so).


Now my question:


During movement invoked by player input, I want the acceleration to be constant. So when the player presses the right arrow key, the acceleration.x will always be 2 - and not 2, and then 4, and then 6, etc.


I could easily acheive this be having if(playerInput.rightPressed())(acceleration.x = 2), but then other forces acting on the entity will be ignored.


What is the standard way to implement this?




No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...