i am having trouble with the PolygonShape, i have polygon coordinates from a vector program, which are abolute point coordinates on the canvas. If i take them and draw a polygon via love.graphics.polygon i get exactly the shape i want.
Now i want to use this polygon shape in a physics simulation, so i use:
Code: Select all
function fileToPhysicsGetCenterOfPoly(verts)
local minX, minY = 999999999999, 999999999999
local maxX, maxY = 0, 0
for i,v in ipairs(verts) do
local val = tonumber(v)
if (i%2 == 1) then
if (val<minX) then
minX = val
elseif (val>maxX) then
maxX = val
end
else
if (val<minY) then
minY = val
elseif (val>maxY) then
maxY = val
end
end
end
local w = maxX-minX
local h = maxY-minY
return (w/2), (h/2), minX, minY, maxX, maxY
end
function love.load()
world = love.physics.newWorld(0, 0, 800, 600)
world:setGravity(0, 550)
world:setMeter(64)
local points = {251, 183, 300, 100, 369, 200, 295, 271}
local halfX, halfY, minX, minY, maxX, maxY = fileToPhysicsGetCenterOfPoly(points)
for i,v in ipairs(points) do
if (i%2 == 1) then
points[i]=v-minX
else
points[i]=v-minY
end
end
body = love.physics.newBody(world, minX+halfX, minY+halfY, fileToPhysicsGetValue(p, 'mass', 0), fileToPhysicsGetValue(p, 'inertia', 100))
shape = love.physics.newPolygonShape(body, unpack(points))
end
function love.draw()
love.graphics.polygon("fill", shape:getPoints())
end
It looks exactly as expected, but once i run the physics simulation, the object behaves totally crazy. I also have several rectangle and circle physics objects in the simulation, they behave correct, but the polygon object does collide correct, but as soon as it starts rotating, it acts like it would be connected to a huge lever that swings around its center, it rotates back and forth and takes long time to stop (if at all, didn't wait that long )
How do i set up a polygon object so it simulates correctly?
I can upload a .love file, but its a bunch of code i think its easier this way.
Thank you