# Update movement actor with collisions in 2D game

I have the algorithm for moving an actor. The actor has:

1. 4 states: Stay, Move, Jump, Fall and direction vector x,y.
2. The direction vector can be -1.f and 1.f or 0.f in both directions.
3. Boolean value onGround.
4. currentJumpVelocity
5. startJumpVelocity

Algorithm:

if (direction.x != 0.f) {
// move X
// check collision X by direction = true
//// move to difference
}

if (onGround == true) {
// check collision down == false
//// state = state == State::Jump ? State::Jump : State::Fall
//// direction.y = state == State::Jump ? -1.f : 1.f
//// currentJumpVelocoity -= state == State::Jump ? deltaTime*startJumpVelocity : currentJumpVelocity
//// onGround = false
}

if (state == State::Jump) {
// doJump -> when press Space currentJumpVelocity sets from startJumpVelocity
// check collision up
//// move to difference
//// state = Fall
//// direction.y = 1.f
//currentJumpVelocity -= state == State::Jump ? deltaTime*startJumpVelocity : currentJumpVelocity
}
else if (state == State::Fall) {
// doFall
// check collision down
//// move to difference
//// state = direction.x != 0.f ? State::Move : State::Stay
//// onGround = true;
//// direction.y = 0.f
//// currentJumpVelocity = 0.f
}


Is this correct algorithm?

• "Is this correct?" You tell us! When you implement this, does it give the results you want? If so, it would appear to be correct for your purposes. If not, then tell us in exactly what way the behaviour differs from the behaviour that you want, and we can suggest ways you can fix that specific problem. – DMGregory May 14 '19 at 11:51
• @DMGregory, I want to know is execution order of algorithm correct. – Dark Hell May 14 '19 at 12:00
• Correctness is determined by whether it gives the correct results. You can test your own game without waiting for Internet strangers to do it for you, which will get you more complete and trustworthy answers, faster. Once you've done your own testing, if you identify a problem that you don't know how to fix, that's the kind of issue we can help you with here. – DMGregory May 14 '19 at 12:03
• @DMGregory, thanks. I will trying. In all of my algorithms i had problems with jumps. Everytime onGround state was false and I couldn't jump. – Dark Hell May 14 '19 at 12:14