Box2d offset shape from body
Posted: Wed Mar 25, 2015 2:30 am
My understanding of Box2d is that, upon creating a shape, it will be centered on the body that you attach it to via newFixture.
Two questions stem from this.
Firstly, how do I compute the center of an arbitrary polygon?
Second, how would I position a shape offset from the body that it belongs to?
For instance, if I wanted to make a submarine I'd do two circles with an overlapping rectangle in the middle and a second rectangle for the sail, and then I'd attach all of these shapes to one body. Or I could just make a submarine shaped polygon, but then I run into the first problem in that it will not be centered relative to the body object and I'll need to be able to either offset it so that, say, I can get the torpedoes and missiles to come out of the right places, or I would need to calculate where the center of that polygon is so that I can offset the torpedo and missile origins.
I understand that I can get the mass of the body from the density of the attached shapes, and that I can get the center of mass from that. But how would I then change the relative location of the body or shapes so that, when forces cause it to rotate, it always rotates about the center of mass?
Two questions stem from this.
Firstly, how do I compute the center of an arbitrary polygon?
Second, how would I position a shape offset from the body that it belongs to?
For instance, if I wanted to make a submarine I'd do two circles with an overlapping rectangle in the middle and a second rectangle for the sail, and then I'd attach all of these shapes to one body. Or I could just make a submarine shaped polygon, but then I run into the first problem in that it will not be centered relative to the body object and I'll need to be able to either offset it so that, say, I can get the torpedoes and missiles to come out of the right places, or I would need to calculate where the center of that polygon is so that I can offset the torpedo and missile origins.
I understand that I can get the mass of the body from the density of the attached shapes, and that I can get the center of mass from that. But how would I then change the relative location of the body or shapes so that, when forces cause it to rotate, it always rotates about the center of mass?