From 1032161366da1b2153de8d804465061e6bcc4fce Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Sun, 17 Oct 2010 21:25:27 +0000 Subject: moved bullet objets to core::Entity, moved docking functions to game.cc and removed entity->dock(), enabled depth testing for bullet debug draw --- src/core/entity.cc | 131 ++++++++++++++++++++++------------------------- src/core/entity.h | 37 ++++++------- src/game/base/game.cc | 41 +++++++++++++-- src/game/base/planet.cc | 23 --------- src/game/base/planet.h | 6 --- src/game/base/station.cc | 22 -------- src/game/base/station.h | 3 -- src/render/draw.cc | 8 +-- 8 files changed, 120 insertions(+), 151 deletions(-) (limited to 'src') diff --git a/src/core/entity.cc b/src/core/entity.cc index 8a7976f..2f8b8a5 100644 --- a/src/core/entity.cc +++ b/src/core/entity.cc @@ -144,7 +144,10 @@ Entity::Entity() : entity_moduletypeid = 0; entity_model = 0; + + entity_body = 0; entity_body = 0; + entity_motionstate = 0; entity_collision_shape = 0; entity_speed = 0.0f; @@ -180,7 +183,10 @@ Entity::Entity(std::istream & is) entity_visible = true; entity_model = 0; + entity_body = 0; + entity_body = 0; + entity_motionstate = 0; entity_collision_shape = 0; entity_speed = 0.0f; @@ -219,11 +225,19 @@ Entity::~Entity() if (entity_zone) entity_zone->remove(this); + if (entity_motionstate) + delete entity_motionstate; + if (entity_collision_shape) delete entity_collision_shape; if (entity_body) delete entity_body; + + if (entity_body_info) + delete entity_body_info; + + } void Entity::die() @@ -244,34 +258,6 @@ void Entity::clear_updates() } } -// reset physics state -void Entity::reset() -{ - if (!entity_body) { - if (entity_collision_shape ) { - delete entity_collision_shape ; - entity_collision_shape = 0; - } - - if (model()) { - entity_collision_shape = new btBoxShape(to_btVector3(model()->maxbbox())); - } else { - entity_collision_shape = new btSphereShape(radius()); - } - - btVector3 inertia(0, 0, 0); - entity_body = new btRigidBody(0.0f, 0, entity_collision_shape, inertia); - } - - btTransform t; - t.setIdentity(); - t.setOrigin(to_btVector3(location())); - t.setBasis(to_btMatrix3x3(axis())); - - entity_body->setWorldTransform(t); - set_dirty(); -} - void Entity::set_info(Info *info) { entity_info = info; @@ -467,10 +453,6 @@ void Entity::receive_server_update(std::istream &is) { } -void Entity::dock(core::Entity *entity) -{ -} - void Entity::frame(float seconds) { } @@ -498,27 +480,63 @@ void Entity::remove_menu(std::string const &label) } } +void Entity::reset() +{ + // location and orientation + btTransform t; + t.setIdentity(); + t.setOrigin(to_btVector3(location())); + t.setBasis(to_btMatrix3x3(axis())); + + // construct physics body if necessary + if (!entity_body) { + // create collision shape + if (model()) { + entity_collision_shape = new btBoxShape(to_btVector3(model()->maxbbox())); + } else { + entity_collision_shape = new btSphereShape(radius()); + } + btVector3 inertia(0, 0, 0); + if (entity_mass) + entity_collision_shape->calculateLocalInertia(entity_mass, inertia); + + // create motion state + entity_motionstate = new btDefaultMotionState(t); + + // create physics body + entity_body_info = new btRigidBody::btRigidBodyConstructionInfo(entity_mass, entity_motionstate, entity_collision_shape, inertia); + + entity_body = new btRigidBody(*entity_body_info); + //entity_body->setActivationState(ISLAND_SLEEPING); + + if (zone()) + zone()->physics()->addRigidBody(entity_body); + } + + // transfer entity location to motion state + entity_body->setWorldTransform(t); + if (zone()) + zone()->physics()->synchronizeSingleMotionState(entity_body); + + set_dirty(); +} + /* ---- class EntityDynamic ---------------------------------------- */ EntityDynamic::EntityDynamic() : Entity() { entity_state = Normal; entity_timer = 0; - - entity_motionstate = new btDefaultMotionState(); } EntityDynamic::EntityDynamic(std::istream & is) : Entity(is) { entity_state = Normal; entity_timer = 0; - entity_motionstate = 0; } EntityDynamic::~EntityDynamic() { - if (entity_motionstate) - delete entity_motionstate; } void EntityDynamic::set_state(int state) @@ -529,39 +547,15 @@ void EntityDynamic::set_state(int state) } } -// reset physics state void EntityDynamic::reset() { - assert(entity_motionstate); - - // construct physics body if necessary - if (!entity_body) { - if (entity_collision_shape ) { - delete entity_collision_shape ; - entity_collision_shape = 0; - } - - if (model()) { - entity_collision_shape = new btBoxShape(to_btVector3(model()->maxbbox())); - } else { - entity_collision_shape = new btSphereShape(radius()); - } - - btVector3 inertia(0, 0, 0); - entity_collision_shape->calculateLocalInertia(entity_mass, inertia); - entity_body = new btRigidBody(entity_mass, entity_motionstate, entity_collision_shape, inertia); + Entity::reset(); + + if ((entity_state == Docked) || (entity_state == Destroyed) || (!visible())){ + entity_body->activate(false); + } else { + entity_body->activate(true); } - - // transfer entity location to motion state - btTransform t; - t.setIdentity(); - t.setOrigin(to_btVector3(location())); - t.setBasis(to_btMatrix3x3(axis())); - entity_body->setWorldTransform(t); - if (zone()) - zone()->physics()->synchronizeSingleMotionState(entity_body); - - set_dirty(); } void EntityDynamic::frame(float seconds) @@ -571,8 +565,7 @@ void EntityDynamic::frame(float seconds) // transfer bullet state to entity state // FIXME disable physics when docked - if (entity_body && entity_motionstate) { - + if (entity_body) { // this makes sure an update is sent if speed goes to 0 in the next step if (entity_speed > 0) { set_dirty(); diff --git a/src/core/entity.h b/src/core/entity.h index 2719040..e51dac3 100644 --- a/src/core/entity.h +++ b/src/core/entity.h @@ -166,11 +166,16 @@ public: return entity_body; } - /// collision shape + /// physics collision shape inline btCollisionShape *collision_shape() { return entity_collision_shape; } + /// physics motion info + inline btMotionState *motionstate() { + return entity_motionstate; + } + /// indicates a server-side entity inline const bool serverside() const { return entity_serverside; @@ -277,10 +282,6 @@ public: /* ---- actors ---------------------------------------------------- */ - /// called when the entity received a docking request - // FIXME move docking functions to game - virtual void dock(Entity *entity); - /// set flags inline void set_flag(Flags flag) { entity_flags |= flag; @@ -419,8 +420,10 @@ public: float entity_servertimestamp; protected: - btRigidBody *entity_body; - btCollisionShape *entity_collision_shape; + btRigidBody *entity_body; + btRigidBody::btRigidBodyConstructionInfo *entity_body_info; + btCollisionShape *entity_collision_shape; + btMotionState *entity_motionstate; private: unsigned int entity_id; @@ -489,11 +492,7 @@ public: return entity_timer; } - inline btMotionState *motionstate() { - return entity_motionstate; - } - - /*----- mutators -------------------------------------------------- */ +/*----- mutators -------------------------------------------------- */ /// mass of the entity inline void set_mass(const float mass) { @@ -505,11 +504,6 @@ public: entity_speed = speed; } - /** - * @brief reset the physics state - */ - virtual void reset(); - /*----- serializers ----------------------------------------------- */ /// serialize the entity to a stream @@ -542,13 +536,14 @@ public: */ virtual void frame(float seconds); + /** + * @brief reset the physics state + */ + virtual void reset(); + protected: float entity_timer; int entity_state; - - btMotionState *entity_motionstate; - - }; /// an entity that can be controlled by a player diff --git a/src/game/base/game.cc b/src/game/base/game.cc index c0ef4ce..a075574 100644 --- a/src/game/base/game.cc +++ b/src/game/base/game.cc @@ -37,6 +37,8 @@ core::Entity *Default::view = 0; ShipModel *Default::shipmodel = 0; long Default::credits = 0; +const float planet_safe_distance = 150.0f; + void Default::clear() { zone = 0; @@ -74,9 +76,11 @@ void Game::func_join(core::Player *player, std::string const &args) ship->get_location().assign(dock->location() + (dock->axis().forward() *((ship->radius() + dock->radius())*2.0f))); ship->get_axis().assign(dock->axis()); ship->set_state(core::Entity::Docked); + ship->reset(); player->set_view(dock); } - + + std::string message("^B"); message.append(player->name()); message.append("^B joins the game."); @@ -177,8 +181,34 @@ void Game::func_dock(core::Player *player, core::Entity *entity) if (player->control()->state() != core::Entity::Normal) return; - if ((entity->flags() & core::Entity::Dockable) == core::Entity::Dockable) { - entity->dock(player->control()); + if (player->control()->moduletype() != ship_enttype) + return; + + if (!(entity->flags() & core::Entity::Dockable)) { + return; + } + + Ship * ship = static_cast(player->control()); + + float range = entity->radius() + ship->radius(); + if (entity->moduletype() == planet_enttype) { + range = planet_safe_distance; + } + + if (math::distance(entity->location(), ship->location()) > range) { + if (ship->owner()) { + ship->owner()->send("^W" + entity->name() + " out of range"); + } + return; + } + + ship->get_location().assign(entity->location()); + ship->set_state(core::Entity::Docked); + ship->reset(); + + if (ship->owner() && ship->owner()->control() == ship) { + ship->owner()->set_view(entity); + ship->owner()->send("^BDocking at " + entity->name()); } } @@ -1170,6 +1200,11 @@ bool Game::validate_zone(core::Zone *zone) generate_entity_menus(entity); } } + + // initialize physics on planets and entities with a model + if ((entity->entity_moduletypeid == planet_enttype) || (entity->model())) { + entity->reset(); + } } return true; diff --git a/src/game/base/planet.cc b/src/game/base/planet.cc index 7a648db..1e280c7 100644 --- a/src/game/base/planet.cc +++ b/src/game/base/planet.cc @@ -30,27 +30,4 @@ Planet::~Planet() } -void Planet::dock(core::Entity *entity) -{ - if (entity->moduletype() != ship_enttype) - return; - - Ship * ship = static_cast(entity); - - // fixed 5 km docking radius - if (math::distance(location(), ship->location()) > radius() + ship->radius() + 50.0f) { - if (ship->owner()) - ship->owner()->send("Planet out of range"); - return; - } - - ship->get_location().assign(location()); - ship->set_state(core::Entity::Docked); - - if (ship->owner() && ship->owner()->control() == ship) { - ship->owner()->set_view(this); - ship->owner()->send("^BDocking at " + name()); - } -} - } // namespace game diff --git a/src/game/base/planet.h b/src/game/base/planet.h index d2b18a8..19db012 100644 --- a/src/game/base/planet.h +++ b/src/game/base/planet.h @@ -21,14 +21,8 @@ class Planet : public core::EntityGlobe public: Planet(); virtual ~Planet(); - - /// entity received a docking request - virtual void dock(core::Entity *entity); }; -/// FIXME -const float planet_safe_distance = 5.0f; - } // namespace game #endif // __INCLUDED_BASE_PLANET_H__ diff --git a/src/game/base/station.cc b/src/game/base/station.cc index f5dc50a..0d992bd 100644 --- a/src/game/base/station.cc +++ b/src/game/base/station.cc @@ -22,26 +22,4 @@ Station::~Station() { } -void Station::dock(core::Entity *entity) -{ - if (entity->moduletype() != ship_enttype) - return; - - Ship * ship = static_cast(entity); - - if (math::distance(location(), ship->location()) > radius() + ship->radius()) { - if (ship->owner()) - ship->owner()->send("Target out of range"); - return; - } - - ship->get_location().assign(location()); - ship->set_state(core::Entity::Docked); - - if (ship->owner() && ship->owner()->control() == ship) { - ship->owner()->set_view(this); - ship->owner()->send("^BDocking at " + name()); - } -} - } diff --git a/src/game/base/station.h b/src/game/base/station.h index 3f8bea6..2aa8a95 100644 --- a/src/game/base/station.h +++ b/src/game/base/station.h @@ -15,9 +15,6 @@ class Station : public core::Entity public: Station(); virtual ~Station(); - - /// entity received a docking request - virtual void dock(core::Entity *entity); }; } diff --git a/src/render/draw.cc b/src/render/draw.cc index 656653b..04ccce4 100644 --- a/src/render/draw.cc +++ b/src/render/draw.cc @@ -1294,9 +1294,6 @@ void draw(float seconds) glDisableClientState(GL_NORMAL_ARRAY); glDisableClientState(GL_TEXTURE_COORD_ARRAY); - gl::depthmask(GL_TRUE); // enable depth buffer writing - gl::disable(GL_DEPTH_TEST); // disable depth buffer testing - // draw physics if (r_physics && r_physics->value()) { if (zone->physics()) { @@ -1310,7 +1307,10 @@ void draw(float seconds) gl::end(); } } - + + gl::depthmask(GL_TRUE); // enable depth buffer writing + gl::disable(GL_DEPTH_TEST); // disable depth buffer testing + gl::disable(GL_COLOR_MATERIAL); // disable color tracking gl::disable(GL_CULL_FACE); // disable culling -- cgit v1.2.3