Physics engine: always resolve penetration, but apply impulse only on approach velocity
This commit is contained in:
parent
b2b6d18dd0
commit
a75b8710de
1 changed files with 54 additions and 61 deletions
|
|
@ -390,7 +390,7 @@ namespace psemek::phys2d
|
|||
|
||||
void apply_gravity(float dt);
|
||||
void integrate(float dt);
|
||||
bool resolve_collisions();
|
||||
void resolve_collisions();
|
||||
|
||||
void log_energy();
|
||||
float energy();
|
||||
|
|
@ -447,10 +447,8 @@ namespace psemek::phys2d
|
|||
}
|
||||
}
|
||||
|
||||
bool engine::impl::resolve_collisions()
|
||||
void engine::impl::resolve_collisions()
|
||||
{
|
||||
bool had_collision = false;
|
||||
|
||||
for (std::size_t gi = 0; gi < groups.size(); ++gi)
|
||||
{
|
||||
for (std::size_t gj = gi; gj < groups.size(); ++gj)
|
||||
|
|
@ -473,8 +471,6 @@ namespace psemek::phys2d
|
|||
|
||||
if (c->penetration == geom::vector<float, 2>::zero()) continue;
|
||||
|
||||
// TODO: handle inv_mass == 0
|
||||
|
||||
auto const n = geom::normalized(c->penetration);
|
||||
|
||||
auto const & infoi = groups[gi].infos[i];
|
||||
|
|
@ -491,10 +487,8 @@ namespace psemek::phys2d
|
|||
|
||||
auto const u = uj - ui;
|
||||
|
||||
if (geom::dot(u, n) > 0.f) continue;
|
||||
|
||||
had_collision = true;
|
||||
|
||||
if (geom::dot(u, n) < 0.f)
|
||||
{
|
||||
auto const & mati = materials[infoi.material];
|
||||
auto const & matj = materials[infoj.material];
|
||||
|
||||
|
|
@ -546,6 +540,7 @@ namespace psemek::phys2d
|
|||
|
||||
groups[gi].dynamic_states[i].angular_velocity -= geom::det(ri, J) * infoi.inv_inertia;
|
||||
groups[gj].dynamic_states[j].angular_velocity += geom::det(rj, J) * infoj.inv_inertia;
|
||||
}
|
||||
|
||||
groups[gi].static_states[i].position -= c->penetration * infoi.inv_mass / (infoi.inv_mass + infoj.inv_mass);
|
||||
groups[gj].static_states[j].position += c->penetration * infoj.inv_mass / (infoi.inv_mass + infoj.inv_mass);
|
||||
|
|
@ -553,8 +548,6 @@ namespace psemek::phys2d
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
return !had_collision;
|
||||
}
|
||||
|
||||
float engine::impl::energy()
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue