#ifndef POLY_SHAPES_H #define POLY_SHAPES_H class PolyShapesCallback : public b2QueryCallback { public: enum { e_maxCount = 4 }; PolyShapesCallback() { m_count = 0; } void DrawFixture(b2Fixture* fixture) { b2Color color(0.95f, 0.95f, 0.6f); } bool ReportFixture(b2Fixture* fixture) { if (m_count == e_maxCount) { return false; } b2Body* body = fixture->GetBody(); b2Shape* shape = fixture->GetShape(); bool overlap = b2TestOverlap(shape, 0, &m_circle, 0, body->GetTransform(), m_transform); if (overlap) { DrawFixture(fixture); ++m_count; } return true; } b2CircleShape m_circle; b2Transform m_transform; b2Draw* m_debugDraw; int32 m_count; }; class PolyShapes : public Test { public: enum { e_maxBodies = 256 }; PolyShapes() { //'ziemia' - podloze { b2BodyDef bd; b2Body* ground = m_world->CreateBody(&bd); b2EdgeShape shape; shape.Set(b2Vec2(-40.0f, 0.0f), b2Vec2(40.0f, 0.0f)); ground->CreateFixture(&shape, 0.0f); } //ksztalty { b2Vec2 vertices[3]; vertices[0].Set(-0.5f, 0.0f); vertices[1].Set(0.5f, 0.0f); vertices[2].Set(0.0f, 1.5f); m_polygons[0].Set(vertices, 3); } { b2Vec2 vertices[3]; vertices[0].Set(-0.1f, -1.0f); vertices[1].Set(0.1f, -3.0f); vertices[2].Set(0.0f, 1.5f); m_polygons[1].Set(vertices, 3); } { float32 w = 1.0f; float32 b = w / (2.0f + b2Sqrt(2.0f)); float32 s = b2Sqrt(2.0f) * b; b2Vec2 vertices[8]; vertices[0].Set(1.5f * s, 0.0f); vertices[1].Set(1.5f * w, b); vertices[2].Set(1.5f * w, b + s); vertices[3].Set(1.5f * s, w); vertices[4].Set(-1.5f * s, w); vertices[5].Set(-1.5f * w, b + s); vertices[6].Set(-1.5f * w, b); vertices[7].Set(-1.5f * s, 2.0f); m_polygons[2].Set(vertices, 8); } { m_polygons[3].SetAsBox(1.0f, 1.0f); } { m_circle.m_radius = 2.5f; } m_bodyIndex = 0; memset(m_bodies, 0, sizeof(m_bodies)); } //glowna funkcja tworzaca ksztalty void Create(int32 index) { if (m_bodies[m_bodyIndex] != NULL) { m_world->DestroyBody(m_bodies[m_bodyIndex]); m_bodies[m_bodyIndex] = NULL; } b2BodyDef bd; bd.type = b2_dynamicBody; float32 x = RandomFloat(-2.0f, 2.0f); bd.position.Set(x, 10.0f); bd.angle = RandomFloat(-b2_pi, b2_pi); if (index == 4) { bd.angularDamping = 0.02f; } m_bodies[m_bodyIndex] = m_world->CreateBody(&bd); if (index < 4) { b2FixtureDef fd; fd.shape = m_polygons + index; fd.density = 1.0f; fd.friction = 0.3f; m_bodies[m_bodyIndex]->CreateFixture(&fd); } else { b2FixtureDef fd; fd.shape = &m_circle; fd.density = 1.0f; fd.friction = 0.3f; m_bodies[m_bodyIndex]->CreateFixture(&fd); } m_bodyIndex = (m_bodyIndex + 1) % e_maxBodies; } void Keyboard(unsigned char key) { // wybor ksztaltu switch (key) { case '1': case '2': case '3': case '4': case '5': Create(key - '1'); break; } } void Step(Settings* settings) { Test::Step(settings); PolyShapesCallback callback; callback.m_transform.SetIdentity(); callback.m_debugDraw = &m_debugDraw; m_debugDraw.DrawString(5, m_textLine, "Napisz 1-5 aby uzyskac rozne ksztalty"); m_textLine += DRAW_STRING_NEW_LINE; } static Test* Create() { return new PolyShapes; } int32 m_bodyIndex; b2Body* m_bodies[e_maxBodies]; b2PolygonShape m_polygons[4]; b2CircleShape m_circle; }; #endif