Take the 2-minute tour ×

i'm using box2d for the first time, and i've set up my shapes via the hello world tutorial.

I am creating a box as so:

b2BodyDef bodyDef;
bodyDef.type = b2_kinematicBody;
bodyDef.position.Set(7.0f, 7.0f);
bodyDef.angle = 0;

m_body = m_world->CreateBody(&bodyDef);

b2PolygonShape shape;
shape.SetAsBox(1.5f, 0.5f);

b2FixtureDef fixtureDef;
fixtureDef.shape = &shape;
fixtureDef.density = 1.0f;


Now I am ready to render this box, so I call:

b2Vec2 pos = m_body->GetPosition();

At this point, I need to call m_renderTarget->SetTransform() using the values of pos, but I can't figure out how to render the box correctly. I have tried:

m_renderTarget->SetTransform(D2D1::Matrix3x2F::Translation(pos.x * 30, pos.y * 30));
m_renderTarget->DrawRectangle(D2D1::RectF(0.0f, 0.0f, 3.0f * 30.0f, 1.0f * 30.0f), m_brush);

And the circle:

bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(7.0f, 1.0f);

m_circleBody = m_world->CreateBody(&bodyDef);

b2CircleShape circleShape;
circleShape.m_p.Set(0.0f, 0.0f);
circleShape.m_radius = 0.5f;

fixtureDef.shape = &circleShape;


And to render the circle:

    b2Vec2 circlePos = m_circleBody->GetPosition();

    mpRenderTarget->SetTransform(D2D1::Matrix3x2F::Translation(circlePos.x * 30.0f, circlePos.y * 30.0f));

    mpRenderTarget->DrawEllipse(D2D1::Ellipse(D2D1::Point2F(0.0f, 0.0f), 30.0f, 30.0f), m_brush);
share|improve this question
What do you mean by no luck? Nothing is drawn? Something is drawn, but its wrong? –  Patrick Lafferty Nov 2 '11 at 5:47
The shape is drawn but the physics aren't correct (other dynamic shapes fall through an area which should be solid). –  user987280 Nov 2 '11 at 6:20
Pick one object that appears to be falling through, and post the physics {position, dimensions} as well as graphics {translation, rectangle coordinates} –  Patrick Lafferty Nov 2 '11 at 6:30
Edited with code for the dynamic shape. –  user987280 Nov 2 '11 at 6:40
Does the circle appear split in half on the rectangle's right side? –  Patrick Lafferty Nov 2 '11 at 7:03
show 1 more comment

1 Answer

up vote 0 down vote accepted

You aren't drawing your rectangle properly centered. The rectangle's center is the top left.


In order to center it properly, you should have Left = -Right, Top = -Bottom like so

m_renderTarget->DrawRectangle(D2D1::RectF(-1.5 * 30.f, -0.5 * 30.f, 1.5f * 30.0f, 0.5f * 30.0f), m_brush);

Here's a diagram explaining why centering is important:

diagram illustrating the importance of centering

Physically you represent both shapes properly, but graphically you unknowingly added an offset to the rectangle. Also, your scale is off: you assume 1 = 30 pixels for the rectangle, and 0.5 = 30 pixels for the circle. Consistency is key in simulations, so you should lower your D2D1::Ellipse's radii to 15 each.

share|improve this answer
It seems that you are correct, but also that I need to change the radius to 15.0f in the DrawEllipse call. –  user987280 Nov 2 '11 at 7:42
add comment

Your Answer


