diff options
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/entity.cc | 95 | ||||
-rw-r--r-- | src/core/entity.h | 12 |
2 files changed, 86 insertions, 21 deletions
diff --git a/src/core/entity.cc b/src/core/entity.cc index 6aef6e5..7aa72fa 100644 --- a/src/core/entity.cc +++ b/src/core/entity.cc @@ -150,7 +150,6 @@ Entity::Entity() : entity_body = 0; entity_body_info = 0; - entity_motionstate = 0; entity_collision_shape = 0; entity_speed = 0.0f; @@ -191,7 +190,6 @@ Entity::Entity(std::istream & is) entity_body = 0; entity_body_info = 0; - entity_motionstate = 0; entity_collision_shape = 0; entity_speed = 0.0f; @@ -235,9 +233,6 @@ Entity::~Entity() } } - if (entity_motionstate) - delete entity_motionstate; - if (entity_collision_shape) delete entity_collision_shape; @@ -511,6 +506,7 @@ void Entity::reset() if (flag_is_set(Complex) && model()->collisionmesh()) { // use collision mesh + // static entities use a btBvhTriangleMeshShape btBvhTriangleMeshShape *meshshape = new btBvhTriangleMeshShape(model()->collisionmesh()->triangles(), true, true); meshshape->buildOptimizedBvh(); meshshape->recalcLocalAabb(); @@ -519,7 +515,7 @@ void Entity::reset() btVector3 modelscalevec(modelscale, modelscale, modelscale); meshshape->setLocalScaling(modelscalevec); - meshshape->setMargin(0.0f); + //meshshape->setMargin(0.0f); //meshshape->updateBound(); entity_collision_shape = meshshape; @@ -536,14 +532,12 @@ void Entity::reset() 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_info = new btRigidBody::btRigidBodyConstructionInfo(entity_mass, 0, entity_collision_shape, inertia); entity_body = new btRigidBody(*entity_body_info); entity_body->setActivationState(ISLAND_SLEEPING); + entity_body->setCollisionFlags(entity_body->getCollisionFlags() | btCollisionObject::CF_STATIC_OBJECT); if (zone()) zone()->physics()->addRigidBody(entity_body); @@ -553,7 +547,6 @@ void Entity::reset() body()->setLinearVelocity(btVector3(0.0f, 0.0f, 0.0f)); body()->setLinearVelocity(btVector3(0.0f, 0.0f, 0.0f)); body()->setWorldTransform(t); - motionstate()->setWorldTransform(t); //if (zone()) // zone()->physics()->synchronizeSingleMotionState(entity_body); @@ -567,16 +560,20 @@ EntityDynamic::EntityDynamic() : Entity() { entity_state = Normal; entity_timer = 0; + entity_motionstate = 0; } 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) @@ -593,7 +590,58 @@ void EntityDynamic::reset() return; } - 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() && model()->radius()) { + const float modelscale = radius() / model()->radius(); + + if (flag_is_set(Complex) && model()->collisionmesh()) { + // use collision mesh + // dynamic entities use a btGImpactMeshShape + btGImpactMeshShape *meshshape = new btGImpactMeshShape(model()->collisionmesh()->triangles()); + + btVector3 modelscalevec(modelscale, modelscale, modelscale); + meshshape->setLocalScaling(modelscalevec); + //meshshape->setMargin(0.0f); + meshshape->updateBound(); + + entity_collision_shape = meshshape; + } else { + // use bounding box + entity_collision_shape = new btBoxShape(to_btVector3(model()->box().max() * modelscale)); + } + } 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); + entity_body->setCollisionFlags(entity_body->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT); + + if (zone()) + zone()->physics()->addRigidBody(entity_body); + } + + // transfer entity location to motion state + body()->setLinearVelocity(btVector3(0.0f, 0.0f, 0.0f)); + body()->setLinearVelocity(btVector3(0.0f, 0.0f, 0.0f)); + body()->setWorldTransform(t); if (entity_state == Docked) { body()->setLinearFactor(btVector3(0.0f, 0.0f, 0.0f)); @@ -602,6 +650,8 @@ void EntityDynamic::reset() body()->setLinearFactor(btVector3(1.0f, 1.0f, 1.0f)); body()->setAngularFactor(btVector3(1.0f, 1.0f, 1.0f)); } + + set_dirty(); } void EntityDynamic::frame(float seconds) @@ -967,17 +1017,31 @@ void EntityControlable::reset() // construct physics body if necessary if (!entity_body) { // create collision shape - // create collision shape if (model() && model()->radius()) { const float modelscale = radius() / model()->radius(); - entity_collision_shape = new btBoxShape(to_btVector3(model()->box().max() * modelscale)); + + if (flag_is_set(Complex) && model()->collisionmesh()) { + // use collision mesh + // dynamic entities use a btGImpactMeshShape + btGImpactMeshShape *meshshape = new btGImpactMeshShape(model()->collisionmesh()->triangles()); + + btVector3 modelscalevec(modelscale, modelscale, modelscale); + meshshape->setLocalScaling(modelscalevec); + //meshshape->setMargin(0.0f); + meshshape->updateBound(); + + entity_collision_shape = meshshape; + } else { + // use bounding box + entity_collision_shape = new btBoxShape(to_btVector3(model()->box().max() * modelscale)); + } } 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); @@ -985,6 +1049,7 @@ void EntityControlable::reset() entity_body_info = new btRigidBody::btRigidBodyConstructionInfo(entity_mass, entity_motionstate, entity_collision_shape, inertia); entity_body = new btRigidBody(*entity_body_info); entity_body->setActivationState(DISABLE_DEACTIVATION); + //entity_body->setCollisionFlags(entity_body->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT); entity_actioninterface = new ActionInterface(this); diff --git a/src/core/entity.h b/src/core/entity.h index 378a71a..e12123e 100644 --- a/src/core/entity.h +++ b/src/core/entity.h @@ -173,11 +173,6 @@ public: 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; @@ -456,7 +451,6 @@ protected: btRigidBody *entity_body; btRigidBody::btRigidBodyConstructionInfo *entity_body_info; btCollisionShape *entity_collision_shape; - btMotionState *entity_motionstate; // the zone the entity belongs to Zone* entity_zone; @@ -536,6 +530,11 @@ public: return entity_timer; } + /// physics motion info + inline btMotionState *motionstate() { + return entity_motionstate; + } + /*----- mutators -------------------------------------------------- */ /// mass of the entity @@ -588,6 +587,7 @@ public: protected: float entity_timer; int entity_state; + btMotionState *entity_motionstate; }; /// an entity that can be controlled by a player |