Fix rewind step calculation

This commit is contained in:
Quinn
2021-12-26 21:36:54 -08:00
parent 23104ec1f1
commit 3ec46b6bbf
3 changed files with 14 additions and 16 deletions
+6 -7
View File
@@ -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