nudge colliding definitions aside when moving
Implementation copied from: https://akkartik.itch.io/carousel/devlog/649349/handling-cascading-collisions
This commit is contained in:
parent
bc12efc6be
commit
b7b45ff969
|
@ -25,9 +25,10 @@ on.mouse_press = function(x,y, mouse_button)
|
|||
local node = on_handle(x,y)
|
||||
if node then
|
||||
-- move node
|
||||
prepare_to_move()
|
||||
Move = {xoff=App.mouse_x()-vx(node.x), yoff=App.mouse_y()-vy(node.y), node=node}
|
||||
return
|
||||
end
|
||||
-- pan surface
|
||||
Pan = {x=Viewport.x+x/Viewport.zoom, y=Viewport.y+y/Viewport.zoom}
|
||||
end
|
||||
end
|
|
@ -23,6 +23,9 @@ on.update = function(dt)
|
|||
-- quantize the x axis to discrete columns
|
||||
Move.node.x = round(Move.node.x/100)*100
|
||||
A1(Move.node.key)
|
||||
Move.node.pos.x = Move.node.x+Move.node.w/2
|
||||
Move.node.pos.y = Move.node.y+Move.node.h/2
|
||||
move_others(Move.node)
|
||||
return
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,15 @@
|
|||
-- returns the _minimum separation vector_ if there's a collision
|
||||
-- requires a pos+halfsize representation for nodes
|
||||
collide = function(a, b)
|
||||
local delta = {x=a.pos.x-b.pos.x, y=a.pos.y-b.pos.y}
|
||||
local abs_delta = {x=math.abs(a.pos.x-b.pos.x), y=math.abs(a.pos.y-b.pos.y)}
|
||||
local size = {x=a.hs.x+b.hs.x, y=a.hs.y+b.hs.y}
|
||||
local abs_amount = {x=size.x-abs_delta.x, y=size.y-abs_delta.y}
|
||||
if abs_amount.x > 0 and abs_amount.y > 0 then
|
||||
if abs_amount.x <= abs_amount.y then
|
||||
return {x=abs_amount.x*sign(delta.x), y=0}
|
||||
else
|
||||
return {x=0, y=abs_amount.y*sign(delta.y)}
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,13 @@
|
|||
-- move any colliding Rects to make room for `a`
|
||||
move_others = function(a)
|
||||
for _,d in pairs(Definitions) do
|
||||
if d ~= a then
|
||||
local msv = collide(a, d)
|
||||
if msv then
|
||||
move(d, msv)
|
||||
A1(d.key)
|
||||
move_others(d)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,12 @@
|
|||
prepare_to_move = function()
|
||||
for _,def in pairs(Definitions) do
|
||||
assert(def.w)
|
||||
assert(def.h)
|
||||
if def.pos == nil then def.pos = {} end
|
||||
if def.hs == nil then def.hs = {} end
|
||||
def.pos.x = def.x + def.w/2
|
||||
def.pos.y = def.y + def.h/2
|
||||
def.hs.x = def.w/2
|
||||
def.hs.y = def.h/2
|
||||
end
|
||||
end
|
|
@ -0,0 +1,6 @@
|
|||
move = function(def, msv)
|
||||
def.x = def.x - msv.x
|
||||
def.y = def.y - msv.y
|
||||
def.pos.x = def.pos.x - msv.x
|
||||
def.pos.y = def.pos.y - msv.y
|
||||
end
|
Loading…
Reference in New Issue