From 50947891a97f19fa11f9f073c8988156f9655d97 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Fri, 6 May 2011 18:27:55 +0000 Subject: Support for func_rotate and func_move collision meshes. --- src/core/entity.cc | 47 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 6 deletions(-) (limited to 'src/core/entity.cc') diff --git a/src/core/entity.cc b/src/core/entity.cc index bba3c5c..1535de8 100644 --- a/src/core/entity.cc +++ b/src/core/entity.cc @@ -490,11 +490,44 @@ void Entity::remove_menu(std::string const &label) void Entity::frame(float seconds) { - if (entity_collision_child_shapes.size()) { - //btCompoundShape *compoundshape = static_cast (entity_collision_shape); - - // TODO apply collision mesh animation - + if (entity_collision_child_shapes.size() > 0) { + btCompoundShape *compoundshape = static_cast (entity_collision_shape); + bool recalculate = false; + + for (int n = 0; n < compoundshape->getNumChildShapes(); n++) { + model::CollisionMesh *mesh = static_cast(compoundshape->getChildShape(n)->getUserPointer()); + + if (mesh) { + if (mesh->type() == model::FragmentGroup::Rotate) { + const float rotation_radians = math::degrees360f(game()->time() * mesh->speed()) * M_PI / 180.0f; + math::Axis rotation_axis; + rotation_axis.rotate(mesh->movement(), rotation_radians); + + btTransform child_transform; + child_transform.setIdentity(); + child_transform.setOrigin(math::to_btVector3(mesh->location())); + 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.setBasis(math::to_btMatrix3x3(mesh->axis())); + + compoundshape->updateChildTransform(n, child_transform); + } + } + + if (recalculate) + compoundshape->recalculateLocalAabb(); + } } } @@ -548,11 +581,13 @@ void Entity::reset() 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; default: - meshshape->setUserPointer(0); + meshshape->setUserPointer(0); + break; } // add the mesh to the compoundhape and store a pointer to the child shape compoundshape->addChildShape(child_transform, meshshape); -- cgit v1.2.3