Shouldn't this line
| if (player_moved_down) {
final_position.y = ent_rect_pos.y + local_player->collision.dimentions.h - local_player->collision.position.y;
}
|
be
| if (player_moved_down) {
final_position.y = ent_rect_pos.y + entity->collision.dimentions.h - local_player->collision.position.y;
}
|
And on those lines
| if (player_moved_left) {
final_position.x = ent_rect_pos.x + entity->collision.dimentions.w;
}
if (player_moved_right) {
final_position.x = ent_rect_pos.x - local_player->collision.dimentions.w;
}
|
don't you need to account for a local_player->collision.position.x that isn't zero ?
I don't think this would solve the problem though.
The issue seems to be that you detect that you are overlapping col_result_x and col_result_y "units" but are resolving the collision using other values (the values in the ifs (player_moved_x) ). For example, the first glitch in your video: the player is up, and going down left near the left side of the other entity. You detect that you are colliding and that x is the smallest overlap. So you want to correct x, but instead of moving the player using col_result_x (the actual overlap), you move it to the other side (final_position.x = ent_rect_pos.x + entity->collision.dimentions.w;) because he was moving left.
A few things that could help debug this king of bug:
- have the camera fixed instead of following the character to better see what is actually happening;
- since this bug require user input and you can't really put a breakpoint to debug when it's happening, you can use a log to print values and the code path that was taken. Or having a replay input system (like in handmade hero).
A side note: when posting a question like this one, try to give more context about your code. For example, explain what the position of the player represent. Is it the bottom left corner ? Is it the bottom center ? Is it the center ? Also try to describe the problem more precisely: what is happening that isn't what you want. And if you can, provide a simple reproduction code we can compile and debug.