void GameplayState::update(sf::Time elapsedTime) { servLoc.getProfiler()->start("GameplayState: update"); //physics updateObjects(elapsedTime); resolveCollisions(); cleanForces(); resolveExistance(); if(!player->exist) { servLoc.getLogger()->log(POS, "Player doesn't exist(destroyed)."); servLoc.getEngine()->popState(); return; } playerHP->update(player->hp); score->text.setString(ezo::string::format("Score: %u", player->score).c()); servLoc.getRender()->getWindow()->setView(sf::View(player->representation.getPosition(), servLoc.getRender()->getWindow()->getDefaultView().getSize())); servLoc.getProfiler()->stop(); }
void Player::update(float frameTime, const Level& level, char input, bool jump) { // Update position m_pos += frameTime * m_velocity; // Update velocity based on input if (input == 'a') { m_velocity -= glm::vec2(m_acceleration.x, 0); if (m_velocity.x < -m_maxVelocity.x) m_velocity.x = -m_maxVelocity.x; } else if (input == 'd') { m_velocity += glm::vec2(m_acceleration.x, 0); if (m_velocity.x > m_maxVelocity.x) m_velocity.x = m_maxVelocity.x; } else { // Dampen movement if not pressing keys if (m_velocity.x > 0 && m_velocity.x > m_acceleration.x) m_velocity -= glm::vec2(m_acceleration.x, 0); else if (m_velocity.x < 0 && m_velocity.x < m_acceleration.x) m_velocity += glm::vec2(m_acceleration.x, 0); else m_velocity = glm::vec2(0, m_velocity.y); } bool isGrounded = false; // Check for collisions resolveCollisions(level, isGrounded); // Gravity if (!isGrounded) { const float GRAVITY_POWA = 1.5f; m_velocity -= glm::vec2(0, GRAVITY_POWA); } else { // Can jump m_lastGroundedPosition = m_pos; m_velocity.y = 0.0f; if (jump) { //m_pos.y += m_acceleration.y; m_velocity.y += m_acceleration.y * 20; } } // printf("On Ground %d\n", (int)isGrounded); }
void PhysicsSimulation::stepForward(float deltaTime, float minError, int maxIterations, quint64 maxUsec) { ++_frameCount; if (!_ragdoll) { return; } quint64 now = usecTimestampNow(); quint64 startTime = now; quint64 expiry = startTime + maxUsec; moveRagdolls(deltaTime); enforceContacts(); int numDolls = _otherRagdolls.size(); { PerformanceTimer perfTimer("enforce"); _ragdoll->enforceConstraints(); for (int i = 0; i < numDolls; ++i) { _otherRagdolls[i]->enforceConstraints(); } } bool collidedWithOtherRagdoll = false; int iterations = 0; float error = 0.0f; do { collidedWithOtherRagdoll = computeCollisions() || collidedWithOtherRagdoll; updateContacts(); resolveCollisions(); { // enforce constraints PerformanceTimer perfTimer("enforce"); error = _ragdoll->enforceConstraints(); for (int i = 0; i < numDolls; ++i) { error = glm::max(error, _otherRagdolls[i]->enforceConstraints()); } } applyContactFriction(); ++iterations; now = usecTimestampNow(); } while (_collisions.size() != 0 && (iterations < maxIterations) && (error > minError) && (now < expiry)); // the collisions may have moved the main ragdoll from the simulation center // so we remove this offset (potentially storing it as movement of the Ragdoll owner) _ragdoll->removeRootOffset(collidedWithOtherRagdoll); // also remove any offsets from the other ragdolls for (int i = 0; i < numDolls; ++i) { _otherRagdolls[i]->removeRootOffset(false); } pruneContacts(); }
void PhysicsSimulation::stepForward(float deltaTime, float minError, int maxIterations, quint64 maxUsec) { ++_frameCount; if (!_ragdoll) { return; } quint64 now = usecTimestampNow(); quint64 startTime = now; quint64 expiry = startTime + maxUsec; moveRagdolls(deltaTime); buildContactConstraints(); int numDolls = _otherRagdolls.size(); { PerformanceTimer perfTimer("enforce"); _ragdoll->enforceRagdollConstraints(); for (int i = 0; i < numDolls; ++i) { _otherRagdolls[i]->enforceRagdollConstraints(); } } int iterations = 0; float error = 0.0f; do { computeCollisions(); updateContacts(); resolveCollisions(); { // enforce constraints PerformanceTimer perfTimer("enforce"); error = _ragdoll->enforceRagdollConstraints(); for (int i = 0; i < numDolls; ++i) { error = glm::max(error, _otherRagdolls[i]->enforceRagdollConstraints()); } } enforceContactConstraints(); ++iterations; now = usecTimestampNow(); } while (_collisions.size() != 0 && (iterations < maxIterations) && (error > minError) && (now < expiry)); pruneContacts(); }
void Game::update(float timeDelta) { // Cheats! if (m_window.getKeyState(Graphics::KEY_DELETE) == Graphics::KeyState::Press) { //m_lives = 3; m_asteroids.clear(); } updateState(timeDelta); // Input if (m_state == Game::Playing || m_state == Game::LevelTransition) { auto projectile = m_ship.handleInput(); if (projectile) { m_projectiles.push_back(projectile); } } if (m_state != Game::GameOver && m_state != Game::WaitingForRespawn && m_state != Game::WaitingForStart) { m_ship.update(timeDelta); } if (m_state != Game::WaitingForStart) { m_emitter.update(timeDelta); for (auto asteroid : m_asteroids) { asteroid->update(timeDelta); } for (auto p : m_projectiles) { p->update(timeDelta); } if (m_ufo) { m_ufo->update(timeDelta); auto projectile = m_ufo->shoot(); if (projectile) { m_projectiles.push_back(projectile); } } else { m_ufoTime -= timeDelta; if (m_ufoTime <= 0.f) { m_ufo.reset(new UFO()); m_ufoTime = 20.f; } } resolveCollisions(); } }