// File: /workspace/github_c_makefile_star2_O1/FredTheDino[P]Fog/src/engine/logic/block_physics.cpp
// Function: Physics::solve(Physics::Overlap)
void solve(Overlap overlap)
{
    if (!overlap) {
        return;
    }

    Body *a = overlap.a;
    Body *b = overlap.b;

    f32 total_mass = 1.0f / (a->inverse_mass + b->inverse_mass);

    // Position
    Vec2 relative_position = (a->position + a->offset) - (b->position + b->offset);
    Vec2 normal = -overlap.normal * (f32) SIGN(dot(relative_position, overlap.normal));
#if 1
    a->position -= overlap.normal * overlap.depth * total_mass * a->inverse_mass;
    b->position += overlap.normal * overlap.depth * total_mass * b->inverse_mass;
#endif

    // Don't do velocity correct unless you have to.
    Vec2 relative_velocity = a->velocity - b->velocity;
    f32 normal_velocity = dot(normal, relative_velocity);
    if (normal_velocity < 0)
        return;

    // Normal
    f32 bounce = MAX(a->bounce, b->bounce);
    f32 normal_force = normal_velocity * total_mass * (1.0f + bounce);
    a->velocity -= normal * normal_force * a->inverse_mass;
    b->velocity += normal * normal_force * b->inverse_mass;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
cmpb $0x0, 0x2c(%rbp)
je 0x26180
leaq 0x10(%rbp), %rdx
movq (%rdx), %rcx
movq 0x8(%rdx), %rax
movss 0x3c(%rcx), %xmm2
movss 0x3c(%rax), %xmm0
addss %xmm2, %xmm0
movss 0xa028(%rip), %xmm5     # 0x30004
movsd 0x8(%rcx), %xmm1
movss 0x14(%rcx), %xmm3
movaps %xmm1, %xmm6
addss %xmm3, %xmm6
movss 0x18(%rcx), %xmm4
shufps $0x55, %xmm1, %xmm1     # xmm1 = xmm1[1,1,1,1]
addss %xmm4, %xmm1
movsd 0x8(%rax), %xmm7
movss 0x14(%rax), %xmm8
addss %xmm7, %xmm8
subss %xmm8, %xmm6
shufps $0x55, %xmm7, %xmm7     # xmm7 = xmm7[1,1,1,1]
addss 0x18(%rax), %xmm7
subss %xmm7, %xmm1
movsd 0x14(%rdx), %xmm7
mulss %xmm7, %xmm6
shufps $0x55, %xmm7, %xmm7     # xmm7 = xmm7[1,1,1,1]
mulss %xmm1, %xmm7
addss %xmm7, %xmm6
xorps %xmm10, %xmm10
cmpltss %xmm6, %xmm10
andps %xmm5, %xmm10
divss %xmm0, %xmm5
movss 0x14(%rdx), %xmm8
movaps 0x9fef(%rip), %xmm11    # 0x30040
movaps %xmm8, %xmm0
xorps %xmm11, %xmm0
movss 0x18(%rdx), %xmm9
xorps %xmm9, %xmm11
xorps %xmm7, %xmm7
cmpltss %xmm7, %xmm6
movss 0xa1b5(%rip), %xmm1     # 0x30228
andps %xmm6, %xmm1
andnps %xmm10, %xmm6
orps %xmm6, %xmm1
mulss %xmm1, %xmm0
mulss %xmm11, %xmm1
movss 0x10(%rdx), %xmm6
mulss %xmm6, %xmm8
mulss %xmm6, %xmm9
mulss %xmm5, %xmm8
mulss %xmm5, %xmm9
movaps %xmm2, %xmm6
mulss %xmm8, %xmm6
mulss %xmm9, %xmm2
subss %xmm6, %xmm3
movss %xmm3, 0x14(%rcx)
subss %xmm2, %xmm4
movss %xmm4, 0x18(%rcx)
movss 0x3c(%rax), %xmm2
mulss %xmm2, %xmm8
mulss %xmm2, %xmm9
addss 0x14(%rax), %xmm8
movss %xmm8, 0x14(%rax)
addss 0x18(%rax), %xmm9
movss %xmm9, 0x18(%rax)
movsd 0x24(%rax), %xmm6
movss 0x24(%rcx), %xmm3
movss 0x28(%rcx), %xmm2
movaps %xmm3, %xmm4
subss %xmm6, %xmm4
shufps $0x55, %xmm6, %xmm6     # xmm6 = xmm6[1,1,1,1]
movaps %xmm2, %xmm8
subss %xmm6, %xmm8
mulss %xmm1, %xmm8
mulss %xmm0, %xmm4
addss %xmm8, %xmm4
ucomiss %xmm4, %xmm7
ja 0x26180
movss 0x3c(%rcx), %xmm6
movss 0x44(%rcx), %xmm7
maxss 0x44(%rax), %xmm7
addss 0x9ed2(%rip), %xmm7     # 0x30004
mulss %xmm4, %xmm5
mulss %xmm5, %xmm7
mulss %xmm7, %xmm0
mulss %xmm7, %xmm1
movaps %xmm6, %xmm4
mulss %xmm0, %xmm4
mulss %xmm1, %xmm6
subss %xmm4, %xmm3
movss %xmm3, 0x24(%rcx)
subss %xmm6, %xmm2
movss %xmm2, 0x28(%rcx)
movss 0x3c(%rax), %xmm2
mulss %xmm2, %xmm0
mulss %xmm2, %xmm1
addss 0x24(%rax), %xmm0
movss %xmm0, 0x24(%rax)
addss 0x28(%rax), %xmm1
movss %xmm1, 0x28(%rax)
popq %rbp
retq