通过 direct2d 渲染 box2d 形状

发布于 2024-12-13 17:36:11 字数 1456 浏览 1 评论 0原文

我是第一次使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吃颗糖壮壮胆 2024-12-20 17:36:11

您没有正确居中绘制矩形。矩形的中心位于左上角。

m_renderTarget->DrawRectangle(D2D1::RectF(0.0f, 0.0f, 3.0f * 30.0f, 1.0f * 30.0f), m_brush);

为了正确居中,您应该像这样设置 Left = -Right, Top = -Bottom

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

下面是解释为什么居中很重要的图表:

说明居中重要性的图表

从物理上讲,您正确地表示了这两个形状,但从图形上讲,您不知不觉地向矩形添加了偏移量。另外,您的比例尺已关闭:您假设矩形为 1 = 30 像素,圆形为 0.5 = 30 像素。一致性是模拟的关键,因此您应该将 D2D1::Ellipse 的半径分别降低到 15。

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

m_renderTarget->DrawRectangle(D2D1::RectF(0.0f, 0.0f, 3.0f * 30.0f, 1.0f * 30.0f), m_brush);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文