How to set the centers of figures / lamps / cases in Box2D

Hey, I'm trying to integrate SFML and Box2D, and SFML made center settings for sprites, forms, etc. very easy. Box2D, on the other hand, is having problems because I cannot figure out how to set or even find the center of a figure or fixture.

It seems to me that when manually adding vertices to b2_PolygonShape, the center is set to the first vertex in the vertex array, but when using the functions of the SetAsBox () shortcut or any other SetAs __ (), the results are very different. The center is the middle of the form, or half (s) of the length (s) of the box / form.

I need to talk about the box2D and SFML centering system, but I cannot understand how local coordinate systems for objects work.

How can I set / get the centers of many objects, such as figures, lamps, bodies, etc. in Box2D?

+7
source share
5 answers

Well, I realized that SFML and Box2D really weren't that different, but I just didn't think about how the forms are created / displayed correctly in world coordinates.

Unless otherwise specified, (0,0) is always used as the point / center of the refrence of an object when its position moves around / is drawn, and each vertex is drawn relative to this point.

The SFML tutorial was a bit confusing as it said that adjusting the center of the shape / sprite was an offset from the top left corner of the object, not (0,0).

+4
source

I think with box2d you are responsible for ensuring that the “centroid” of the polygon matches your body position. Basically, what you need to do is something like this.

  • Calculate the centroid of your vertex list (b2vec)

  • Move vertices with -centroid.

Box2d provides the necessary functions to help you solve this problem. What happens to SetAs__ is that the centroid is (0,0), and the polygon is created symmetrically around the center of the body.

+2
source

I'll try again. I’m sure I can help you, but I don’t fully understand your question.

The body has a position ( b2Vec2 ) in the world.
The body has b2Fixture (density, friction, restitution, shape , etc.)

b2PolygonShape has vertices and bulk data. These vertices relate to the position of the body.

Vertex examples for b2PolgygonShape (rectangle, size: 1 meter * 1 meter)

 (-0.5f, -0.5f) // left upper corner ( 0.5f, -0.5f) // right upper corner ( 0.5f, 0.5f) // right lower corner (-0.5f, 0.5f) // left lower corner 

Keep in mind that you cannot set vertices yourself! You must pass them to

 b2PolgygonShape::Set(b2Vec2 *vertices, int count); 

This method will calculate the mass data depending on the vertices.

+1
source

This may be related, although I use Box2DWeb and Easel.js, not Box2D and SFML. The problem I ran into is that Easel.js regX / regY (the graphical center of the sprite) must match the physical center of mass.

For a simple b2Body with one b2FixtureDef, the center of mass of the body is the same as m_centroid for b2FixtureDef.shape (be it SetAsBox, SetAsArray, new b2CircleShape, etc.).

For multi-level bodies and irregular polygons, it worked for me to add all the fixtures (for example, call b2Body.CreateFixture), then set the graphic regX / regY equal to b2Body.m_sweep.localCenter.x / y (multiplied by the pixel / meter scale factor) .

This does not work for simple circles and boxes, but in the first place this is not a problem. Hope this helps.

0
source

You can simply do this:

 b2Body *body = ...; b2Vec2 pos = body->GetPosition(); 

This is the position of the body in the world ( pos.x and pos.y , defined in meters). This means that you need to use a ratio, for example: 1 meter = 20 pixels. Thus, you need to multiply the coordinates by your ratio to get the position in pixels.

Use this position to draw your images. If necessary, you can add coordinates to your image and draw an image in these coordinates relative to the position of the body.

Keep in mind that you should also draw your images with this ratio. When the ratio changes (increase or decrease), the images should also increase.

In my game I use SDL with OpenGL, which is a very nice combination, so I don’t know exactly how SFML works.

-one
source

All Articles