我第一次使用box2d,並且通過hello world教程設置了我的形狀。通過direct2d渲染box2d形狀
我創建一個框,因此:
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();
在這一點上,我需要調用m_renderTarget-> SetTransform()使用pos的值,但我無法弄清楚如何正確渲染框。我曾嘗試:
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);
你是什麼意思沒有運氣?什麼都沒有繪製?東西被吸引,但它的錯誤? –
繪製了形狀,但物理屬性不正確(其他動態形狀會穿過應該是實心的區域)。 – user987280
選取一個看似正在落下的物體,併發布物理{位置,尺寸}以及圖形{平移,矩形座標} –