After asking a similar question yesterday I've come across another problem with using joints in box2D / LOVE and trying to create a weighted chain.
Everything is set up as follows, I've tried to remove most of the fluff:
Each link is created as a body/shape pair then joined together and added to a links table.
for i = 1, segments, 1 do
link = {}
link.body = love.physics.newBody(world, xpos, ypos, "dynamic")
if (i == segments) then
link.shape = love.physics.newCircleShape(endlink_radius) --Ending link
else
link.shape = love.physics.newCircleShape(link_radius)
end
link.fixture = love.physics.newFixture(link.body, link.shape) --Fix bodies to shapes
table.insert(links,link)
ypos = ypos + link_distance
end
Links are joined together using rope joints to allow for some springy-ness:
for i = 2, #links, 1 do
x1,y1 = links[i-1].body:getPosition()
x2,y2 = links[i].body:getPosition()
links[i-1].join = love.physics.newRopeJoint(links[i-1].body, links[i].body, x1, y1, x2, y2, link_distance, true )
end
The player controls the first element of the chain and holds the table of links, its created as follows:
local body = love.physics.newBody(world, 300, 100, "kinematic")
local shape = love.physics.newCircleShape(3)
local fixture = love.physics.newFixture(body, shape)
local chain = _chain.new(world)
Joined to the chain:
x1,y1 = body:getPosition()
x2,y2 = links[1].body:getPosition()
join = love.physics.newRevoluteJoint(body, links[1].body, x1, y1, x2, y2, true )
The player tracks the mouse cursor, determines a velocity and moves towards it by setting the linear velocity of the first chain element:
body:setLinearVelocity(velocity.x, velocity.y)
And this very nearly works, you can pull the chain around quite nicely using the mouse (directing the top pink element), however if you keep spinning the chain it begins to stretch and pull away from the first element:
Any ideas on how to solve this?
I've tried using different joints and parameters, adding a reinforcing joint from the first link to the last, adjusting body weights and altering the update iterations but can't seem to find anything effective.
Many thanks.