Making a fixture with a polygon

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
Ferreret
Prole
Posts: 4
Joined: Thu Mar 15, 2018 4:56 pm

Making a fixture with a polygon

Post by Ferreret »

Hi community,

It's my first question here. I've looked for some topic similar to this but I haven't found anything. What I want to do is making a fixture for a polygon. First of all I've created a map with Tiled. Then I take the points in the polyline field of the object. But when I run the program, the polygon fixture is in another place. I print the points in the console and they are correct, but they are moved in the map.

I also make rectangles shapes, more less, in the same way and it works perfectly.

I have a function like this to extract the points (because the field polyline doesn't return the points as love.physics.newFixture expects):

Code: Select all

function makePointsPolyline(polyline)
  local points = {}
  for i,obj in pairs(polyline) do
    table.insert(points, obj.x)
    table.insert(points, obj.y)
  end
  return points
end
And then I make the object:

Code: Select all

platform.body = love.physics.newBody(myWorld, x, y, "static")
platform.shape = love.physics.newPolygonShape(points)
platform.fixture = love.physics.newFixture(platform.body, platform.shape)
Could you give me some advice? Thank you very much.
User avatar
pgimeno
Party member
Posts: 3544
Joined: Sun Oct 18, 2015 2:58 pm

Re: Making a fixture with a polygon

Post by pgimeno »

Ferreret wrote: Thu Mar 15, 2018 5:13 pm Hi community,

It's my first question here. I've looked for some topic similar to this but I haven't found anything. What I want to do is making a fixture for a polygon. First of all I've created a map with Tiled. Then I take the points in the polyline field of the object. But when I run the program, the polygon fixture is in another place. I print the points in the console and they are correct, but they are moved in the map.
Hi, welcome to the forums.

The problem is probably that the points of the polygon are relative to the body they belong to. If you create a rectangle with only width and height, the centre will be that of the object and you won't see the problem.

So, if you have the points in world coordinates, you will need to subtract the position where you're going to place the platform's body, to make them relative to the body.
Ferreret
Prole
Posts: 4
Joined: Thu Mar 15, 2018 4:56 pm

Re: Making a fixture with a polygon

Post by Ferreret »

pgimeno wrote: Thu Mar 15, 2018 10:12 pm The problem is probably that the points of the polygon are relative to the body they belong to. If you create a rectangle with only width and height, the centre will be that of the object and you won't see the problem.
But if I print the point in the console I can see the same point as in Tiled. I think they are not relative to the body. Anyway, I've tried to substract it and doesn't work...
User avatar
pgimeno
Party member
Posts: 3544
Joined: Sun Oct 18, 2015 2:58 pm

Re: Making a fixture with a polygon

Post by pgimeno »

Ferreret wrote: Thu Mar 15, 2018 10:28 pmBut if I print the point in the console I can see the same point as in Tiled. I think they are not relative to the body. Anyway, I've tried to substract it and doesn't work...
They are definitely relative to the body. That's what allows you to reuse the same polygon for different bodies. Think Asteroids, for example. Several asteroids with the same size could use the same PolygonShape, and each be in a different Body.

Are you sure you are subtracting the coordinates of the position where you place the body? In your snippet that's x, y.
Ferreret
Prole
Posts: 4
Joined: Thu Mar 15, 2018 4:56 pm

Re: Making a fixture with a polygon

Post by Ferreret »

Thank you for the response. This is now my code:

Code: Select all

function maps:makePointsPolyline(x, y, vertices)
  local points = {}
  for i,obj in pairs(vertices) do
    table.insert(points, obj.x - x)
    table.insert(points, obj.y - y)
  end
  return points
end
obj.x is the relative point and x is the point of the body. If I make a polygon like a square it works, but with a polygon with six or seven points it's a bit moved. I'm missing something, but I don't know what.
User avatar
pgimeno
Party member
Posts: 3544
Joined: Sun Oct 18, 2015 2:58 pm

Re: Making a fixture with a polygon

Post by pgimeno »

I don't know either because I can't see the context where that is used. This simple test works just fine and shows that the polygon is indeed relative to the body:

Code: Select all

local polygon = {-60,   0,
                 -80, -20,
                  80, -20,
                  40,   0,
                   0, -40}

local shape = love.physics.newPolygonShape(polygon)

local world = love.physics.newWorld(0, 100)
local body1 = love.physics.newBody(world, 400, 150, "dynamic")
body1:setAngle(-1.3)
local body2 = love.physics.newBody(world, 400, 450, "static")
local fixture1 = love.physics.newFixture(body1, shape)
local fixture2 = love.physics.newFixture(body2, shape)

function love.update(dt)
  world:update(dt)
end

function love.draw()
  love.graphics.polygon("line", body1:getWorldPoints(shape:getPoints()))
  love.graphics.polygon("line", body2:getWorldPoints(shape:getPoints()))
end
Ferreret
Prole
Posts: 4
Joined: Thu Mar 15, 2018 4:56 pm

Re: Making a fixture with a polygon

Post by Ferreret »

Thank you pgimeno. My code is working with triangles and squares (also with relative points), but if I do a polyline with more vertex it doesn't work. I don't know why. I can manage with triangles, but I would like to know why. Anyway, at the moment I can continue.
Post Reply

Who is online

Users browsing this forum: No registered users and 40 guests