From 44380701705b1aba2f84c23f7bf9732e70e30f2f Mon Sep 17 00:00:00 2001 From: Sara Date: Wed, 1 Nov 2023 22:59:48 +0100 Subject: [PATCH] reworked _internal_collision_overlap to be more verbose --- src/collision.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/collision.c b/src/collision.c index 8bcaec2..372f8f6 100644 --- a/src/collision.c +++ b/src/collision.c @@ -62,7 +62,7 @@ int _internal_collision_get_overlap(PhysicsEntity self, PhysicsEntity other, Col // the shortest distance to solve collision found so far Vector shortest_escape = InfinityVector; // the squared length of the shortest escape vector found so far - float shortest_sqrmag = INFINITY; + float shortest_dot = INFINITY; // the first index of the points on the edge size_t shortest_escape_edge = 0; // the number of points in the shape of self @@ -72,20 +72,20 @@ int _internal_collision_get_overlap(PhysicsEntity self, PhysicsEntity other, Col // the next point on the line size_t next_index = (point_index + 1) % self_point_count; // get the two points defining the collision edge - Vector a = shape_get_point_transformed(self.tc->get_shape(self.data), point_index, *self_transform); - Vector b = shape_get_point_transformed(self.tc->get_shape(self.data), next_index, *self_transform); + Vector edge_lhs = shape_get_point_transformed(self.tc->get_shape(self.data), point_index, *self_transform); + Vector edge_rhs = shape_get_point_transformed(self.tc->get_shape(self.data), next_index, *self_transform); // the direction of the line - Vector normal = vperpendicularf(vsubf(b, a)); + Vector normal = vnormalizedf(vperpendicularf(vsubf(edge_rhs, edge_lhs))); Vector overlap_point; // the smallest escape vector on this axis - Vector escape = _internal_collision_overlap_on_axis(self, other, vnormalizedf(normal), &overlap_point); - float sqr_mag = vsqrmagnitudef(escape); - if(sqr_mag == 0) { + Vector escape = _internal_collision_overlap_on_axis(self, other, normal, &overlap_point); + float dot = vdotf(vinvf(normal), escape); + if(dot <= 0.0) { return 0; } - if(sqr_mag < shortest_sqrmag) { - shortest_sqrmag = sqr_mag; + if(dot <= shortest_dot) { + shortest_dot = dot; shortest_escape = escape; shortest_escape_edge = point_index; }