How do I set the center of a fixture/body consisting of multiple shapes (triangles)?
-
2\$\begingroup\$ Does Box2D run in Flash? If not, why is this tagged actionscript-3? \$\endgroup\$– WackidevJun 13, 2012 at 15:32
-
\$\begingroup\$ The point of this is I'm trying to set where the center of the body is as you can see in the bottom it's in the correct place where the wheels would be by giving the it vertices where the origin (0, 0) is towards the front of the car where the steering wheels would be. However when I triangulate this shape and attach the triangles to the fixture, the center of the body is moved to it's center. Where means when I try and rotate the car it looks odd. @Wackidev, Yes it has been ported to Flash (box2dflash.org) \$\endgroup\$– jshbrnttJun 13, 2012 at 18:33
-
1\$\begingroup\$ @synth Wow; I didn't know that. It's been a long time since I used Box2D, but it would seem that "center" is an immutable point. Can you move the center of a circle without moving the circle itself? It would break the circle's very definition. This is a more complicated shape, but I think the same principle applies. Maybe you could keep an offset vector from the "real" center and perform your rotations around the offset position. \$\endgroup\$– WackidevJun 13, 2012 at 19:54
-
\$\begingroup\$ So, it's correctly finding the center, and you want to change that to off-center? "the center of the body is moved to its center" Isn't that expected? Can't you just define an offset from true center to where you want your center to be? \$\endgroup\$– HouseJun 13, 2012 at 21:22
-
\$\begingroup\$ I don't understand why you chose to triangulate these shapes? They are convex and don't have to be triangulated.. just keep them as they are. \$\endgroup\$– bummzackAug 13, 2012 at 7:08
1 Answer
From the box2d manual:
A body has two main points of interest. The first point is the body's origin. Fixtures and joints are attached relative to the body's origin. The second point of interest is the center of mass. The center of mass is determined from mass distribution of the attached shapes or is explicitly set with b2MassData. Much of Box2D's internal computations use the center of mass position. For example b2Body stores the linear velocity for the center of mass. When you are building the body definition, you may not know where the center of mass is located. Therefore you specify the position of the body's origin. You may also specify the body's angle in radians, which is not affected by the position of the center of mass. If you later change the mass properties of the body, then the center of mass may move on the body, but the origin position does not change and the attached shapes and joints do not move.
bodyDef.position.Set(0.0f, 2.0f); // the body's origin position.
bodyDef.angle = 0.25f * b2_pi; // the body's angle in radians.
So it seems you can either set the origin, or the center of mass through the MassData.