bool Space::Update(Scene &scene, float dt) { // Offset for UV mapping, creates illusion of scrolling //offset -= dt/5; GenerateModelMatrix(); return true; }
bool Asteroid::Update(Scene &scene, float dt) { // Count time alive age += dt; // Animate position according to time position += speed * dt; // Rotate the object rotation += rotMomentum * dt; // Delete when alive longer than 10s or out of visibility if (age > 10.0f || position.y < -10) return false; // Collide with scene for (auto obj : scene.objects) { // Ignore self in scene if (obj.get() == this) continue; // We only need to collide with asteroids and projectiles, ignore other objects auto asteroid = std::dynamic_pointer_cast<Asteroid>(obj); auto projectile = std::dynamic_pointer_cast<Projectile>(obj); if (!asteroid && !projectile) continue; // When colliding with other asteroids make sure the object is older than .5s // This prevents excessive collisions when asteroids explode. if (asteroid && age < 0.5f) continue; // Compare distance to approximate size of the asteroid estimated from scale. if (glm::distance(position, obj->position) < (obj->scale.y + scale.y)*0.7f) { int pieces = 3; // Too small to split into pieces if (scale.y < 0.5) pieces = 0; // The projectile will be destroyed if (projectile) projectile->Destroy(); // Generate smaller asteroids Explode(scene, (obj->position+position)/2.0f, (obj->scale+scale)/2.0f, pieces); // Destroy self return false; } } // Generate modelMatrix from position, rotation and scale GenerateModelMatrix(); return true; }
bool Projectile::Update(Scene &scene, float dt) { // Increase age age += dt; // Accelerate speed += glm::vec3(0.0f, 0.2f, 0.0f); rotation += rotMomentum; // Move the projectile position += speed * dt; // Die after 5s if (age > 5.0f) return false; GenerateModelMatrix(); return true; }