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
+1 -1
View File
@@ -49,7 +49,7 @@ function ball:new(o)
ball.__count += 1 ball.__count += 1
local o = o or {} local o = o or {}
o.id = ball.__count o.id = ball.__count
o.col = o.id % 16 o.col = o.col or o.id % 16
setmetatable(o, self) setmetatable(o, self)
o.__index = self o.__index = self
return o return o
+6 -7
View File
@@ -1,5 +1,5 @@
ball_count = 16 ball_count = 10
ball_radius = 5 max_radius = 8
function _init() function _init()
balls = {} balls = {}
@@ -8,9 +8,8 @@ function _init()
add(balls, ball:new({ add(balls, ball:new({
pos=vec2:new(rnd(127),rnd(127)), pos=vec2:new(rnd(127),rnd(127)),
vel=vec2:new(rnd(2)-1,rnd(2)-1):normal(), vel=vec2:new(rnd(2)-1,rnd(2)-1):normal(),
rad=ball_radius, rad=2+flr(rnd(max_radius)),
col=rnd(15)+1 }))
}))
end end
end end
@@ -43,10 +42,10 @@ function handle_collisions()
local t_xpoint = target.pos local t_xpoint = target.pos
repeat repeat
steps += 1 steps += 1
rewind += 0.1 * steps rewind = 0.1 * steps
s_xpoint = source.pos - source.vel * rewind s_xpoint = source.pos - source.vel * rewind
t_xpoint = target.pos - target.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 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) local t_newvel = collision_vel(t_xpoint, target.vel, s_xpoint, source.vel)
source.pos = s_xpoint + source.vel * rewind source.pos = s_xpoint + source.vel * rewind
+7 -8
View File
@@ -106,15 +106,15 @@ function ball:new(o)
ball.__count += 1 ball.__count += 1
local o = o or {} local o = o or {}
o.id = ball.__count o.id = ball.__count
o.col = o.id % 16 o.col = o.col or o.id % 16
setmetatable(o, self) setmetatable(o, self)
o.__index = self o.__index = self
return o return o
end end
-->8 -->8
ball_count = 16 ball_count = 10
ball_radius = 5 max_radius = 8
function _init() function _init()
balls = {} balls = {}
@@ -123,9 +123,8 @@ function _init()
add(balls, ball:new({ add(balls, ball:new({
pos=vec2:new(rnd(127),rnd(127)), pos=vec2:new(rnd(127),rnd(127)),
vel=vec2:new(rnd(2)-1,rnd(2)-1):normal(), vel=vec2:new(rnd(2)-1,rnd(2)-1):normal(),
rad=ball_radius, rad=2+flr(rnd(max_radius)),
col=rnd(15)+1 }))
}))
end end
end end
@@ -158,10 +157,10 @@ function handle_collisions()
local t_xpoint = target.pos local t_xpoint = target.pos
repeat repeat
steps += 1 steps += 1
rewind += 0.1 * steps rewind = 0.1 * steps
s_xpoint = source.pos - source.vel * rewind s_xpoint = source.pos - source.vel * rewind
t_xpoint = target.pos - target.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 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) local t_newvel = collision_vel(t_xpoint, target.vel, s_xpoint, source.vel)
source.pos = s_xpoint + source.vel * rewind source.pos = s_xpoint + source.vel * rewind