Here's ee's debug draw, slightly modified and commented, it's kind of quick and dirty, it's a
debug tool after all, I hope I didn't make a mistake somewhere,
it assumes you flipped the Y axis to match SFML's convention:
class EEDebugDraw3 : public b2Draw
{
private:
sf::RenderTarget * m_target;
//inliners for colour and point conversions
inline sf::Color EEColor(const b2Color& gCol)
{
return sf::Color(static_cast<sf::Uint8>(255*gCol.r),
static_cast<sf::Uint8>(255*gCol.g),
static_cast<sf::Uint8>(255*gCol.b));
}
inline sf::Vector2f EEVector(const b2Vec2& gVec){return sf::Vector2f(gVec.x*pixmeters,gVec.y*pixmeters);}
const float pixmeters,radegrees;//constants for point and degree conversions
public:
EEDebugDraw3(void);
virtual ~EEDebugDraw3(void){};
void LinkTarget(sf::RenderTarget& gtarget);
virtual void DrawPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color);
virtual void DrawSolidPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color);
virtual void DrawCircle(const b2Vec2& center, float32 radius, const b2Color& color);
virtual void DrawSolidCircle(const b2Vec2& center, float32 radius, const b2Vec2& axis, const b2Color& color);
virtual void DrawSegment(const b2Vec2& p1, const b2Vec2& p2, const b2Color& color);
virtual void DrawTransform(const b2Transform &xf){};
};
#include "EEDebugDraw3.h"
EEDebugDraw3::EEDebugDraw3(void):
pixmeters(32.f),//arbitrary value dependant on the program needs
radegrees(57.2957795f),//degrees per radian,a physical constant
m_target(0x0)
{
AppendFlags(static_cast<uint32>(~0));//set all drawing bits to 1(not all 32 are relevant but it's ok)
}
void EEDebugDraw3::LinkTarget(sf::RenderTarget& gtarget)
{
m_target=>arget;
}
void EEDebugDraw3::DrawPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color)
{
sf::ConvexShape shape;
shape.setOutlineColor(EEColor(color));
shape.setOutlineThickness(1);
shape.setFillColor(sf::Color::Transparent);
shape.setPointCount(vertexCount);
for(int i=0;i<vertexCount;++i)
{shape.setPoint(i,EEVector(vertices[i]));}
m_target->draw(shape);
}
void EEDebugDraw3::DrawSolidPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color)
{
sf::ConvexShape shape;
shape.setFillColor(EEColor(color));
shape.setPointCount(vertexCount);
for(int i=0;i<vertexCount;++i)
{shape.setPoint(i,EEVector(vertices[i]));}
m_target->draw(shape);
}
void EEDebugDraw3::DrawCircle(const b2Vec2& center, float32 radius, const b2Color& color)
{
sf::CircleShape shape;
shape.setOutlineColor(EEColor(color));
shape.setOutlineThickness(1);
shape.setFillColor(sf::Color::Transparent);
shape.setRadius(radius*pixmeters);
shape.setOrigin(sf::Vector2f(radius*pixmeters,radius*pixmeters));//set origin to middle or position setter below would not work correctly
shape.setPosition(EEVector(center));
m_target->draw(shape);
}
void EEDebugDraw3::DrawSolidCircle(const b2Vec2& center, float32 radius, const b2Vec2& axis, const b2Color& color)
{
sf::CircleShape shape;
shape.setFillColor(EEColor(color));
shape.setRadius(radius*pixmeters);
shape.setOrigin(sf::Vector2f(radius*pixmeters,radius*pixmeters));
shape.setPosition(EEVector(center));
m_target->draw(shape);
}
void EEDebugDraw3::DrawSegment(const b2Vec2& p1, const b2Vec2& p2, const b2Color& color)
{
sf::Vertex line[2];//const sized c styled array, safe enough in here
line[0].color=EEColor(color);
line[0].position=EEVector(p1);
line[1].color=EEColor(color);
line[1].position=EEVector(p2);
m_target->draw(line,2,sf::Lines);
}
Example:
{
sf::RenderWindow App(sf::VideoMode(500,500),"Bug Draw");//window setup
App.setFramerateLimit(60);//important because steps are linked to fps for this example,
//construction and linkage to window
EEDebugDraw3 debugdraw;
debugdraw.LinkTarget(App);
//construction and linkage to debug draw
b2World world(b2Vec2(0.f,9.8f));
world.SetDebugDraw(&debugdraw);
//sample body
b2BodyDef bdef;
bdef.type=b2_dynamicBody;
b2FixtureDef fdef;
b2PolygonShape sha;
sha.SetAsBox(1.f,1.f);
fdef.shape=&sha;
bdef.position=b2Vec2(4.f,0.f);
world.CreateBody(&bdef)->CreateFixture(&fdef);
while(1)
{
App.clear();
world.Step(1.f/60.f,8,3);
world.DrawDebugData();
App.display();
}
}