Fix rewind step calculation
This commit is contained in:
@@ -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
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user