Multiple fixtures per body (love.physics)

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
christian-pfeifer
Prole
Posts: 2
Joined: Fri Sep 18, 2015 6:01 pm

Multiple fixtures per body (love.physics)

Post by christian-pfeifer »

Hello everybody,

i have a problem with love.physics. I have created a little test-game. In this game i create one body and attach multiple fixtures (+shapes). At the update-method i rotate the body. All works fine, but the graphical result is not what i expected. Here is the code and a graphic explain what i expect and what i get.

What i'm doing wrong?

https://love2d.org/imgmirrur/zmzGppo.png

Code: Select all

Test = Class('Test');

function Test:initialize()
    local size = 25;
    local x, y = 0, 0;
    
    self.body = love.physics.newBody(World, 350, 250, "dynamic");
    
    for i=0, 2, 1 do
        local shape = love.physics.newRectangleShape(x, y, size, size, 0);
        local fixture = love.physics.newFixture(self.body, shape, 1);
    
        x = x + size;
    end
end

function Test:draw()
    local fixtures = self.body:getFixtureList();
    
    for idx, fixture in pairs(fixtures) do
        local shape = fixture:getShape();
        
        local x, y = self.body:getWorldPoints(shape:getPoints());
        
        love.graphics.setColor(255, 0, 0, 255);
        love.graphics.rectangle("line", x, x, 25, 25);
        
        love.graphics.setColor(255, 0, 0, 50);
        love.graphics.rectangle("fill", y, y, 25, 25);
    end    
end

function Test:update(dt)
    self.body:applyTorque(100);
end
Thanks for your help.
Christian
User avatar
adnzzzzZ
Party member
Posts: 305
Joined: Sun Dec 26, 2010 11:04 pm
Location: Porto Alegre, Brazil

Re: Multiple fixtures per body (love.physics)

Post by adnzzzzZ »

getWorldPoints(shape:getPoints()) will return you multiple points, not only x, y. You should draw those points using love.graphics.polygon.

Here's some code I wrote that does the same for all types of different shapes: https://github.com/adonaac/hxdx/blob/ma ... it.lua#L70
christian-pfeifer
Prole
Posts: 2
Joined: Fri Sep 18, 2015 6:01 pm

Re: Multiple fixtures per body (love.physics)

Post by christian-pfeifer »

Thank you very much adnzzzzZ,
is was that simble :megagrin:
Post Reply

Who is online

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