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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - nicktlloyd

Pages: [1]
1
Graphics / Re: Conversion from Box2D to SFML creates gap
« 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!

2
Graphics / Re: Conversion from Box2D to SFML creates gap
« 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.

3
Graphics / Re: Conversion from Box2D to SFML creates gap
« 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.

4
Graphics / Re: Conversion from Box2D to SFML creates gap
« 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;

}
 

5
Graphics / Re: Conversion from Box2D to SFML creates gap
« 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.

6
Graphics / 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.

7
Window / sf::Mouse::getPosition slightly off
« on: April 20, 2016, 07:56:18 pm »
I'm using:
   sf::Vector2i pixelPos = sf::Mouse::getPosition(*this);
   sf::Vector2f worldPos = this->mapPixelToCoords(pixelPos);
to get the position of mouse clicks within a world. When I draw draw things based on these coordinates, everything is drawing slightly higher than where the mouse actually was. This is consistent no matter how I change the view, it's always off by the same amount. If I resize the window, it's off in both axis' by more, consistently. I know this is somewhat vague, as the code for the window is spread out across three or four classes. I'm just wondering if there is a sample program out there that deals with window resizes so I can see what's going on. Thanks!

Pages: [1]