From 936c8fe40f85f627da662f6818bcd665af15964d Mon Sep 17 00:00:00 2001 From: Quinn Date: Sun, 26 Dec 2021 21:49:44 -0800 Subject: [PATCH] Add mass to collision calculation --- ball.lua | 4 ++++ main.lua | 12 +++++++----- qphys.p8 | 16 +++++++++++----- 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/ball.lua b/ball.lua index 9ca9e57..8638a49 100644 --- a/ball.lua +++ b/ball.lua @@ -5,6 +5,10 @@ ball = { vel = vec2:new(0,0), rad = 1, col = 7, + + mass = function() + return rad + end, draw = function(self) circfill(self.pos.x, self.pos.y, self.rad, self.col) diff --git a/main.lua b/main.lua index 6e61318..7211b75 100644 --- a/main.lua +++ b/main.lua @@ -45,9 +45,9 @@ function handle_collisions() rewind = 0.1 * steps s_xpoint = source.pos - source.vel * rewind t_xpoint = target.pos - target.vel * rewind - until rewind > 1 or (s_xpoint - t_xpoint):mag() >= source.rad + target.rad - local s_newvel = collision_vel(s_xpoint, source.vel, t_xpoint, target.vel) - local t_newvel = collision_vel(t_xpoint, target.vel, s_xpoint, source.vel) + until rewind > 1 or (s_xpoint - t_xpoint):mag() >= source.rad + target.rad + local s_newvel = collision_vel(s_xpoint, source.vel, t_xpoint, target.vel, source.mass(), target.mass()) + local t_newvel = collision_vel(t_xpoint, target.vel, s_xpoint, source.vel, target.mass(), source.mass()) source.pos = s_xpoint + source.vel * rewind target.pos = t_xpoint + target.vel * rewind source.vel = s_newvel @@ -56,8 +56,10 @@ function handle_collisions() end end -function collision_vel(x1,v1,x2,v2) +function collision_vel(x1,v1,x2,v2,m1,m2) + m1 = m1 or 1 + m2 = m2 or 1 local x_diff = x1 - x2 local v_diff = v1 - v2 - return v1 - x_diff * v_diff:dot(x_diff) / x_diff:mag()^2 + return v1 - x_diff * (2 * m2 / (m1 + m2)) * v_diff:dot(x_diff) / x_diff:mag()^2 end diff --git a/qphys.p8 b/qphys.p8 index 7580307..554f96b 100644 --- a/qphys.p8 +++ b/qphys.p8 @@ -62,6 +62,10 @@ ball = { vel = vec2:new(0,0), rad = 1, col = 7, + + mass = function() + return rad + end, draw = function(self) circfill(self.pos.x, self.pos.y, self.rad, self.col) @@ -160,9 +164,9 @@ function handle_collisions() rewind = 0.1 * steps s_xpoint = source.pos - source.vel * rewind t_xpoint = target.pos - target.vel * rewind - until rewind > 1 or (s_xpoint - t_xpoint):mag() >= source.rad + target.rad - local s_newvel = collision_vel(s_xpoint, source.vel, t_xpoint, target.vel) - local t_newvel = collision_vel(t_xpoint, target.vel, s_xpoint, source.vel) + until rewind > 1 or (s_xpoint - t_xpoint):mag() >= source.rad + target.rad + local s_newvel = collision_vel(s_xpoint, source.vel, t_xpoint, target.vel, source.mass(), target.mass()) + local t_newvel = collision_vel(t_xpoint, target.vel, s_xpoint, source.vel, target.mass(), source.mass()) source.pos = s_xpoint + source.vel * rewind target.pos = t_xpoint + target.vel * rewind source.vel = s_newvel @@ -171,10 +175,12 @@ function handle_collisions() end end -function collision_vel(x1,v1,x2,v2) +function collision_vel(x1,v1,x2,v2,m1,m2) + m1 = m1 or 1 + m2 = m2 or 1 local x_diff = x1 - x2 local v_diff = v1 - v2 - return v1 - x_diff * v_diff:dot(x_diff) / x_diff:mag()^2 + return v1 - x_diff * (2 * m2 / (m1 + m2)) * v_diff:dot(x_diff) / x_diff:mag()^2 end __gfx__