通过 direct2d 渲染 box2d 形状
我是第一次使用 box2d,并且我已经通过 hello world 教程设置了我的形状。
我正在创建一个盒子:
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;
m_body->CreateFixture(&fixtureDef);
现在我准备渲染这个盒子,所以我调用:
b2Vec2 pos = m_body->GetPosition();
此时,我需要使用 pos 的值调用 m_renderTarget->SetTransform() ,但我不知道如何正确渲染盒子。我尝试过:
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);
和圆圈:
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;
m_circleBody->CreateFixture(&fixtureDef);
并渲染圆圈:
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);
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;
m_body->CreateFixture(&fixtureDef);
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;
m_circleBody->CreateFixture(&fixtureDef);
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您没有正确居中绘制矩形。矩形的中心位于左上角。
为了正确居中,您应该像这样设置 Left = -Right, Top = -Bottom
下面是解释为什么居中很重要的图表:
从物理上讲,您正确地表示了这两个形状,但从图形上讲,您不知不觉地向矩形添加了偏移量。另外,您的比例尺已关闭:您假设矩形为 1 = 30 像素,圆形为 0.5 = 30 像素。一致性是模拟的关键,因此您应该将 D2D1::Ellipse 的半径分别降低到 15。
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
Here's a diagram explaining why centering is important:
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.