void OverlayTask::Update() { for (auto it = m_Selected.begin(), end = m_Selected.end(); it != end; ++it) { const auto& entity = *it; auto pos = entity->GetPosition(); pos.x = ToRenderUnits(pos.x), pos.y = ToRenderUnits(pos.y); pos += m_Offset; clan::Rectf box(clan::Sizef(50, 50)); box.translate(pos.x - box.get_width() * 0.5f, pos.y - box.get_height() * 0.5f); m_DebugDraw.DrawRectangle(box, GetColour(it)); } if (m_EditCam) { clan::Colorf rangeColour(1.0f, 0.6f, 0.6f, 0.95f); auto center = m_EditCam->GetPosition(); auto radius = ToRenderUnits(m_CamRange); clan::Rectf camRect(center.x - radius, center.y - radius, center.x + radius, center.y + radius); m_DebugDraw.DrawRectangle(camRect, rangeColour); } if (m_PolygonTool && m_PolygonTool->IsActive()) m_PolygonTool->Draw(); if (m_RectangleTool && m_RectangleTool->IsActive()) m_RectangleTool->Draw(); if (m_CircleTool && m_CircleTool->IsActive()) m_CircleTool->Draw(); }
GinsengSandbox::GinsengSandbox(Engine *engine) : engine(engine) { ModulePtr sensorModule(new Module()); utility::add_class<FootSensor>(*sensorModule, "FootSensor", { }, { {fun(&FootSensor::OnGround), "OnGround"} } ); engine->chai->add(sensorModule); accumulator = 0.f; sf::Vector2u wSize = engine->window.getSize(); gridView.reset(sf::FloatRect(wSize.x / -2.f, wSize.y / -2.f, wSize.x, wSize.y)); PhysicsWorld physicsWorld(b2Vec2(0.f, -9.81f * 8), 1.f / 60.f, 8, 3); physicsWorld.World->SetContactListener(new ContactListener()); DebugDraw* debugDraw = new DebugDraw(engine->window, 64.f); debugDraw->SetFlags(b2Draw::e_shapeBit); physicsWorld.World->SetDebugDraw(debugDraw); db.makeComponent(db.makeEntity(), physicsWorld); loadLevel("data/scripts/level/test.json"); EntID player = db.makeEntity(); db.makeComponent(player, DynamicBody(physicsWorld, 4.f, 0.9f, 1.f , 1.8f)); db.makeComponent(player, FootSensor(player.get<DynamicBody>().data().Body)); db.makeComponent(player, AIComponent{PlayerAI(player)}); engine->chai->add(var(player.get<FootSensor>().data()), "foot"); }
void SelectionDrawerTask::Update() { if (!fe_fzero(m_SelectionBox.get_width()) || !fe_fzero(m_SelectionBox.get_height())) { auto fillC = clan::Colorf::aquamarine; fillC.set_alpha(0.20f); m_DebugDraw.DrawRectangle(m_SelectionBox, clan::Colorf::white); m_DebugDraw.DrawSolidRectangle(m_SelectionBox, fillC); } }
void Arrow(ArrowSelection as, int dir, float angle, float scale, float offset2) { DebugDraw dbgDraw; glPushMatrix(); glTranslatef(settings.viewCenter.x + (extents.x - arrowOffset + arrowScale - offset2) * dir, settings.viewCenter.y, 0); glRotatef(angle, 0, 0, 1); glScalef(arrowScale * scale, arrowScale * scale, 1); dbgDraw.DrawArrow(lMouseDown && whichArrow == as ? arrowActiveColor : arrowPassiveColor); glPopMatrix(); }
void main() { auto &window = Window::instance(); auto &input = Input::instance(); auto &time = Time::instance(); window.init(); input.init(); time.init(); Asset::instance().loadTexture("Ship", "../textures/sprite.png"); //Factory::makeBall({ 40, 40 }, {10,10}, 400, 40)->rigidbody->addTorque(1000); // Factory::makeBall({ 80, 200 }, { 100,0}, 120, 120); //auto e = Factory::makeBall({ 720, 200 }, { }, 60, 1); Factory::makePlayer({ 400,350 }); DebugDraw debugDraw; RigidbodyDynamics rigidbodies; LifetimeSystem lifetimes; CollisionDetection collisioner; DynamicResolution dynamic; PlayerFlightSystem flightsystem; RenderSystem render; while (window.step()) { input.step(); time.step(); flightsystem.step(); rigidbodies.step(); lifetimes.step(); collisioner.step(); dynamic.step(); render.step(); debugDraw.step(); } time.term(); input.term(); window.term(); }
// Load your images here void LoadImages () { myWorld.SetDebugDraw (&draw); draw.SetFlags ( DebugDraw::e_shapeBit | DebugDraw::e_aabbBit | DebugDraw::e_jointBit | DebugDraw::e_centerOfMassBit | DebugDraw::e_pairBit); glutTimerFunc (1000 / 100.0, Update, 1); glutTimerFunc (2000 / 1000, Clear, 1); }
void DrawJoint( b2Joint *joint, b2Color color) { b2Vec2 anchor1, anchor2; anchor1 = joint->GetAnchorA(); anchor2 = joint->GetAnchorB(); glPointSize(5); glBegin(GL_POINTS); glVertex2f(anchor1.x,anchor1.y); glVertex2f(anchor2.x,anchor2.y); glEnd(); glPointSize(1); renderer.DrawSegment(anchor1, anchor2, color); }
// Draw a colored arrow rotated by angle, scaled by scale and at // the viewport relative position using DebugDraw. // With no transformation matrix applied, the arrow is drawn in box // area (3.5f, 3.5f) (see Arrow::k_size) and the overall bounding box // of the arrow is (-1.75f, -1.75f) to (1.75f, 1.75f). void Arrow::DrawArrow(const b2Color &color, const float32 angle, const float32 scale, const b2Vec2 &position) { static const b2Vec2 square[] = { b2Vec2(0.25f, 1.0f), b2Vec2(0.25f, -1.0f), b2Vec2(-1.75f, -1.0f), b2Vec2(-1.75f, 1.0f) }; static const b2Vec2 triangle[] = { b2Vec2(0.25f, 1.75f), b2Vec2(1.75f, 0.0f), b2Vec2(0.25f, -1.75f) }; // Build the transformation matrix. glPushMatrix(); glTranslatef(position.x, position.y, 0.0f); glRotatef(angle, 0.0f, 0.0f, 1.0f); glScalef(scale, scale, 1.0f); // Draw the arrow. DebugDraw dbgDraw; dbgDraw.DrawFlatPolygon(square, B2_ARRAY_SIZE(square), color); dbgDraw.DrawFlatPolygon(triangle, B2_ARRAY_SIZE(triangle), color); glPopMatrix(); }
//Rotina que chama os métodos de desenho da classe DebugDraw para desenhar os objetos da cena void DrawFixture(b2Fixture* fixture, b2Color color) { const b2Transform& xf = fixture->GetBody()->GetTransform(); switch (fixture->GetType()) { case b2Shape::e_circle: { b2CircleShape* circle = (b2CircleShape*)fixture->GetShape(); b2Vec2 center = b2Mul(xf, circle->m_p); float32 radius = circle->m_radius; renderer.DrawCircle(center, radius, color); } break; case b2Shape::e_polygon: { b2PolygonShape* poly = (b2PolygonShape*)fixture->GetShape(); int32 vertexCount = poly->m_count; b2Assert(vertexCount <= b2_maxPolygonVertices); b2Vec2 vertices[b2_maxPolygonVertices]; for (int32 i = 0; i < vertexCount; ++i) { vertices[i] = b2Mul(xf, poly->m_vertices[i]); } renderer.DrawPolygon(vertices, vertexCount, color); } break; case b2Shape::e_edge: { b2EdgeShape* edge = (b2EdgeShape*)fixture->GetShape(); int32 vertexCount; b2Vec2 vertices[b2_maxPolygonVertices]; int i=0; if (edge->m_hasVertex0) { vertices[i] = b2Mul(xf, edge->m_vertex0); i++; } vertices[i] = b2Mul(xf, edge->m_vertex1); i++; vertices[i] = b2Mul(xf, edge->m_vertex2); i++; if (edge->m_hasVertex3) { vertices[i] = b2Mul(xf, edge->m_vertex3); i++; } vertexCount = i; renderer.DrawPolygon(vertices, vertexCount, color); } break; } }
//Callback de desenho da GLUT, nela é chamada a rotina que chama o passo da simulação void SimulationLoop() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClearColor(1.0f, 1.0f, 1.0f, 1.0f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); //Chama a rotina que chama o passo da simulação RunBox2D(); //Define a cor do texto como preta b2Color color; color.r = 0.0; color.g = 0.0; color.b = 0.0; //Imprimindo dados da simulação int32 bodyCount = world->GetBodyCount(); int32 contactCount = world->GetContactCount(); int32 jointCount = world->GetJointCount(); renderer.DrawString(5, 15, color,"corpos/contatos/juntas = %d/%d/%d", bodyCount, contactCount, jointCount); renderer.DrawString(5, 30, color,"birds/pigs = %d/%d", birds.size(), pigs.size()-deadPigsCount); //Define a cor dos objetos como vermelha color.r = 1.0; color.g = 0.0; color.b = 0.0; b2Body *b; for(b = world->GetBodyList(); b; b=b->GetNext()){ renderer.DrawFixture(b->GetFixtureList(),color); } //Desenha o fundo EnableTextureInOpenGL(textures[2]); //a textura do fundo está na posição 2 DrawSprite(80,0,0,0,0); //Desenha os passarinhos color.r = 0.0; color.g = 1.0; color.b = 1.0; EnableTextureInOpenGL(textures[1]); //a textura do passaro está na posição 1 for (int i=0; i<birds.size(); i++) DrawShape(birds[i]->GetFixtureList(),color); //Desenha os pigs color.r = 0.0; color.g = 0.8; color.b = 0.0; EnableTextureInOpenGL(textures[0]); //a textura do passaro está na posição 0 for (int i=0; i<pigs.size(); i++) { if (pigs[i] != NULL) DrawShape(pigs[i]->GetFixtureList(),color); } //Desenhando o ponto de picking do mouse glPointSize(5); glBegin(GL_POINTS); glVertex2f(mouseWorld.x,mouseWorld.y); glEnd(); glPointSize(1); DesenhaLinhaGuia(); //Para desenhar os pontos de contatos color.r = 1.0; color.g = 0.0; color.b = 1.0; b2Contact * contact = world->GetContactList(); for (int i=0; i<world->GetContactCount(); i++) { b2WorldManifold worldManifold; contact->GetWorldManifold(&worldManifold); b2Vec2 p = worldManifold.points[0]; renderer.DrawPoint(p, 4, color); contact = contact->GetNext(); } //Depois de percorrer a lista de contatos, destruir os corpos dos pigs mortos for(int i=0; i < pigsToDieIndexes.size(); i++) { int pigIndex = pigsToDieIndexes[i]; if (pigs[pigIndex] != NULL) //desse jeito que eu fiz pode acontecer isso { world->DestroyBody(pigs[pigIndex]); pigs[pigIndex] = NULL; deadPigsCount++; } } glutSwapBuffers(); //O glutPostRedisplay() está sendo dada na rotina de callback de timer!! }
//Rotina que chama os métodos de desenho da classe DebugDraw para desenhar os objetos da cena void DrawShape(b2Fixture* fixture, b2Color color) { const b2Transform& xf = fixture->GetBody()->GetTransform(); switch (fixture->GetType()) { case b2Shape::e_circle: { b2CircleShape* circle = (b2CircleShape*)fixture->GetShape(); b2Vec2 center = b2Mul(xf, circle->m_p); float32 radius = circle->m_radius; b2Vec2 axis = b2Mul(xf.q, b2Vec2(1.0f, 0.0f)); float angle = xf.q.GetAngle(); renderer.DrawSolidCircle(center, radius, axis, color); DrawSprite((radius*1.44)*2,center.x,center.y,0.0,RadianosParaGraus(angle)); //este 1.44 foi um cálculo q eu fiz para encaixar... o ideal é que o circulo tenha o raio da imagem } break; case b2Shape::e_polygon: { b2PolygonShape* poly = (b2PolygonShape*)fixture->GetShape(); int32 vertexCount = poly->m_count; b2Assert(vertexCount <= b2_maxPolygonVertices); b2Vec2 vertices[b2_maxPolygonVertices]; for (int32 i = 0; i < vertexCount; ++i) { vertices[i] = b2Mul(xf, poly->m_vertices[i]); } renderer.DrawPolygon(vertices, vertexCount, color); } break; case b2Shape::e_edge: { b2EdgeShape* edge = (b2EdgeShape*)fixture->GetShape(); int32 vertexCount; b2Vec2 vertices[b2_maxPolygonVertices]; int i=0; if (edge->m_hasVertex0) { vertices[i] = b2Mul(xf, edge->m_vertex0); i++; } vertices[i] = b2Mul(xf, edge->m_vertex1); i++; vertices[i] = b2Mul(xf, edge->m_vertex2); i++; if (edge->m_hasVertex3) { vertices[i] = b2Mul(xf, edge->m_vertex3); i++; } vertexCount = i; renderer.DrawPolygon(vertices, vertexCount, color); } break; case b2Shape::e_chain: { b2ChainShape* chain = (b2ChainShape*)fixture->GetShape(); int32 count = chain->m_count; const b2Vec2* vertices = chain->m_vertices; b2Vec2 v1 = b2Mul(xf, vertices[0]); for (int32 i = 1; i < count; ++i) { b2Vec2 v2 = b2Mul(xf, vertices[i]); renderer.DrawSegment(v1, v2, color); renderer.DrawCircle(v1, 0.05f, color); v1 = v2; } } break; } }
void DebugDrawDir(DebugDraw &draw, const core::math::Vec4 &start, const core::math::Vec4 &dir, const core::math::Color &color) { const Color color1(color.r(), color.g(), color.b(), 0); const Color color2(color.r(), color.g(), color.b(), 255); draw.addLine(start, start + dir, color1, color2); }
DebugDraw* DebugDraw::create() { DebugDraw* draw = new DebugDraw(); draw->autorelease(); return draw; }
void DebugDrawMatrix(DebugDraw &draw, const core::math::Matrix44 &mat) { draw.addLine(mat.pos() + mat.left() * 0.5f, mat.pos() - mat.left() * 0.5f, Color(255, 0, 0, 255), Color(64, 0, 0, 255)); draw.addLine(mat.pos() + mat.up() * 0.5f, mat.pos() - mat.up() * 0.5f, Color(0, 255, 0, 255), Color(0, 64, 0, 255)); draw.addLine(mat.pos() + mat.at() * 0.5f, mat.pos() - mat.at() * 0.5f, Color(0, 0, 255, 255), Color(0, 0, 64, 255)); }
//Callback de desenho da GLUT, nela é chamada a rotina que chama o passo da simulação void SimulationLoop() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); //Chama a rotina que chama o passo da simulação RunBox2D(); //Define a cor dos objetos como vermelha b2Color color; color.r = 1.0; color.g = 0.0; color.b = 0.0; //Desenha os objetos b2Body *b; for(b = world->GetBodyList(); b; b=b->GetNext()) { DrawFixture(b->GetFixtureList(),color); } color.r = 1.0; color.g = 0.0; color.b = 1.0; if (distanceJoint) DrawJoint(distanceJoint,color); if (revoluteJoint1) DrawJoint(revoluteJoint1, color); if (revoluteJoint2) DrawJoint(revoluteJoint2, color); if (pJoint) DrawJoint(pJoint,color); if (gearJoint) DrawJoint(gearJoint,color); if (wheelJoint1) DrawJoint(wheelJoint1,color); if (wheelJoint2) DrawJoint(wheelJoint2,color); if (pulleyJoint) //Pulley joint { b2Vec2 anchor1 = pulleyJoint->GetAnchorA(); b2Vec2 anchor2 = pulleyJoint->GetAnchorB(); b2Vec2 ground1 = pulleyJoint->GetGroundAnchorA(); b2Vec2 ground2 = pulleyJoint->GetGroundAnchorB(); glBegin(GL_LINES); glVertex2f(anchor1.x,anchor1.y); glVertex2f(ground1.x,ground1.y); glVertex2f(anchor2.x,anchor2.y); glVertex2f(ground2.x,ground2.y); glVertex2f(ground1.x,ground1.y); glVertex2f(ground2.x,ground2.y); glEnd(); } //Desenha as forças aplicadas DesenhaForcasAplicadas(); //Desenhando o ponto de picking do mouse glPointSize(5); glBegin(GL_POINTS); glVertex2f(mouseWorld.x,mouseWorld.y); glEnd(); glPointSize(1); if(desenhaLinhaGuia == true) DesenhaLinhaGuia(); ostringstream aux1; //incluir sstream aux1 << "Comandos: r, R, p, P, g, w, t"; renderer.DrawString(10,20,aux1.str().c_str()); glutSwapBuffers(); }
void Game::Setup(void){ sf::WindowSettings s = this->app->GetSettings(); s.DepthBits = 24; s.StencilBits = 8; s.AntialiasingLevel = 4; this->app->PreserveOpenGLStates(true); this->app->SetFramerateLimit(60); glViewport(0,0,800,600); glEnable(GL_TEXTURE_RECTANGLE_EXT); glDisable( GL_LIGHTING ); /* glDisable(GL_DITHER); */ glDepthFunc(GL_LESS); glEnable(GL_DEPTH_TEST); // Setup a perspective projection glMatrixMode(GL_PROJECTION); glAlphaFunc(GL_GREATER, 0.5); glEnable(GL_ALPHA_TEST); glOrtho(0, 800, 600, 0, -100, 100); // Setup a perspective projection glMatrixMode(GL_MODELVIEW); // Displacement trick for exact pixelization glTranslatef(0.375, 0.375, 0); /* glTranslatef(0, 0, -5); */ // Box2D initialization b2Vec2 gravity(0, 9.8f); b2World *w = Utils::GetWorld(); w = new b2World(gravity); w->SetContinuousPhysics(true); this->world = w; Utils::SetWorld(w); // Ground definition b2BodyDef groundBodyDef; groundBodyDef.position.Set(0.0f / RATIO / 2, 0.0f / RATIO / 2); b2Body *groundBody = w->CreateBody(&groundBodyDef); b2EdgeShape groundEdge; b2FixtureDef boxShapeDef; boxShapeDef.shape = &groundEdge; groundEdge.Set(b2Vec2(0.0f, 600.0f / RATIO), b2Vec2(800.0f / RATIO, 600.0f / RATIO)); /* b2PolygonShape groundBox; */ /* groundBox.SetAsBox(800.0f / RATIO / 2, 600.0f / RATIO / 2); */ /* groundBody->CreateFixture(&groundBox, 1.0f); */ groundBody->CreateFixture(&boxShapeDef); // DebugDraw initialization if (DEBUG_DRAW) { DebugDraw *debug = new DebugDraw(); this->world->SetDebugDraw(debug); uint32 flags = 0; flags += 1 * b2Draw::e_shapeBit; flags += 1 * b2Draw::e_jointBit; /* flags += 1 * b2Draw::e_aabbBit; */ flags += 1 * b2Draw::e_pairBit; flags += 1 * b2Draw::e_centerOfMassBit; debug->SetFlags(flags); } }
void DebugDrawPoint(DebugDraw &draw, const core::math::Vec4 &point) { const Color white(255, 255, 255, 255); draw.addLine(point + core::math::G_UpVector * 0.5f, point - core::math::G_UpVector * 0.5f, white, white); draw.addLine(point + core::math::G_LeftVector * 0.5f, point - core::math::G_LeftVector * 0.5f, white, white); draw.addLine(point + core::math::G_AtVector * 0.5f, point - core::math::G_AtVector * 0.5f, white, white); }