Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

How do I set the center of a fixture/body consisting of multiple shapes (triangles)? enter image description here

share|improve this question
    
Thanks for working on your accept rate! OK, so what have you tried so far? Also it seems by your image that the first is more centered than the second. What are you trying to accomplish? –  Byte56 Jun 13 '12 at 15:04
2  
Does Box2D run in Flash? If not, why is this tagged actionscript-3? –  Wackidev Jun 13 '12 at 15:32
    
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) –  SyntheCypher Jun 13 '12 at 18:33
1  
@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. –  Wackidev Jun 13 '12 at 19:54
    
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? –  Byte56 Jun 13 '12 at 21:22
show 1 more comment

1 Answer 1

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.