Page 1 of 1

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

Posted: Wed Feb 12, 2020 10:33 pm
by iimurpyh
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.

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

Posted: Wed Feb 12, 2020 11:12 pm
by pgimeno
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.

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

Posted: Wed Feb 12, 2020 11:53 pm
by iimurpyh
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! :)