From 38eb51c26ab0d9dbebc974c7a21f96a429ce3098 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Tue, 8 Feb 2011 16:55:23 +0000 Subject: Corrected triangle loading of collision model, added 'complex' flag to ships.ini to enable collision models on player ships. --- src/core/entity.cc | 95 +++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 80 insertions(+), 15 deletions(-) (limited to 'src/core/entity.cc') 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) @@ -966,18 +1016,32 @@ 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); -- cgit v1.2.3