Shape:getPoints() behaves weird with multiple fixtures/shapes in a body [SOLVED]

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
User avatar
iimurpyh
Prole
Posts: 6
Joined: Tue Feb 04, 2020 1:02 am

Shape:getPoints() behaves weird with multiple fixtures/shapes in a body [SOLVED]

Post by iimurpyh » Wed Feb 12, 2020 10:33 pm

This is somewhat related to my last two posts because it's about making two box2d bodies act like one, but this is a separate issue so I thought it deserved its own thread.

Since there is no getPosition() function for shapes, I've had to get their x/y positions using the points provided by Fixture:getBoundingBox() and some math. (I need to get x/y positions so I can draw an image at the object's position.) However, the bounding box returned by Fixture:getBoundingBox doesn't actually represent where the points of the shape would really be, especially in the case of rotation. Instead, I've opted to use Shape:getPoints() converted to world coordinates, so Body:getWorldPoints(Shape:getPoints()) - but it seems like this dosen't accurately show where points are when two fixtures are in one body. When one fixture gets added to a body already containing another fixture, the former just falls as if it were an independent shape that can't collide with anything - but only when drawn! In the box2d simulation, it still behaves like it's been "glued" to the former shape.

Here is a demo showing what I just described. (You can see it easier if you connect the two bodies while they're still airborne.)

Here is a version that uses Fixture:getBoundingBox() to draw the bodies. It works as intended, but only when angles aren't considered.

You may find some fragments of unused code in the demos, that's because I hastily scrapped the code for drawing and creating bodies from another project.
Also, angles aren't considered at all in both examples.
Last edited by iimurpyh on Wed Feb 12, 2020 11:53 pm, edited 1 time in total.

User avatar
pgimeno
Party member
Posts: 1987
Joined: Sun Oct 18, 2015 2:58 pm

Re: Shape:getPoints() behaves weird with multiple fixtures/shapes in a body

Post by pgimeno » Wed Feb 12, 2020 11:12 pm

Once you make the shape part of the second body, the first body is meaningless and needs to be destroyed. But you were using it for calculations during draw, which is why it didn't work for you.

Code: Select all

    box1.body:destroy()
    box1.body = box2.body
Adding that at the end of Weld(), it draws properly for me.

User avatar
iimurpyh
Prole
Posts: 6
Joined: Tue Feb 04, 2020 1:02 am

Re: Shape:getPoints() behaves weird with multiple fixtures/shapes in a body

Post by iimurpyh » Wed Feb 12, 2020 11:53 pm

pgimeno wrote:
Wed Feb 12, 2020 11:12 pm
Once you make the shape part of the second body, the first body is meaningless and needs to be destroyed. But you were using it for calculations during draw, which is why it didn't work for you.

Code: Select all

    box1.body:destroy()
    box1.body = box2.body
Adding that at the end of Weld(), it draws properly for me.
This worked for me, thanks! :)

Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests