# Game movement in 2D SFML game

I am making simple 2D game, imagine it as 2D Minecraft where you can dig blocks and build other things.

Currently I am struggling with movement. I have none experience in game development, but I am quite familliar with C++, that is why I am using SFML.

What situations I am considering in movement:

1. Collision while moving on the X axis
2. Collision when jumping ( on the Y axis )
3. Fall down if there is no block under

My player sprite is 64x32 (h x w) and block is 32 x 32. I store blocks as vector<vector<unique_ptr<Block>>> so I can calculate the index of the block as x_pos DIV 32, y_pos DIV 32

This is how I currently check if there is block ( X axis ):

new_position = actual_pos + velocity; // calculate new position
face_level = new_position.y DIV 32; // level 0 is the ground
feet_level = face_level + 1; // +1 is good, cause we increase level by going down
x_level = new_position.x DIV 32;
if (block_exists[feet_level][x_level] || block_exists[face_level][x_level]) {
return Collision::Block_Collision;
}


and then check

collision = check_collision();
if ( collision == Collision::Block_Collision ) {
// X axis
stop_moving();
}
if ( collision == Collision::No_Block_Under ) {
// falling
fall_down;
}
if ( collision == Collision::Block_Above ) {
// Y axis, actually do nothing, "dont jump"
}


If going left, I consider left border of the player. If going right, I consider right border of the player.

This was the easiest part.

What position should I use for the falling part? Should it be the middle of the player, or left/right border combined ? If I used the middle position, then player would fall into the block if he was standing on the left/right half.

Question is, what would be the best approach generally to solve these 3 situations.

When going left/right, you use the whole left/right border (aka collider/detector) to check if player collides. If going up/down, you should use the whole top/bottom border of the player to check the collision. This means you have to check for all blocks that detector overlaps.

This will complicate your collision detection, but doing it right will spare you from nasty bugs like player falling into the terrain, of phasing through the obstacles.

your character should only fall if there is nothing underneath him as in he shouldn't be colliding with any block underneath him from the left extent of his bounding rectangle to the right extent of the bounding rectangle.

if you have moving objects that you need to consider the relative velocities of each or of one to the other.

Easiest to perform is a sweep test where you take the bounding rectangle and drag it and the resulting area is what you test against the other object.

So basically it resolves to polygonal collision detection.