|
|
|
@ -27,6 +27,11 @@ normalize :: (v: Vector2) -> Vector2 #must {
|
|
|
|
|
normalize(*v);
|
|
|
|
|
return v;
|
|
|
|
|
}
|
|
|
|
|
normalize_or_zero :: inline (using _v: Vector2) -> Vector2 #must {
|
|
|
|
|
v := _v;
|
|
|
|
|
normalize_or_zero(*v);
|
|
|
|
|
return v;
|
|
|
|
|
}
|
|
|
|
|
negative :: (v: Vector2) -> Vector2 #must {
|
|
|
|
|
return xy(-v.x, -v.y);
|
|
|
|
|
}
|
|
|
|
@ -111,8 +116,12 @@ apply_impulse_at_point :: (using r: *Rect, impulse: Vector2, point_world_space:
|
|
|
|
|
{
|
|
|
|
|
apply_force_at_point(r, impulse / TIMESTEP, point_world_space);
|
|
|
|
|
} else {
|
|
|
|
|
vel += impulse / r.mass;
|
|
|
|
|
angle_vel += length(impulse) * (cross(point_world_space - pos, normalize(impulse))/moment_of_inertia(r));
|
|
|
|
|
if !static
|
|
|
|
|
{
|
|
|
|
|
vel += impulse / r.mass;
|
|
|
|
|
angle_vel += length(impulse) * (cross(point_world_space - pos, normalize_or_zero(impulse))/moment_of_inertia(r));
|
|
|
|
|
if isnan(angle_vel) Debug.breakpoint();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// everything needed to resolve the collision
|
|
|
|
@ -134,7 +143,6 @@ perp :: (v: Vector2) -> Vector2
|
|
|
|
|
handle_collision :: (m: Manifold, dt: float) {
|
|
|
|
|
a := m.a;
|
|
|
|
|
b := m.b;
|
|
|
|
|
if m.count == 0 return;
|
|
|
|
|
for 0..m.count-1
|
|
|
|
|
{
|
|
|
|
|
total_momentum: float = length(a.vel) * a.mass + length(b.vel) * b.mass;
|
|
|
|
@ -142,6 +150,7 @@ handle_collision :: (m: Manifold, dt: float) {
|
|
|
|
|
//impulse_strength += m.depths[it] * 3.0;
|
|
|
|
|
impulse_strength += total_momentum / 2.0;
|
|
|
|
|
|
|
|
|
|
// the momentum would be unused if put into static
|
|
|
|
|
if a.static || b.static impulse_strength *= 2.0;
|
|
|
|
|
|
|
|
|
|
impulse := m.normal * impulse_strength;
|
|
|
|
|