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:
- Collision while moving on the X axis
- Collision when jumping ( on the Y axis )
- 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.