How to specify coordinates/center on PolygonShape

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
G.o.D
Prole
Posts: 8
Joined: Wed Aug 17, 2011 6:15 am

How to specify coordinates/center on PolygonShape

Post by G.o.D »

Hi,

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
So what i am doing is, get the center of the polygon, set it as position of the body, and then change the original point positions, so they draw the shape around the centers position.

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
Rad3k
Citizen
Posts: 69
Joined: Mon Aug 08, 2011 12:28 pm

Re: How to specify coordinates/center on PolygonShape

Post by Rad3k »

AFAIK, Box2d can only handle convex polygons, and there's an upper limit to the number of vertices (I think that's 8, but don't know for sure). If you have a concave polygon, or higher number of vertices, you have to divide it somehow into smaller concave convex polys, and then assign all of them to a single physics body. Also, polygons in Box2d have to be winded counterclockwise, but for Löve that's clockwise (because in Löve the y axis is pointing down).
Last edited by Rad3k on Mon Sep 05, 2011 2:39 pm, edited 1 time in total.
G.o.D
Prole
Posts: 8
Joined: Wed Aug 17, 2011 6:15 am

Re: How to specify coordinates/center on PolygonShape

Post by G.o.D »

Thanx for the quick reply, didn't know of the vertex limit, good to know. The polygon in question is just a little 'out of shape' box, no convex polygons, 4 vertices. Collision shape does work, but its moving (actually rotating) fancy
Rad3k
Citizen
Posts: 69
Joined: Mon Aug 08, 2011 12:28 pm

Re: How to specify coordinates/center on PolygonShape

Post by Rad3k »

Then it looks like the center of mass is not where it should be, and this "fileToPhysicsGetCenterOfPoly" function seems to be the cultprit. It's supposed to return the center of the polygon, right? It seems that it returns halved width and height instead.
G.o.D
Prole
Posts: 8
Joined: Wed Aug 17, 2011 6:15 am

Re: How to specify coordinates/center on PolygonShape

Post by G.o.D »

That is exactly my point: how to i set the center of mass?

The fileToPhysicsGetCenterOfPoly returns a number of values, including the half width and the most left point, which together i use on newBody() as the position, which i assumed is the center of mass, and then i draw the polygon shape around this position (which means there are negative positions), is this the correct way?
G.o.D
Prole
Posts: 8
Joined: Wed Aug 17, 2011 6:15 am

Re: How to specify coordinates/center on PolygonShape

Post by G.o.D »

arr, just fixed it

Code: Select all

if (i%2 == 1) then
       points[i]=v-minX
else
       points[i]=v-minY
end
should be

Code: Select all

if (i%2 == 1) then
       points[i]=v-minX-halfX
else
       points[i]=v-minY-halfY
end
the object was slightly off center, and together with the very low mass of the object, the movement looked totally wonky... a small leak will sink a great ship ;)

actually your hint with the half width got me to it, thanx
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 1 guest