Zero if normalize fail

main
Cameron Murphy Reikes 2 years ago
parent 7e53aec4dc
commit 6a07e27f2a

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

@ -8,9 +8,10 @@ SetWorkingDir, %A_ScriptDir%
^Esc::return
^b::
Runwait, build_physics.bat
SetTitleMatchMode, 2
WinActivate Visual
Send, {Shift down}{F5}{Shift up}
Runwait, build_physics.bat
Sleep 100
Send, {F5}
Send, {Ctrl down}{Shift down}{F5}{Ctrl up}{Shift up}

Loading…
Cancel
Save