void Bird::launch(b2Vec2 velocity) { invulnerability=false; launched = true; g_size = QSizeF(g_worldsize.height()*ratio,g_worldsize.height()*ratio); QPointF mapped; mapped.setX((g_pixmap.x()+g_pixmap.pixmap().width()/2)/g_windowsize.width()*g_worldsize.width()); mapped.setY((1.0f-(g_pixmap.y()+g_pixmap.pixmap().height()/2)/g_windowsize.height())*g_worldsize.height()); // Create Body b2BodyDef bodydef; bodydef.type = b2_dynamicBody; bodydef.position.Set(mapped.x(),mapped.y()); bodydef.userData = this; g_body = g_world->CreateBody(&bodydef); b2CircleShape bodyshape; bodyshape.m_radius = g_worldsize.height()*ratio/2; b2FixtureDef fixturedef; fixturedef.shape = &bodyshape; fixturedef.density = BIRD_DENSITY; fixturedef.friction = BIRD_FRICTION; fixturedef.restitution = BIRD_RESTITUTION; g_body->CreateFixture(&fixturedef); g_body->SetAngularDamping(1); g_body->SetLinearVelocity(velocity); connect(g_timer, SIGNAL(timeout()), this,SLOT(paint())); connect(g_timer, SIGNAL(timeout()), this, SLOT(checkPos())); connect(g_timer, SIGNAL(timeout()), this, SLOT(checkVelocity())); }
void moveEntities(float dt, const Map* map, const std::vector<Entity*>& entities, CollisionData& coldata) { // Do not affect "non-physics" entities std::vector<Entity*> physicsEntities; for (size_t i=0; i<entities.size(); ++i) { if (entities[i]->getBehavior()->getSlot<PhysicsBehaviorSlot>()) { physicsEntities.push_back(entities[i]); } } Vec2f newVel; for (size_t i=0; i < physicsEntities.size(); ++i) { PhysicsBehaviorSlot* phys = physicsEntities[i]->getBehavior()->getSlot<PhysicsBehaviorSlot>(); physicsEntities[i]->setNextWithCurrent(); newVel = physicsEntities[i]->getVel() + getAccel(physicsEntities[i]) * dt; checkVelocity(newVel, phys); physicsEntities[i]->setNextVel(newVel); phys->inAir = true; //the<VisDebug>().addSegment(physicsEntities[i]->getPos(),physicsEntities[i]->getPos() + physicsEntities[i]->getVel() * dt, Vec3f(1,0,1)); } // Find collisions for all entities, and find out where they need to move (by setting next pos) resolveCollisionsAndMove(dt,map,physicsEntities); // Officially move the entities by making their next positions the current ones, this is needed to // ensure that all entities collide against the same setup. for (size_t i=0; i < physicsEntities.size(); ++i) { physicsEntities[i]->setCurrentWithNext(); //the<VisDebug>().addSegment(physicsEntities[i]->getPos(),physicsEntities[i]->getPos() + physicsEntities[i]->getVel() * dt, Vec3f(0,0,0.5)); } }