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.


Topics - nicktlloyd

Pages: [1]
1
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.

2
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]
anything