From adb7309b22091b595d2bd11222dd1ec3ca1bab82 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Thu, 10 Feb 2011 19:21:02 +0000 Subject: Corrected physics on dynamic entities like cargo pods. --- src/core/entity.cc | 49 ++++++++++++++++++++++--------------------------- 1 file changed, 22 insertions(+), 27 deletions(-) (limited to 'src/core/entity.cc') diff --git a/src/core/entity.cc b/src/core/entity.cc index ebe4938..ec25017 100644 --- a/src/core/entity.cc +++ b/src/core/entity.cc @@ -153,7 +153,6 @@ Entity::Entity() : entity_collision_shape = 0; entity_speed = 0.0f; - entity_mass = 0.0f; entity_radius = 0.5f; entity_shape = Diamond; @@ -193,7 +192,6 @@ Entity::Entity(std::istream & is) entity_collision_shape = 0; entity_speed = 0.0f; - entity_mass = 0.0f; entity_created = true; entity_destroyed = false; @@ -511,15 +509,10 @@ void Entity::reset() meshshape->buildOptimizedBvh(); meshshape->recalcLocalAabb(); - //btGImpactMeshShape *meshshape = new btGImpactMeshShape(model()->collisionmesh()->triangles()); - btVector3 modelscalevec(modelscale, modelscale, modelscale); meshshape->setLocalScaling(modelscalevec); - meshshape->setMargin(Cvar::sv_collisionmargin->value()); entity_collision_shape = meshshape; - // con_debug << " " << label() << " attached collision mesh: " << model()->collisionmesh()->size() << " triangles" << std::endl; - } else { // use bounding box entity_collision_shape = new btBoxShape(to_btVector3(model()->box().max() * modelscale)); @@ -527,16 +520,14 @@ void Entity::reset() } else { entity_collision_shape = new btSphereShape(radius()); } + entity_collision_shape->setMargin(Cvar::sv_collisionmargin->value()); btVector3 inertia(0, 0, 0); - if (entity_mass) - entity_collision_shape->calculateLocalInertia(entity_mass, inertia); - + // create physics body - entity_body_info = new btRigidBody::btRigidBodyConstructionInfo(entity_mass, 0, entity_collision_shape, inertia); + entity_body_info = new btRigidBody::btRigidBodyConstructionInfo(0, 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); @@ -547,9 +538,6 @@ void Entity::reset() body()->setLinearVelocity(btVector3(0.0f, 0.0f, 0.0f)); body()->setWorldTransform(t); - //if (zone()) - // zone()->physics()->synchronizeSingleMotionState(entity_body); - set_dirty(); } @@ -560,6 +548,7 @@ EntityDynamic::EntityDynamic() : Entity() entity_state = Normal; entity_timer = 0; entity_motionstate = 0; + entity_mass = 0.0f; } EntityDynamic::EntityDynamic(std::istream & is) : Entity(is) @@ -567,6 +556,7 @@ EntityDynamic::EntityDynamic(std::istream & is) : Entity(is) entity_state = Normal; entity_timer = 0; entity_motionstate = 0; + entity_mass = 0.0f; } EntityDynamic::~EntityDynamic() @@ -608,10 +598,9 @@ void EntityDynamic::reset() btVector3 modelscalevec(modelscale, modelscale, modelscale); meshshape->setLocalScaling(modelscalevec); - meshshape->setMargin(Cvar::sv_collisionmargin->value()); meshshape->updateBound(); - entity_collision_shape = meshshape; + entity_collision_shape = meshshape; } else { // use bounding box entity_collision_shape = new btBoxShape(to_btVector3(model()->box().max() * modelscale)); @@ -619,6 +608,8 @@ void EntityDynamic::reset() } else { entity_collision_shape = new btSphereShape(radius()); } + entity_collision_shape->setMargin(Cvar::sv_collisionmargin->value()); + btVector3 inertia(0, 0, 0); if (entity_mass) entity_collision_shape->calculateLocalInertia(entity_mass, inertia); @@ -627,11 +618,14 @@ void EntityDynamic::reset() 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, 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 (entity_mass) { + entity_body->setActivationState(DISABLE_DEACTIVATION); + } else { + entity_body->setActivationState(ISLAND_SLEEPING); + } if (zone()) zone()->physics()->addRigidBody(entity_body); @@ -641,6 +635,10 @@ void EntityDynamic::reset() body()->setLinearVelocity(btVector3(0.0f, 0.0f, 0.0f)); body()->setLinearVelocity(btVector3(0.0f, 0.0f, 0.0f)); body()->setWorldTransform(t); + + if (motionstate()) { + motionstate()->setWorldTransform(t); + } if (entity_state == Docked) { body()->setLinearFactor(btVector3(0.0f, 0.0f, 0.0f)); @@ -661,11 +659,7 @@ void EntityDynamic::frame(float seconds) // transfer bullet state to entity state 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(); - } - + btTransform t; entity_motionstate->getWorldTransform(t); get_location().assign(t.getOrigin()); @@ -675,7 +669,7 @@ void EntityDynamic::frame(float seconds) if (entity_speed > 0) { set_dirty(); } - entity_speed = (float) entity_body->getLinearVelocity().length(); + entity_speed = (float) entity_body->getLinearVelocity().length(); if (entity_speed > 0) { set_dirty(); } @@ -1048,6 +1042,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); -- cgit v1.2.3