Hi!
I'm writing an offroad 2d game using Box2d and SFML. And i have got a problem with texturing objects - texture of the shape don't rotate with it ->
// B2toSF() and SFtoB2() convert units beetween pixels and Box2d
// RDtoDG() convert radians to degreeses
void Body::displayBody(RenderWindow &win, b2Body *b, const Texture** fillTexture)
// draws all fixtures in *b on win
// fillTexture is ptr to list of ptrs to textures to all fixtures, NULL is NO TEXTURING
{
int i = 0;
for(b2Fixture* f = b->GetFixtureList(); f; f = f->GetNext(), i++)
// for each fixtures in body
{
// shape and aabb of body - don't interest with it, it's box2d utility
b2AABB aabb = f->GetAABB(0);
b2Shape* sh = f->GetShape();
RenderStates st; // transform for all shapes
// rotate shape by center of its aabb
st.transform.rotate(RDtoDG(b->GetAngle()), B2toSF(aabb.GetCenter()));
switch(sh->GetType())
{
case b2Shape::e_polygon: // polygon shape
{
b2PolygonShape* s = dynamic_cast<b2PolygonShape*>(sh);
ConvexShape cs(s->GetVertexCount());
// shapes are NOT ROTATED, RenderStates::transform contains their rotation
if(fillTexture[i])
cs.setTexture(fillTexture[i]);
else
cs.setFillColor(Color::Red);
for(int i = 0; i < s->GetVertexCount(); i++)
cs.setPoint(i, B2toSF(b->GetWorldPoint(s->GetVertex(i))));
// GetWorldPoint() translates local position to global coordinates
win.draw(cs, st);
break;
}
case b2Shape::e_circle: // circle
{
b2CircleShape* s = dynamic_cast<b2CircleShape*>(sh);
CircleShape cs(B2toSF(s->m_radius));
cs.setPosition(B2toSF(aabb.lowerBound)); // left-top of aabb
if(fillTexture[i])
cs.setTexture(fillTexture[i]);
else
cs.setFillColor(Color::Blue);
win.draw(cs, st);
break;
}
case b2Shape::e_edge:
// edge and chain cannot be textured
{
b2EdgeShape* s = dynamic_cast<b2EdgeShape*>(sh);
VertexArray va(sf::Lines, 2);
va[0].position = B2toSF(b->GetWorldPoint(s->m_vertex1));
va[0].color = Color::White;
va[1].position = B2toSF(b->GetWorldPoint(s->m_vertex2));
va[1].color = Color::White;
win.draw(va, st);
break;
}
case b2Shape::e_chain: // chain is list of edges
{
b2ChainShape* s = dynamic_cast<b2ChainShape*>(sh);
for (int32 i = 0; i < s->GetChildCount(); ++i) // for each edges in chain
{
// draw child edge
b2EdgeShape edge;
s->GetChildEdge(&edge, i);
VertexArray va(sf::Lines, 2);
va[0].position = B2toSF(b->GetWorldPoint(edge.m_vertex1));
va[0].color = Color::White;
va[1].position = B2toSF(b->GetWorldPoint(edge.m_vertex2));
va[1].color = Color::White;
win.draw(va, st);
}
break;
}
}
}
}
Is it a bug in SFML? Or is my code wrong?