The following code for resolving collisions between two objects assumes a resitution of zero. The `inverse_inertia` of a body is represented as a matrix (`glm::mat4`).

```
void apply_impulse(Body& body, glm::vec3 impulse, glm::vec3 offset)
{
    body.velocity += impulse * body.inverse_mass;
    body.angular_velocity += body.inverse_inertia * glm::cross(offset, impulse);
}

void resolve_collision(Body& a, Body& b, glm::vec3 contact_point, glm::vec3 normal)
{
    glm::vec3 ra = contact_point - a.position;
    glm::vec3 rb = contact_point - b.position;

    glm::vec3 relative_velocity =
        b.velocity + glm::cross(b.angular_velocity, rb) -
        a.velocity - glm::cross(a.angular_velocity, ra);

    // Not moving towards each other, ignore the collision as it will be resolved anyway
    if (glm::dot(relative_velocity, normal) > 0) {
        return;
    }

    normal = glm::normalize(normal);

    float inverse_mass_sum = a.inverse_mass + b.inverse_mass +
        glm::length2(a.inverse_inertia * glm::cross(ra, normal)) +
        glm::length2(b.inverse_inertia * glm::cross(rb, normal));

    float normal_impulse = -glm::dot(relative_velocity, normal) / inverse_mass_sum;

    apply_impulse(a, -normal_impulse * normal, ra);
    apply_impulse(b, normal_impulse * normal, rb);

    // Recalculate after normal impulse
    relative_velocity =
        b.velocity + glm::cross(b.angular_velocity, rb) -
        a.velocity - glm::cross(a.angular_velocity, ra);
    glm::vec3 relative_momentum = relative_velocity / inverse_mass_sum;

    // Apply friction
    glm::vec3 friction_impulse;
    if (glm::length2(relative_momentum) < glm::length2(normal_impulse * static_friction)) {
        friction_impulse = -relative_momentum;
    }
    else {
        friction_impulse = -normal_impulse * glm::normalize(relative_momentum) * dynamic_friction;
    }
    apply_impulse(a, -friction_impulse, ra);
    apply_impulse(b, friction_impulse, rb);
}

```

When I run this code, it works fine on lower values like 1, 2 or 3 of `static_friction` and `dynamic_friction`, even though it looks like it slides a bit. But when I raise them both to, say, 9999, it overreacts instantly, flinging away the objects at extreme speeds. It shouldn't do that since the last `if` statement of `resolve_collision` should serve to "clamp" the friction impulse to on that makes the relative velocity exactly zero. But it doesn't seem to do that.

When setting the frictions to around 10-20, the linear and angular velocities of the ball seem to jiggle back and forth.

What am I doing wrong here? The section of the code that I think is most likely to be wrong is:

```
float inverse_mass_sum = a.inverse_mass + b.inverse_mass +
    glm::length2(a.inverse_inertia * glm::cross(ra, normal)) +
    glm::length2(b.inverse_inertia * glm::cross(rb, normal));

```

But I have no idea how this it is wrong. I want to add that I could do this with only one moving object:

```
for (auto& platform : platforms) {
    glm::vec3 contact_point;
    if (intersects(platform, ball, contact_point)) {
        // Only react if moving towards the platform
        if (glm::dot(ball.position - contact_point, ball.velocity) > 0) {
            continue;
        }

        // Collision with the platform applies a normal impulse
        glm::vec3 normal = glm::normalize(ball.position - contact_point);
        float normal_impulse = -ball.mass * glm::dot(normal, ball.velocity);
        ball.velocity += normal_impulse * normal;

        // Apply friction
        glm::vec3 relative_momentum = ball.mass * ball.velocity +
            inertia(ball) * glm::cross(ball.position - contact_point, ball.angular_velocity);
        apply_impulse(
            ball,
            friction_impulse(relative_momentum, normal_impulse),
            contact_point - ball.position);
    }
}

```

With `friction_impulse` being defined as:

```
glm::vec3 friction_impulse(glm::vec3 relative_momentum, float normal_impulse)
{
    if (glm::length2(relative_momentum) < glm::length2(normal_impulse * static_friction)) {
        return -relative_momentum;
    }
    else {
        return -normal_impulse * glm::normalize(relative_momentum) * dynamic_friction;
    }
}

```

Feel free to move this to gamedev or whatever fits best, I am not sure where to put it.