Skip to content

Commit

Permalink
Revised compound test (#598)
Browse files Browse the repository at this point in the history
Compare different approaches to convex decomposition
  • Loading branch information
erincatto authored Apr 13, 2020
1 parent e94472d commit 4de0ea3
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 75 deletions.
4 changes: 4 additions & 0 deletions testbed/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,10 @@ int main(int, char**)

sprintf(buffer, "Box2D Testbed Version %d.%d.%d", b2_version.major, b2_version.minor, b2_version.revision);
g_mainWindow = glfwCreateWindow(g_camera.m_width, g_camera.m_height, buffer, NULL, NULL);

// Full screen for capture
// g_mainWindow = glfwCreateWindow(1920, 1080, buffer, glfwGetPrimaryMonitor(), NULL);

if (g_mainWindow == NULL)
{
fprintf(stderr, "Failed to open GLFW g_mainWindow.\n");
Expand Down
231 changes: 156 additions & 75 deletions testbed/tests/compound_shapes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
// SOFTWARE.

#include "test.h"
#include "imgui/imgui.h"

// TODO_ERIN test joints on compounds.
class CompoundShapes : public Test
{
public:
Expand All @@ -39,108 +39,189 @@ class CompoundShapes : public Test
body->CreateFixture(&shape, 0.0f);
}

// Table 1
{
b2CircleShape circle1;
circle1.m_radius = 0.5f;
circle1.m_p.Set(-0.5f, 0.5f);

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

for (int i = 0; i < 10; ++i)
{
float x = RandomFloat(-0.1f, 0.1f);
b2BodyDef bd;
bd.type = b2_dynamicBody;
bd.position.Set(x + 5.0f, 1.05f + 2.5f * i);
bd.angle = RandomFloat(-b2_pi, b2_pi);
b2Body* body = m_world->CreateBody(&bd);
body->CreateFixture(&circle1, 2.0f);
body->CreateFixture(&circle2, 0.0f);
}
b2BodyDef bd;
bd.type = b2_dynamicBody;
bd.position.Set(-15.0f, 1.0f);
m_table1 = m_world->CreateBody(&bd);

b2PolygonShape top;
top.SetAsBox(3.0f, 0.5f, b2Vec2(0.0f, 3.5f), 0.0f);

b2PolygonShape leftLeg;
leftLeg.SetAsBox(0.5f, 1.5f, b2Vec2(-2.5f, 1.5f), 0.0f);

b2PolygonShape rightLeg;
rightLeg.SetAsBox(0.5f, 1.5f, b2Vec2(2.5f, 1.5f), 0.0f);

m_table1->CreateFixture(&top, 2.0f);
m_table1->CreateFixture(&leftLeg, 2.0f);
m_table1->CreateFixture(&rightLeg, 2.0f);
}

// Table 2
{
b2PolygonShape polygon1;
polygon1.SetAsBox(0.25f, 0.5f);

b2PolygonShape polygon2;
polygon2.SetAsBox(0.25f, 0.5f, b2Vec2(0.0f, -0.5f), 0.5f * b2_pi);

for (int i = 0; i < 10; ++i)
{
float x = RandomFloat(-0.1f, 0.1f);
b2BodyDef bd;
bd.type = b2_dynamicBody;
bd.position.Set(x - 5.0f, 1.05f + 2.5f * i);
bd.angle = RandomFloat(-b2_pi, b2_pi);
b2Body* body = m_world->CreateBody(&bd);
body->CreateFixture(&polygon1, 2.0f);
body->CreateFixture(&polygon2, 2.0f);
}
b2BodyDef bd;
bd.type = b2_dynamicBody;
bd.position.Set(-5.0f, 1.0f);
m_table2 = m_world->CreateBody(&bd);

b2PolygonShape top;
top.SetAsBox(3.0f, 0.5f, b2Vec2(0.0f, 3.5f), 0.0f);

b2PolygonShape leftLeg;
leftLeg.SetAsBox(0.5f, 2.0f, b2Vec2(-2.5f, 2.0f), 0.0f);

b2PolygonShape rightLeg;
rightLeg.SetAsBox(0.5f, 2.0f, b2Vec2(2.5f, 2.0f), 0.0f);

m_table2->CreateFixture(&top, 2.0f);
m_table2->CreateFixture(&leftLeg, 2.0f);
m_table2->CreateFixture(&rightLeg, 2.0f);
}

// Spaceship 1
{
b2Transform xf1;
xf1.q.Set(0.3524f * b2_pi);
xf1.p = xf1.q.GetXAxis();
b2BodyDef bd;
bd.type = b2_dynamicBody;
bd.position.Set(5.0f, 1.0f);
m_ship1 = m_world->CreateBody(&bd);

b2Vec2 vertices[3];

b2PolygonShape triangle1;
vertices[0] = b2Mul(xf1, b2Vec2(-1.0f, 0.0f));
vertices[1] = b2Mul(xf1, b2Vec2(1.0f, 0.0f));
vertices[2] = b2Mul(xf1, b2Vec2(0.0f, 0.5f));
triangle1.Set(vertices, 3);

b2Transform xf2;
xf2.q.Set(-0.3524f * b2_pi);
xf2.p = -xf2.q.GetXAxis();

b2PolygonShape triangle2;
vertices[0] = b2Mul(xf2, b2Vec2(-1.0f, 0.0f));
vertices[1] = b2Mul(xf2, b2Vec2(1.0f, 0.0f));
vertices[2] = b2Mul(xf2, b2Vec2(0.0f, 0.5f));
triangle2.Set(vertices, 3);

for (int32 i = 0; i < 10; ++i)
{
float x = RandomFloat(-0.1f, 0.1f);
b2BodyDef bd;
bd.type = b2_dynamicBody;
bd.position.Set(x, 2.05f + 2.5f * i);
bd.angle = 0.0f;
b2Body* body = m_world->CreateBody(&bd);
body->CreateFixture(&triangle1, 2.0f);
body->CreateFixture(&triangle2, 2.0f);
}
b2PolygonShape left;
vertices[0].Set(-2.0f, 0.0f);
vertices[1].Set(0.0f, 4.0f / 3.0f);
vertices[2].Set(0.0f, 4.0f);
left.Set(vertices, 3);

b2PolygonShape right;
vertices[0].Set(2.0f, 0.0f);
vertices[1].Set(0.0f, 4.0f / 3.0f);
vertices[2].Set(0.0f, 4.0f);
right.Set(vertices, 3);

m_ship1->CreateFixture(&left, 2.0f);
m_ship1->CreateFixture(&right, 2.0f);
}

// Spaceship 2
{
b2PolygonShape bottom;
bottom.SetAsBox( 1.5f, 0.15f );
b2BodyDef bd;
bd.type = b2_dynamicBody;
bd.position.Set(15.0f, 1.0f);
m_ship2 = m_world->CreateBody(&bd);

b2Vec2 vertices[3];

b2PolygonShape left;
left.SetAsBox(0.15f, 2.7f, b2Vec2(-1.45f, 2.35f), 0.2f);
vertices[0].Set(-2.0f, 0.0f);
vertices[1].Set(1.0f, 2.0f);
vertices[2].Set(0.0f, 4.0f);
left.Set(vertices, 3);

b2PolygonShape right;
right.SetAsBox(0.15f, 2.7f, b2Vec2(1.45f, 2.35f), -0.2f);
vertices[0].Set(2.0f, 0.0f);
vertices[1].Set(-1.0f, 2.0f);
vertices[2].Set(0.0f, 4.0f);
right.Set(vertices, 3);

m_ship2->CreateFixture(&left, 2.0f);
m_ship2->CreateFixture(&right, 2.0f);
}
}

void Spawn()
{
// Table 1 obstruction
{
b2BodyDef bd;
bd.type = b2_dynamicBody;
bd.position.Set( 0.0f, 2.0f );
bd.position = m_table1->GetPosition();
bd.angle = m_table1->GetAngle();

b2Body* body = m_world->CreateBody(&bd);
body->CreateFixture(&bottom, 4.0f);
body->CreateFixture(&left, 4.0f);
body->CreateFixture(&right, 4.0f);

b2PolygonShape box;
box.SetAsBox(4.0f, 0.1f, b2Vec2(0.0f, 3.0f), 0.0f);

body->CreateFixture(&box, 2.0f);
}

// Table 2 obstruction
{
b2BodyDef bd;
bd.type = b2_dynamicBody;
bd.position = m_table2->GetPosition();
bd.angle = m_table2->GetAngle();

b2Body* body = m_world->CreateBody(&bd);

b2PolygonShape box;
box.SetAsBox(4.0f, 0.1f, b2Vec2(0.0f, 3.0f), 0.0f);

body->CreateFixture(&box, 2.0f);
}

// Ship 1 obstruction
{
b2BodyDef bd;
bd.type = b2_dynamicBody;
bd.position = m_ship1->GetPosition();
bd.angle = m_ship1->GetAngle();
bd.gravityScale = 0.0f;

b2Body* body = m_world->CreateBody(&bd);

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

body->CreateFixture(&circle, 2.0f);
}

// Ship 2 obstruction
{
b2BodyDef bd;
bd.type = b2_dynamicBody;
bd.position = m_ship2->GetPosition();
bd.angle = m_ship2->GetAngle();
bd.gravityScale = 0.0f;

b2Body* body = m_world->CreateBody(&bd);

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

body->CreateFixture(&circle, 2.0f);
}
}

void UpdateUI() override
{
ImGui::SetNextWindowPos(ImVec2(10.0f, 100.0f));
ImGui::SetNextWindowSize(ImVec2(200.0f, 100.0f));
ImGui::Begin("Controls", nullptr, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize);

if (ImGui::Button("Spawn"))
{
Spawn();
}

ImGui::End();
}

static Test* Create()
{
return new CompoundShapes;
}

b2Body* m_table1;
b2Body* m_table2;
b2Body* m_ship1;
b2Body* m_ship2;
};

static int testIndex = RegisterTest("Examples", "Compound Shapes", CompoundShapes::Create);

0 comments on commit 4de0ea3

Please sign in to comment.