Problem is, with this type of implementation collision detection occurs only for the corners of the square and not with the edges.
Is there a way to create a bounding polygon having the xy coordinates of the 4 bodies as vertices, such that if another body touches a jelly square edge the pivots react and compress?
Here is the code so far, it started out from the love.physics tutorial (arrow keys to move square around):
Code: Select all
function love.load()
world = love.physics.newWorld(-650, -650, 650, 650)
world:setGravity(0,700)
world:setMeter(64)
bodies = {}
shapes = {}
joints = {}
bodies[0] = love.physics.newBody(world, 625/2, 625, 0, 0)
shapes[0] = love.physics.newRectangleShape(bodies[0], 0, 0, 800, 50, 0)
bodies[1] = love.physics.newBody(world, 100, 100, 10, 0)
bodies[2] = love.physics.newBody(world, 200, 100, 10, 0)
bodies[3] = love.physics.newBody(world, 200, 200, 10, 0)
bodies[4] = love.physics.newBody(world, 100, 200, 10, 0)
for i = 1,4 do
shapes[i] = love.physics.newCircleShape(bodies[i], 0, 0, .1)
end
joints[1] = love.physics.newDistanceJoint(bodies[1], bodies[2],100,100,200,100)
joints[2] = love.physics.newDistanceJoint(bodies[2], bodies[3],200,100,200,200)
joints[3] = love.physics.newDistanceJoint(bodies[3], bodies[4],200,200,100,200)
joints[4] = love.physics.newDistanceJoint(bodies[4], bodies[1],100,200,100,100)
joints[5] = love.physics.newDistanceJoint(bodies[1], bodies[3],100,100,200,200)
joints[6] = love.physics.newDistanceJoint(bodies[2], bodies[4],200,100,100,200)
for i = 1,6 do
joints[i]:setFrequency(4)
joints[i]:setDamping(0.75)
end
love.graphics.setBackgroundColor(104, 136, 248)
love.graphics.setMode(650, 650, false, true, 0)
end
function love.update(dt)
world:update(dt)
for i = 1,4 do
if love.keyboard.isDown("right") then
bodies[i]:applyForce(300, 0)
elseif love.keyboard.isDown("left") then
bodies[i]:applyForce(-300, 0)
elseif love.keyboard.isDown("up") then
bodies[i]:applyForce(0,-300)
end
end
end
function love.draw()
local x1, y1, x2, y2, x3, y3, x4, y4 = shapes[0]:getBoundingBox()
local boxwidth = x3 - x2
local boxheight = y2 - y1
love.graphics.setColor(72, 160, 14)
love.graphics.rectangle("fill", bodies[0]:getX() - boxwidth/2, bodies[0]:getY() - boxheight/2, boxwidth, boxheight)
love.graphics.setColor(193, 47, 14)
love.graphics.polygon('fill', bodies[1]:getX(), bodies[1]:getY(),bodies[2]:getX(), bodies[2]:getY(),bodies[3]:getX(), bodies[3]:getY(),bodies[4]:getX(), bodies[4]:getY())
end