From a76489275b7c66ab0264e8e29c4d4d08c22e3e16 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Tue, 19 Oct 2010 21:16:41 +0000 Subject: bullet ship physics --- src/game/base/ship.cc | 100 +++++++++++++++++++++++++++++++++++++++++++------- src/game/base/ship.h | 5 ++- 2 files changed, 91 insertions(+), 14 deletions(-) (limited to 'src/game') diff --git a/src/game/base/ship.cc b/src/game/base/ship.cc index 9f04e43..7cd602e 100644 --- a/src/game/base/ship.cc +++ b/src/game/base/ship.cc @@ -77,11 +77,12 @@ Ship::Ship(core::Player *owner, ShipModel *shipmodel) : core::EntityControlable( set_flag(core::Entity::Dockable); } - set_mass(radius()); + set_mass(radius() * 10.0f); reset(); - const float damp = Game::g_damping->value(); - body()->setDamping(damp, damp); + const float linear_damp = 0.5f; + const float angular_damp = 0.75f; + body()->setDamping(linear_damp, angular_damp); } Ship::~Ship() @@ -298,22 +299,96 @@ void Ship::set_zone(core::Zone *zone) owner()->set_zone(zone); } +void Ship::action (btScalar seconds) +{ + /* controls + * + * current_target_direction + * current_target_pitch + * current_target_roll + * current_target_strafe + * current_target_vstrafe + * entity_thrust + */ + + float actual_maxspeed = 0; + float actual_acceleration = 0; + float actual_thrust = 0; + btTransform t; + + switch (entity_state) { + case core::Entity::Normal: + actual_maxspeed = ship_shipmodel->maxspeed(); + actual_acceleration = ship_shipmodel->acceleration(); + actual_thrust = entity_thrust; + break; + case core::Entity::ImpulseInitiate: + case core::Entity::Impulse: + actual_maxspeed = Game::g_impulsespeed->value(); + actual_acceleration = Game::g_impulseacceleration->value(); + actual_thrust = 1.0f; + break; + case core::Entity::JumpInitiate: + case core::Entity::Jump: + actual_maxspeed = 0.0f; + actual_acceleration = Game::g_impulseacceleration->value(); + actual_thrust = 0.0f; + break; + case core::Entity::Docked: + + t.setIdentity(); + t.setOrigin(to_btVector3(location())); + t.setBasis(to_btMatrix3x3(axis())); + body()->setWorldTransform(t); + return; + break; + default: + break; + } + + t.setIdentity(); + entity_motionstate->getWorldTransform(t); + get_location().assign(t.getOrigin()); + get_axis().assign(t.getBasis()); + + const float force_scale = 1.0f; + const float thrust_scale = 4.0f; + + body()->applyCentralImpulse(math::to_btVector3(axis().up() * current_target_vstrafe * actual_acceleration * force_scale)); + body()->applyCentralImpulse(math::to_btVector3(axis().left() * current_target_strafe * actual_acceleration * force_scale)); + + body()->applyCentralImpulse(math::to_btVector3(axis().forward() * current_target_afterburner * actual_acceleration * force_scale)); + if (current_target_afterburner >= 0) { + body()->applyCentralImpulse(math::to_btVector3(axis().forward() * actual_thrust * actual_acceleration * thrust_scale)); + } + + const float torque_scale = 0.001f; + body()->applyTorqueImpulse(math::to_btVector3(axis().up() * current_target_direction * ship_shipmodel->turnspeed() * torque_scale)); + body()->applyTorqueImpulse(math::to_btVector3(axis().left() * -current_target_pitch * ship_shipmodel->turnspeed() * torque_scale)); + + body()->applyTorqueImpulse(math::to_btVector3(axis().forward() * -current_target_roll * ship_shipmodel->turnspeed() * torque_scale * 0.25f)); + + entity_speed = (float) entity_body->getLinearVelocity().length(); + if (entity_speed > Game::g_impulsespeed->value()) { + body()->setLinearVelocity(math::to_btVector3(axis().forward() * Game::g_impulsespeed->value())); + entity_speed = actual_maxspeed; + } + +} void Ship::frame(float seconds) { const float direction_reaction = 2.0f; // direction controls reaction time factor const float thrust_reaction = 0.5f; // thrust control reaction time factor const float strafe_reaction = 1.5f; const float afterburner_reaction = 1.0f; // a fterburner control reaction time + //const float actual_turnspeed = ship_shipmodel->turnspeed(); - float actual_maxspeed = ship_shipmodel->maxspeed(); - float actual_turnspeed = ship_shipmodel->turnspeed(); + float actual_maxspeed = ship_shipmodel->maxspeed(); float actual_acceleration = ship_shipmodel->acceleration(); - float actual_thrust = 0; + //float actual_thrust = 0; - math::Vector3f n; // normal of a plane - + math::Vector3f n; // normal of a plane math::Axis target_axis(axis()); // target axis - entity_movement = 0; /* -- update state ----------------------------------------- */ @@ -438,7 +513,6 @@ void Ship::frame(float seconds) actual_maxspeed = Game::g_impulsespeed->value(); actual_acceleration = Game::g_impulseacceleration->value(); - actual_turnspeed *= 0.5; } } else if (entity_state == core::Entity::Impulse) { @@ -458,7 +532,6 @@ void Ship::frame(float seconds) } else { actual_maxspeed = Game::g_impulsespeed->value(); actual_acceleration = Game::g_impulseacceleration->value(); - actual_turnspeed *= 0.5; target_afterburner = 0.0f; target_thrust = 0.0f; } @@ -474,7 +547,6 @@ void Ship::frame(float seconds) if (speed() > actual_maxspeed * 1.15f) { actual_acceleration = Game::g_impulseacceleration->value(); - actual_turnspeed *= 0.5; } } else if (entity_state == core::Entity::Destroyed) { @@ -602,7 +674,7 @@ void Ship::frame(float seconds) } EntityDynamic::frame(seconds); - */ + // apply direction rotation to target axis if (fabs(current_target_direction) > MIN_DELTA) { @@ -697,6 +769,8 @@ void Ship::frame(float seconds) if (zone()) zone()->physics()->synchronizeSingleMotionState(entity_body); + */ + EntityControlable::frame(seconds); } } // namespace game diff --git a/src/game/base/ship.h b/src/game/base/ship.h index 1f4f743..2c1bc2c 100644 --- a/src/game/base/ship.h +++ b/src/game/base/ship.h @@ -23,7 +23,10 @@ public: Ship(core::Player *owner, ShipModel *shipmodel); ~Ship(); - /// update the ship state + /// physices frame + virtual void action (btScalar seconds); + + /// game frame virtual void frame(float seconds); /// move the ship to a different zone -- cgit v1.2.3