reworked constraint solver to work relative to the overlap point

This commit is contained in:
Sara 2023-10-24 23:49:59 +02:00
parent 635fb7b69f
commit f8462d8e2c

View file

@ -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;
}