Fix (hopefully) one way collision

scene_man
En Yi 2023-08-20 16:40:51 +08:00
parent 8dde1d5344
commit 62de96609b
1 changed files with 26 additions and 32 deletions

View File

@ -45,10 +45,11 @@ static bool check_collision_and_move(
// If there is collision, use previous overlap to determine direction // If there is collision, use previous overlap to determine direction
find_AABB_overlap(p_ct->prev_position, p_bbox->size, *other_pos, other_bbox, &prev_overlap); find_AABB_overlap(p_ct->prev_position, p_bbox->size, *other_pos, other_bbox, &prev_overlap);
// Store collision event here // Resolve collision via moving player by the overlap amount only if other is solid
// Check collision on x or y axis // also check for empty to prevent creating new collision. Not fool-proof, but good enough
// Encode key and value, -ve is left and up, +ve is right and down
Vector2 offset = {0, 0}; Vector2 offset = {0, 0};
if (other_solid == SOLID)
{
if (fabs(prev_overlap.y) > fabs(prev_overlap.x)) if (fabs(prev_overlap.y) > fabs(prev_overlap.x))
{ {
offset.x = overlap.x; offset.x = overlap.x;
@ -65,27 +66,20 @@ static bool check_collision_and_move(
{ {
offset.y = overlap.y; offset.y = overlap.y;
} }
}
// Resolve collision via moving player by the overlap amount only if other is solid else if (other_solid == ONE_WAY)
// also check for empty to prevent creating new collision. Not fool-proof, but good enough {
//if (other_solid && !check_collision_at(ent_idx, p_ct->position, sz, tilemap, offset, p_manager)) // One way collision is a bit special
if ( other_solid == SOLID if (
|| ( p_ct->prev_position.y + p_bbox->size.y - 1 < other_pos->y
other_solid == ONE_WAY && offset.y < 0 && p_ct->position.y + p_bbox->size.y - 1 >= other_pos->y
&& (p_ct->prev_position.y + p_bbox->size.y - 1 < other_pos->y))
) )
{ {
//if (!check_collision_offset(ent, p_ct->position, p_bbox->size, tilemap, offset)) offset.y = other_pos->y - (p_ct->position.y + p_bbox->size.y);
{ }
}
p_ct->position = Vector2Add(p_ct->position, offset); p_ct->position = Vector2Add(p_ct->position, offset);
} }
//else
{
//*other_pos = Vector2Subtract(*other_pos, offset);
}
}
}
else if (overlap_mode == 2) else if (overlap_mode == 2)
{ {
if ( other_solid != SOLID ) goto collision_end; if ( other_solid != SOLID ) goto collision_end;