|
|
|
|
@ -51,7 +51,7 @@ namespace lunarium
|
|
|
|
|
// For step 1 we create the ground body. For this we need a body definition.
|
|
|
|
|
// With the body definition we specify the initial position of the ground body.
|
|
|
|
|
b2BodyDef groundBodyDef;
|
|
|
|
|
groundBodyDef.position.Set(30.0f, 50.0f);
|
|
|
|
|
groundBodyDef.position.Set(60.0f, 50.0f);
|
|
|
|
|
|
|
|
|
|
// For step 2 the body definition is passed to the world object to create the ground body.
|
|
|
|
|
// The world object does not keep a reference to the body definition. Bodies are static by default.
|
|
|
|
|
@ -69,6 +69,13 @@ namespace lunarium
|
|
|
|
|
// to the body without creating a fixture definition.
|
|
|
|
|
mpGroundBody->CreateFixture(mpGroundBox, 0.0f);
|
|
|
|
|
|
|
|
|
|
// Create a second ground body
|
|
|
|
|
groundBodyDef.position.Set(20.0f, 70.0f);
|
|
|
|
|
mpGroundBody2 = mb2World->CreateBody(&groundBodyDef);
|
|
|
|
|
mpGroundBox2 = new b2PolygonShape;
|
|
|
|
|
mpGroundBox2->SetAsBox(30.0f, 5.0f);
|
|
|
|
|
mpGroundBody2->CreateFixture(mpGroundBox2, 0.0f);
|
|
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// So now we have a ground body. We can use the same technique to create a dynamic body.
|
|
|
|
|
// The main difference, besides dimensions, is that we must establish the dynamic body's mass properties.
|
|
|
|
|
@ -77,7 +84,7 @@ namespace lunarium
|
|
|
|
|
// at construction time to make the body dynamic.
|
|
|
|
|
b2BodyDef bodyDef;
|
|
|
|
|
bodyDef.type = b2_dynamicBody; // Caution: You must set the body type to b2_dynamicBody if you want the body to move in response to forces.
|
|
|
|
|
bodyDef.position.Set(9.0f, 4.0f);
|
|
|
|
|
bodyDef.position.Set(39.0f, 4.0f);
|
|
|
|
|
mpDynamicBody = mb2World->CreateBody(&bodyDef);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -118,39 +125,45 @@ namespace lunarium
|
|
|
|
|
|
|
|
|
|
b2AABB groundbox;
|
|
|
|
|
mpGroundBox->ComputeAABB(&groundbox, mpGroundBody->GetTransform(), 0);
|
|
|
|
|
// float hwidth = groundbox.GetExtents().x;
|
|
|
|
|
// float hheight = groundbox.GetExtents().y;
|
|
|
|
|
// float left = (groundbox.GetCenter().x - hwidth);
|
|
|
|
|
// float top = (groundbox.GetCenter().y - hheight);
|
|
|
|
|
// float right = left + hwidth * 2;
|
|
|
|
|
// float bot = top + hheight * 2;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Rectangle groundRect(groundbox);
|
|
|
|
|
g->DrawFilledBox(groundRect.LeftTop() * scaleFactor,
|
|
|
|
|
groundRect.RightBottom() * scaleFactor,
|
|
|
|
|
Color(0.0f, 1.0f, 0.0f, 1.0f));
|
|
|
|
|
|
|
|
|
|
b2AABB groundbox2;
|
|
|
|
|
mpGroundBox2->ComputeAABB(&groundbox2, mpGroundBody2->GetTransform(), 0);
|
|
|
|
|
|
|
|
|
|
Rectangle groundRect2(groundbox2);
|
|
|
|
|
g->DrawFilledBox(groundRect2.LeftTop() * scaleFactor,
|
|
|
|
|
groundRect2.RightBottom() * scaleFactor,
|
|
|
|
|
Color(0.0f, 1.0f, 0.0f, 1.0f));
|
|
|
|
|
|
|
|
|
|
b2AABB dynbox;
|
|
|
|
|
mpDynamicBox->ComputeAABB(&dynbox, mpDynamicBody->GetTransform(), 0);
|
|
|
|
|
b2Transform transform;
|
|
|
|
|
transform.p = mpDynamicBody->GetTransform().p;
|
|
|
|
|
transform.q = b2Rot(0.0f);
|
|
|
|
|
mpDynamicBox->ComputeAABB(&dynbox, transform, 0);
|
|
|
|
|
Rectangle dynRect(dynbox);
|
|
|
|
|
float angle = mpDynamicBody->GetAngle();
|
|
|
|
|
g->DrawFilledBox(dynRect.LeftTop() * scaleFactor,
|
|
|
|
|
dynRect.RightBottom() * scaleFactor,
|
|
|
|
|
Color(0.0f, 0.0f, 1.0f, 1.0f));
|
|
|
|
|
Color(0.0f, 0.0f, 1.0f, 1.0f), angle);
|
|
|
|
|
|
|
|
|
|
// Debug info
|
|
|
|
|
char str[256] = { 0 };
|
|
|
|
|
sprintf(str, "GroundBox: (%f, %f) (%f, %f)",
|
|
|
|
|
groundRect.LeftTop().x * scaleFactor, groundRect.LeftTop().y * scaleFactor,
|
|
|
|
|
groundRect.RightBottom().x * scaleFactor, groundRect.RightBottom().y * scaleFactor);
|
|
|
|
|
g->DrawString(str, Rectangle(10.0f, 10.0f, 800.0f, 30.0f), Color(0.75f, 0.85f, 0.5f, 1.0f), 0.5f);
|
|
|
|
|
g->DrawString(str, Rectangle(10.0f, 10.0f, 800.0f, 30.0f), Color(0.75f, 0.85f, 0.5f, 1.0f), 0.4f);
|
|
|
|
|
|
|
|
|
|
sprintf(str, "GroundBox Half Size: (%f, %f)", groundRect.Width, groundRect.Height);
|
|
|
|
|
g->DrawString(str, Rectangle(10.0f, 35.0f, 800.0f, 30.0f), Color(0.75f, 0.85f, 0.5f, 1.0f), 0.5f);
|
|
|
|
|
g->DrawString(str, Rectangle(10.0f, 35.0f, 800.0f, 30.0f), Color(0.75f, 0.85f, 0.5f, 1.0f), 0.4f);
|
|
|
|
|
|
|
|
|
|
sprintf(str, "DynBox: (%f, %f) (%f, %f)",
|
|
|
|
|
sprintf(str, "DynBox: (%f, %f) (%f, %f), Angle: %f",
|
|
|
|
|
dynRect.LeftTop().x * scaleFactor, dynRect.LeftTop().y * scaleFactor,
|
|
|
|
|
dynRect.RightBottom().x * scaleFactor, dynRect.RightBottom().y * scaleFactor);
|
|
|
|
|
g->DrawString(str, Rectangle(10.0f, 60.0f, 800.0f, 100.0f), Color(0.75f, 0.85f, 0.5f, 1.0f), 0.5f);
|
|
|
|
|
dynRect.RightBottom().x * scaleFactor, dynRect.RightBottom().y * scaleFactor, angle);
|
|
|
|
|
g->DrawString(str, Rectangle(10.0f, 60.0f, 1000.0f, 100.0f), Color(0.75f, 0.85f, 0.5f, 1.0f), 0.4);
|
|
|
|
|
|
|
|
|
|
int ww, wh;
|
|
|
|
|
Core::MainWindow().GetFramebufferSize(&ww, &wh);
|
|
|
|
|
|