/* 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/game.h" #include "game/ship.h" #include "math/mathlib.h" // C++ headers #include using math::degrees360f; using math::degrees180f; namespace game { const float MIN_DELTA = 10e-10; Ship::Ship(core::Player *owner, ShipModel *shipmodel) : core::EntityControlable(owner, ship_enttype) { entity_modelname = "ships/" + shipmodel->modelname(); entity_name = shipmodel->name() + ": <^B" + owner->name() + "^N>"; entity_label = shipmodel->label(); entity_moduletypeid = ship_enttype; current_target_direction = 0.0f; current_target_pitch = 0.0f;; current_target_roll = 0.0f; entity_color = owner->color(); entity_color_second = owner->color_second(); ship_shipmodel = shipmodel; ship_jumpdrive = shipmodel->shipmodel_jumpdrive; } Ship::~Ship() { } void Ship::frame(float seconds) { const float direction_change_speed = 2; float cosangle; // cosine of an angle float angle; // angle in radians math::Vector3f n; // normal of a plane // target axis math::Axis target_axis(entity_axis); // clamp input values math::clamp(target_thrust, 0.0f, 1.0f); math::clamp(target_pitch, -1.0f, 1.0f); math::clamp(target_roll, -1.0f, 1.0f); math::clamp(target_direction, -1.0f, 1.0f); // update thrust entity_thrust = target_thrust; if (autolevel() && Game::instance()->g_autolevel->value()) { n.assign(math::crossproduct(entity_axis.up(), math::Vector3f(0, 0, 1.0f))); if (!(n.length() < MIN_DELTA)) { cosangle = math::dotproduct(entity_axis.up(), math::Vector3f(0, 0, 1.0f)); target_roll = acos(cosangle); math::clamp(target_roll, 0.0f, 1.0f); target_roll *= M_1_PI; target_roll *= math::sgnf(math::dotproduct(entity_axis.left(), math::Vector3f(0.0, 0.0f, 1.0f))); } else { target_roll = 0; } } if (current_target_roll < target_roll) { current_target_roll += direction_change_speed * seconds; if (current_target_roll > target_roll) current_target_roll = target_roll; } else if (current_target_roll > target_roll) { current_target_roll -= direction_change_speed * seconds; if (current_target_roll < target_roll) current_target_roll = target_roll; } math::clamp(current_target_roll, -1.0f, 1.0f); if (fabs(current_target_roll) > MIN_DELTA) { float roll_offset = seconds * current_target_roll; entity_axis.change_roll(ship_shipmodel->turnspeed() * roll_offset); } else { current_target_roll = 0.0f; } // auto-leveling if (autolevel() && Game::instance()->g_autolevel->value()) { n.assign(math::crossproduct(entity_axis.up(), math::Vector3f(0.0f, 0.0f, 1.0f))); if (!(n.length() < MIN_DELTA)) { cosangle = math::dotproduct(entity_axis.up(), math::Vector3f(0.0f, 0.0f, 1.0f)); target_pitch = acos(cosangle); math::clamp(target_roll, 0.0f, 1.0f); target_pitch *= -math::sgnf(math::dotproduct(entity_axis.forward(), math::Vector3f(0.0, 0.0f, 1.0f))); } else { target_pitch = 0; } } // update target_axis direction if (current_target_direction < target_direction) { current_target_direction += direction_change_speed * seconds; if (current_target_direction > target_direction) { current_target_direction = target_direction; } } else if (current_target_direction > target_direction) { current_target_direction -= direction_change_speed * seconds; if (current_target_direction < target_direction) { current_target_direction = target_direction; } } if (fabs(current_target_direction) > MIN_DELTA ) { math::clamp(current_target_direction, -1.0f, 1.0f); target_axis.change_direction(ship_shipmodel->turnspeed() * current_target_direction); } else { //current_target_direction = 0.0f; } if (current_target_pitch < target_pitch) { current_target_pitch += direction_change_speed * seconds; if (current_target_pitch > target_pitch) current_target_pitch = target_pitch; } else if (current_target_pitch > target_pitch) { current_target_pitch -= direction_change_speed * seconds; if (current_target_pitch < target_pitch) current_target_pitch = target_pitch; } if (fabs(current_target_pitch) > MIN_DELTA) { math::clamp(current_target_pitch, -1.0f, 1.0f); target_axis.change_pitch(ship_shipmodel->turnspeed() * current_target_pitch); } else { //current_target_pitch = 0.0f; } n.assign(math::crossproduct(entity_axis.forward(), target_axis.forward())); if (!(n.length() < MIN_DELTA)) { n.normalize(); cosangle = math::dotproduct(entity_axis.forward(), target_axis.forward()); angle = acos(cosangle) * seconds; // * 180.0f / M_PI; if (angle > MIN_DELTA) entity_axis.rotate(n, -angle); } // update speed if (entity_speed < entity_thrust * ship_shipmodel->maxspeed()) { entity_speed += ship_shipmodel->acceleration() * seconds; if (entity_speed > entity_thrust * ship_shipmodel->maxspeed()) { entity_speed = entity_thrust * ship_shipmodel->maxspeed(); } } else if(entity_speed > entity_thrust * ship_shipmodel->maxspeed()) { entity_speed -= ship_shipmodel->acceleration() * seconds; if (entity_speed < 0.0f) entity_speed = 0.0f; } if (entity_speed != 0.0f) { entity_location += entity_axis.forward() * entity_speed * seconds; entity_dirty = true; } else if ((current_target_pitch != 0.0f) || (current_target_direction != 0.0f) || (current_target_roll != 0.0f)) { entity_dirty = true; } } } // namespace game