Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Conversion from Box2D to SFML creates gap  (Read 4562 times)

0 Members and 1 Guest are viewing this topic.

nicktlloyd

  • Newbie
  • *
  • Posts: 7
    • View Profile
Conversion from Box2D to SFML creates gap
« on: April 22, 2016, 09:01:18 pm »
I have the following bit of code, which basically takes a Box2D world and converts it to SFML shapes that are then drawn to the screen. For rectangles, this works perfectly, but for other polygons, it produces a strange gap between the object and it's representation in Box2D which manifests as a gap between objects on screen. Can anyone see why this might be the case?

b2PolygonShape* polyShape = (b2PolygonShape*) bodyFixture->GetShape();

ConvexShape shapeToFill;
shapeToFill.setPosition(body->GetWorldCenter().x*SCALE, body->GetWorldCenter().y*SCALE);

int vertCount = polyShape->GetVertexCount();
shapeToFill.setPointCount(vertCount);

for(int vert = 0 ; vert < vertCount ; vert++) {
   b2Vec2 aVertex = polyShape->GetVertex(vert);
   sf::Vector2f sfVect;
   sfVect.x = aVertex.x*SCALE;
   sfVect.y = aVertex.y*SCALE;
   shapeToFill.setPoint(vert,sfVect);
}
shapeToFill.setRotation(180/b2_pi * body->GetAngle());
 

UPDATE, SOLVED: The problem lay with this line:
shapeToFill.setPosition(body->GetWorldCenter().x*SCALE, body->GetWorldCenter().y*SCALE);
which needed to be changed to:
shapeToFill.setPosition(body->GetPosition().x*SCALE, -body->GetPosition().y*SCALE);
The world center of a body is not it's centroid, but it's local origin.
« Last Edit: April 23, 2016, 04:34:35 pm by nicktlloyd »

fallahn

  • Sr. Member
  • ****
  • Posts: 492
  • Buns.
    • View Profile
    • Trederia
Re: Conversion from Box2D to SFML creates gap
« Reply #1 on: April 22, 2016, 09:46:35 pm »
It looks like you're not inverting your Y coordinates (box2D starts at the bottom of the screen, SFML from the top).

nicktlloyd

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Conversion from Box2D to SFML creates gap
« Reply #2 on: April 22, 2016, 10:43:47 pm »
Thanks for the reply! I flipped the sign on the y coordinates when I create, and when I draw, but the problem persists. Here's a screenshot of several polygons at rest.

victorlevasseur

  • Full Member
  • ***
  • Posts: 206
    • View Profile
Re: Conversion from Box2D to SFML creates gap
« Reply #3 on: April 22, 2016, 10:56:13 pm »
Are you turning the shapes in the good direction ?

nicktlloyd

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Conversion from Box2D to SFML creates gap
« Reply #4 on: April 22, 2016, 11:10:42 pm »
The good direction? I'm turning it according to the angle in box2d. Here's the code for how I'm entering polygons (sorry, I know this is box2d, but I'm not sure if the problem is with it or how I'm translating to SFML).

b2Body* PhysicsArea::addComponentWithName(std::string name,b2Vec2 origin, b2Vec2 points[], int pointCount, float rotation) {

    PhysicsAreaComponent *comp = components.at(name);
    b2BodyDef def;
    def.position = b2Vec2(origin.x/SCALE,-origin.y/SCALE);
        def.type = comp->bodyType;

        b2Body *body = world.CreateBody(&def);

    for (int i = 0; i < pointCount; i++) points[i].Set(points[i].x/SCALE,-points[i].y/SCALE);


        b2PolygonShape shape;
        shape.Set(points, pointCount);

        b2FixtureDef fixture;
        fixture.density = comp->density;
        fixture.friction = comp->friction;
        fixture.shape = &shape;
        body->CreateFixture(&fixture);
        body->SetUserData(&comp->name);
        std::string *compID = (static_cast<std::string*>(body->GetUserData()));
    return body;

}
 

fallahn

  • Sr. Member
  • ****
  • Posts: 492
  • Buns.
    • View Profile
    • Trederia
Re: Conversion from Box2D to SFML creates gap
« Reply #5 on: April 22, 2016, 11:24:58 pm »
As the Y coords are flipped as is the direction of rotation.

If it helps here and here are my conversion functions for reference.

nicktlloyd

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Conversion from Box2D to SFML creates gap
« Reply #6 on: April 23, 2016, 01:24:24 am »
Thanks for the continuing responses! I think the rotation is correct because the shape acts the way I would expect it to. It seems as though there is just a gap between the body in box2d and the texture. The strange part is that the gap seems to scale with non reflected points. If I define a triangle with one side down, the bottom will be drawn inside the ground, and other bodies will be pushed away from the top tip at the same distance. The closer I get to a rectangle, the less of a gap, to the point that if I define a perfect rectangle, there's no gap.

DarkRoku12

  • Full Member
  • ***
  • Posts: 203
  • Lua coder.
    • View Profile
    • Email
Re: Conversion from Box2D to SFML creates gap
« Reply #7 on: April 23, 2016, 03:40:31 am »
Well i use this for the debug drawing class with box2d

I'll post the shape method:

I use a ease conversion between meters and pixels. 1 meter = 10 pixels. So simple and easy.


  double to_mts = 0.1f ;
  double to_pixel = 10.0f ;

  sf::Color toSFML( b2Color color )
  { sf::Color c ; c.r = color.r * 255 ; c.g = color.g * 255 ; c.b = color.b * 255 ; return c ; }

    void DrawSolidPolygon( const b2Vec2* vertices, int32 vertexCount, const b2Color& color )
        {
                 sf::ConvexShape convex ; convex.setPointCount( vertexCount ) ;
           
                 for( int i = 0 ; i < vertexCount ; ++i )
                    convex.setPoint( i , sf::Vector2f( vertices[i].x * to_pixel , vertices[i].y * to_pixel ) ) ;

                 convex.setFillColor( toSFML( color ) ) ; window->draw( convex ) ;
        }

 
I would like a spanish/latin community...
Problems building for Android? Look here

nicktlloyd

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Conversion from Box2D to SFML creates gap
« Reply #8 on: April 23, 2016, 06:17:23 am »
Thanks for your example! Where are you adding the position? I'm beginning to wonder if maybe the origin is off... I mean, the shape is being drawn at the correct size, and acts correctly, it's just not in the right spot.

DarkRoku12

  • Full Member
  • ***
  • Posts: 203
  • Lua coder.
    • View Profile
    • Email
Re: Conversion from Box2D to SFML creates gap
« Reply #9 on: April 23, 2016, 06:53:28 am »
Thanks for your example! Where are you adding the position? I'm beginning to wonder if maybe the origin is off... I mean, the shape is being drawn at the correct size, and acts correctly, it's just not in the right spot.

In debug drawing (from box2d) we don't need the position, the vertex already include it.
« Last Edit: April 23, 2016, 09:07:45 am by Laurent »
I would like a spanish/latin community...
Problems building for Android? Look here

nicktlloyd

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Conversion from Box2D to SFML creates gap
« Reply #10 on: April 23, 2016, 04:33:47 pm »
Thank you to everyone who helped out! I finally figured out what the problem was and updated the post. Thanks again, you guys are awesome!