From 3de78ec1bef2a0274a719ea229709523650ade40 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Mon, 9 May 2011 12:23:36 +0000 Subject: Corrected collisionmesh scaling. --- src/core/entity.cc | 39 ++++++++++++++++++--------------------- 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/src/core/entity.cc b/src/core/entity.cc index 1535de8..915dafe 100644 --- a/src/core/entity.cc +++ b/src/core/entity.cc @@ -493,7 +493,8 @@ void Entity::frame(float seconds) if (entity_collision_child_shapes.size() > 0) { btCompoundShape *compoundshape = static_cast (entity_collision_shape); bool recalculate = false; - + const float modelscale = radius() / model()->radius(); + for (int n = 0; n < compoundshape->getNumChildShapes(); n++) { model::CollisionMesh *mesh = static_cast(compoundshape->getChildShape(n)->getUserPointer()); @@ -505,20 +506,19 @@ void Entity::frame(float seconds) btTransform child_transform; child_transform.setIdentity(); - child_transform.setOrigin(math::to_btVector3(mesh->location())); + child_transform.setOrigin(math::to_btVector3(mesh->location() * modelscale )); child_transform.setBasis(math::to_btMatrix3x3(mesh->axis() * rotation_axis)); compoundshape->updateChildTransform(n, child_transform); } else if (mesh->type() == model::FragmentGroup::Move ) { - const float freq = mesh->speed() / mesh->distance(); math::Vector3f translation(mesh->movement() * mesh->distance()); translation *= sinf(core::game()->time() * M_PI * freq) * 0.5f + 0.5f; btTransform child_transform; child_transform.setIdentity(); - child_transform.setOrigin(to_btVector3(mesh->location() + mesh->axis() * translation)); + child_transform.setOrigin(to_btVector3((mesh->location() + mesh->axis() * translation * mesh->scale()) * modelscale)); child_transform.setBasis(math::to_btMatrix3x3(mesh->axis())); compoundshape->updateChildTransform(n, child_transform); @@ -567,7 +567,10 @@ void Entity::reset() // static entities use a btBvhTriangleMeshShape btBvhTriangleMeshShape *meshshape = new btBvhTriangleMeshShape(mesh->triangles(), true, true); meshshape->setMargin(Cvar::sv_collisionmargin->value()); - btVector3 meshscalevec(mesh->scale(), mesh->scale(), mesh->scale()); + + const float meshscale = mesh->scale() * modelscale; + const btVector3 meshscalevec(meshscale, meshscale, meshscale); + meshshape->setLocalScaling(meshscalevec); meshshape->buildOptimizedBvh(); meshshape->recalcLocalAabb(); @@ -575,13 +578,12 @@ void Entity::reset() // apply collision mesh properties to the btCollisionShape btTransform child_transform; child_transform.setIdentity(); - child_transform.setOrigin(to_btVector3(mesh->location())); + child_transform.setOrigin(to_btVector3(mesh->location() * modelscale)); child_transform.setBasis(to_btMatrix3x3(mesh->axis())); switch (mesh->type()) { case model::FragmentGroup::Rotate: case model::FragmentGroup::Move: - con_debug << " ^Rcollision mesh attached to btCollisionShape " << std::endl; meshshape->setUserPointer((void *) mesh); break; @@ -594,9 +596,6 @@ void Entity::reset() entity_collision_child_shapes.push_back(meshshape); } - btVector3 modelscalevec(modelscale, modelscale, modelscale); - compoundshape->setLocalScaling(modelscalevec); - entity_collision_shape = compoundshape; } else { @@ -698,13 +697,15 @@ void EntityDynamic::reset() // dynamic entities use btGImpactMeshShape btGImpactMeshShape *meshshape = new btGImpactMeshShape(mesh->triangles()); meshshape->setMargin(Cvar::sv_collisionmargin->value()); - btVector3 meshscalevec(mesh->scale(), mesh->scale(), mesh->scale()); + + const float meshscale = mesh->scale() * modelscale; + const btVector3 meshscalevec(meshscale, meshscale, meshscale); meshshape->setLocalScaling(meshscalevec); // apply collision mesh properties to the btCollisionShape btTransform child_transform; child_transform.setIdentity(); - child_transform.setOrigin(to_btVector3(mesh->location())); + child_transform.setOrigin(to_btVector3(mesh->location() * modelscale)); child_transform.setBasis(to_btMatrix3x3(mesh->axis())); switch (mesh->type()) { @@ -721,10 +722,7 @@ void EntityDynamic::reset() compoundshape->addChildShape(child_transform, meshshape); entity_collision_child_shapes.push_back(meshshape); } - - btVector3 modelscalevec(modelscale, modelscale, modelscale); - compoundshape->setLocalScaling(modelscalevec); - + entity_collision_shape = compoundshape; } else { @@ -1161,14 +1159,16 @@ void EntityControlable::reset() // dynamic entities use btGImpactMeshShape btGImpactMeshShape *meshshape = new btGImpactMeshShape(mesh->triangles()); meshshape->setMargin(Cvar::sv_collisionmargin->value()); - btVector3 meshscalevec(mesh->scale(), mesh->scale(), mesh->scale()); + const float meshscale = mesh->scale() * modelscale; + const btVector3 meshscalevec(meshscale, meshscale, meshscale); + meshshape->setLocalScaling(meshscalevec); meshshape->updateBound(); // apply collision mesh properties to the btCollisionShape btTransform child_transform; child_transform.setIdentity(); - child_transform.setOrigin(to_btVector3(mesh->location())); + child_transform.setOrigin(to_btVector3(mesh->location() * modelscale)); child_transform.setBasis(to_btMatrix3x3(mesh->axis())); switch (mesh->type()) { @@ -1186,9 +1186,6 @@ void EntityControlable::reset() entity_collision_child_shapes.push_back(meshshape); } - btVector3 modelscalevec(modelscale, modelscale, modelscale); - compoundshape->setLocalScaling(modelscalevec); - entity_collision_shape = compoundshape; } else { -- cgit v1.2.3