void Particle::advance(int phase){ if(active()) { QVector2D force; foreach(Planet* planet, gameScene()->planets()) { QVector2D r = QVector2D(this->position() - planet->position()); // save the length and lengthSquared to memory to avoid recalculations (with square roots!) // double distance = r.length(); double distanceSquared = r.lengthSquared(); QVector2D rn = r.normalized(); if(distanceSquared != 0) { QVector2D gravity = - rn * GravityConstant * planet->mass() / distanceSquared; force += gravity; if (r.lengthSquared() < planet->radius()*planet->radius()) { //Vj: Temporary fix while testing :O) planet->collideWithParticle(); gameScene()->removeParticle(this); return; } } } QVector2D acceleration = force / 1.0; _velocity += acceleration * gameScene()->dt(); _position += _velocity * gameScene()->dt(); resized(); }
void PhysicalItem::createBody() { if (body_ != nullptr) throw new std::logic_error("A body has already been created."); b2BodyDef bodyDef; defineBody(bodyDef); bodyDef.type = b2_dynamicBody; bodyDef.userData = reinterpret_cast<void*>(this); // For CollisionCallback bodyDef.position = gameScene()->mapToWorld(scenePos()); this->body_ = gameScene()->world()->CreateBody(&bodyDef); // Copies content of bodyDef b2FixtureDef fixtureDef; defineFixture(fixtureDef); fixtureDef.shape = createShape(); body_->CreateFixture(&fixtureDef); // Deep copies fixtureDef. delete fixtureDef.shape; }
void MenusScene::SwitchToGame() { std::vector<ProbenderData> contestantData; KinectBody* body = player1Nav.GetBody(); Player1Data.BaseAttributes.SetAllAttributes(10, 10, 10, 10, 10, 10); Player2Data.BaseAttributes.SetAllAttributes(10, 10, 10, 10, 10, 10); if(body) Player1Data.BodyID = -1;// body->GetBodyID(); else Player1Data.BodyID = -1; body = player2Nav.GetBody(); if(body) Player2Data.BodyID = -1;// body->GetBodyID(); else Player2Data.BodyID = -1; contestantData.push_back(Player1Data); contestantData.push_back(Player2Data); std::shared_ptr<GameScene> gameScene(new GameScene(owningManager, Ogre::Root::getSingletonPtr(), "Probending Arena", contestantData)); owningManager->FlagSceneSwitch(gameScene, true); gameScene.reset(); }
void PhysicalItem::advance(int phase) { // phase 0: The scene is about to advance // phase 1: The scene is advancing if (phase == 0) return; if (body_ != nullptr and body_->IsAwake()) { setPos(gameScene()->mapFromWorld(body_->GetPosition())); setRotation(qRadiansToDegrees(body_->GetAngle())); } QGraphicsItem::advance(phase); // Advance children }
void PhysicalItem::destroyBody() { if (body_ == nullptr) return; // Not created yet or already destroyed gameScene()->world()->DestroyBody(body_); body_ = nullptr; }