/* game/ship.cc This file is part of the Osirion project and is distributed under the terms and conditions of the GNU General Public License version 2 */ // project headers #include "game/ship.h" #include "math/mathlib.h" // C++ headers #include using math::degrees360f; using math::degrees180f; namespace game { Ship::Ship() { speed = 0; yaw_current = 0; yaw_target = yaw_current; thrust_current = 0; // ship specs acceleration = 1.5f; speed_max = 4.0f; yaw_speed = 4.0f; } Ship::~Ship() { } void Ship::set_yaw(float y) { yaw_target = degrees360f(y); } void Ship::set_thrust(float t) { if (t < 0) thrust_current = 0; else if (t > 1) thrust_current = 1; else thrust_current = t; } void Ship::update(float elapsed) { // update yaw float yaw_offset = degrees180f(yaw_target - yaw_current); float d = yaw_speed * elapsed * yaw_offset; yaw_current = degrees360f(yaw_current + d); // update thrust if (speed < thrust_current * speed_max) { speed += acceleration * elapsed; if (speed > thrust_current * speed_max) { speed = thrust_current * speed_max; } } else if(speed > thrust_current * speed_max) { speed -= acceleration * elapsed; if (speed < 0) speed = 0; } // location TODO avoid sin/cos calculations location.x += cosf(yaw_current * M_PI / 180) * speed * elapsed; location.z -= sinf(yaw_current * M_PI / 180) * speed * elapsed; } } // namespace game