From 3ec46b6bbf3dc2f2e0ba4cbf32206129247f9cf3 Mon Sep 17 00:00:00 2001 From: Quinn Date: Sun, 26 Dec 2021 21:36:54 -0800 Subject: [PATCH] Fix rewind step calculation --- ball.lua | 2 +- main.lua | 13 ++++++------- qphys.p8 | 15 +++++++-------- 3 files changed, 14 insertions(+), 16 deletions(-) diff --git a/ball.lua b/ball.lua index 59cfd77..9ca9e57 100644 --- a/ball.lua +++ b/ball.lua @@ -49,7 +49,7 @@ function ball:new(o) ball.__count += 1 local o = o or {} o.id = ball.__count - o.col = o.id % 16 + o.col = o.col or o.id % 16 setmetatable(o, self) o.__index = self return o diff --git a/main.lua b/main.lua index 0bd3eef..6e61318 100644 --- a/main.lua +++ b/main.lua @@ -1,5 +1,5 @@ -ball_count = 16 -ball_radius = 5 +ball_count = 10 +max_radius = 8 function _init() balls = {} @@ -8,9 +8,8 @@ function _init() add(balls, ball:new({ pos=vec2:new(rnd(127),rnd(127)), vel=vec2:new(rnd(2)-1,rnd(2)-1):normal(), - rad=ball_radius, - col=rnd(15)+1 - })) + rad=2+flr(rnd(max_radius)), + })) end end @@ -43,10 +42,10 @@ function handle_collisions() local t_xpoint = target.pos repeat steps += 1 - rewind += 0.1 * steps + rewind = 0.1 * steps s_xpoint = source.pos - source.vel * rewind t_xpoint = target.pos - target.vel * rewind - until (s_xpoint - t_xpoint):mag() >= source.rad + target.rad + 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) source.pos = s_xpoint + source.vel * rewind diff --git a/qphys.p8 b/qphys.p8 index 50a6cd6..7580307 100644 --- a/qphys.p8 +++ b/qphys.p8 @@ -106,15 +106,15 @@ function ball:new(o) ball.__count += 1 local o = o or {} o.id = ball.__count - o.col = o.id % 16 + o.col = o.col or o.id % 16 setmetatable(o, self) o.__index = self return o end -->8 -ball_count = 16 -ball_radius = 5 +ball_count = 10 +max_radius = 8 function _init() balls = {} @@ -123,9 +123,8 @@ function _init() add(balls, ball:new({ pos=vec2:new(rnd(127),rnd(127)), vel=vec2:new(rnd(2)-1,rnd(2)-1):normal(), - rad=ball_radius, - col=rnd(15)+1 - })) + rad=2+flr(rnd(max_radius)), + })) end end @@ -158,10 +157,10 @@ function handle_collisions() local t_xpoint = target.pos repeat steps += 1 - rewind += 0.1 * steps + rewind = 0.1 * steps s_xpoint = source.pos - source.vel * rewind t_xpoint = target.pos - target.vel * rewind - until (s_xpoint - t_xpoint):mag() >= source.rad + target.rad + 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) source.pos = s_xpoint + source.vel * rewind