From f8462d8e2cd069939e52cfbc4a78013ab667e52e Mon Sep 17 00:00:00 2001
From: Sara <sara@saragerretsen.nl>
Date: Tue, 24 Oct 2023 23:49:59 +0200
Subject: [PATCH] reworked constraint solver to work relative to the overlap
 point

---
 src/physics_entity.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)

diff --git a/src/physics_entity.c b/src/physics_entity.c
index f02c7b5..0752109 100644
--- a/src/physics_entity.c
+++ b/src/physics_entity.c
@@ -41,16 +41,15 @@ Vector _internal_calculate_contact_force(RigidBody* self, Contact* contact) {
 
 static inline
 int _internal_default_contact_solver(RigidBody* body, Contact* contact, Transform pre_solve) {
-    // the desired position is anywhere the overlapping vertex is further along the normal than the contact point
     Collision hit = contact->hit;
     Transform* trans = rigidbody_get_transform(body);
     const Vector world_collision_point = vaddf(transform_point(&pre_solve, hit.point), hit.penetration_vector);
-    const float min_dot = vdotf(hit.normal, world_collision_point);
-    const float current_dot = vdotf(hit.normal, transform_point(trans, hit.point));
-    if(current_dot >= min_dot)
+    const float current_dot = vdotf(hit.normal, vsubf(transform_point(trans, hit.point), world_collision_point));
+    if(current_dot >= 0.0)
         return 1;
-    const Vector target = vaddf(trans->position, vmulff(hit.normal, min_dot - current_dot));
-    trans->position = vmovetowardsf(trans->position, target, 0.01f);
+    // the desired position is anywhere the overlapping vertex is further along the normal than the contact point
+    const Vector target = vaddf(trans->position, vmulff(hit.normal, -current_dot));
+    trans->position = vmovetowardsf(trans->position, target, 1.f);
     return 0;
 }