From ebe064bf159a5c6e90f2bbe902efa16c6e654ff8 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Fri, 6 May 2011 16:32:55 +0000 Subject: Always use a btCompoundShape for complex collision, added infrastructure for collision mesh animation, minor API documentation updates --- src/model/collisionmesh.cc | 14 ++++++-- src/model/collisionmesh.h | 84 ++++++++++++++++++++++++++++++++++++++++++++-- src/model/fragment.cc | 2 +- src/model/fragment.h | 15 ++++++--- src/model/mapfile.cc | 10 ++++-- 5 files changed, 112 insertions(+), 13 deletions(-) (limited to 'src/model') diff --git a/src/model/collisionmesh.cc b/src/model/collisionmesh.cc index 5d2328a..e341e9f 100644 --- a/src/model/collisionmesh.cc +++ b/src/model/collisionmesh.cc @@ -47,6 +47,10 @@ CollisionMesh::CollisionMesh() collisionmesh_size = 0; // btTriangleMesh (bool use32bitIndices=true, bool use4componentVertices=true) collisionmesh_triangles = new btTriangleMesh(true, false); + + collisionmesh_scale = 1.0f; + collisionmesh_speed = 0.0f; + collisionmesh_distance = 0.0f; } CollisionMesh::~CollisionMesh() @@ -54,9 +58,15 @@ CollisionMesh::~CollisionMesh() delete collisionmesh_triangles; } -void CollisionMesh::set_type(const FragmentGroup::Type type) +void CollisionMesh::set_params(const FragmentGroup *group) { - collisionmesh_type = type; + collisionmesh_type = group->type(); + collisionmesh_location.assign(group->location()); + collisionmesh_axis.assign(group->axis()); + collisionmesh_scale = group->scale(); + collisionmesh_speed = group->speed(); + collisionmesh_distance = group->distance(); + } void CollisionMesh::add_triangle(const math::Vector3f & v0, const math::Vector3f & v1, const math::Vector3f & v2) diff --git a/src/model/collisionmesh.h b/src/model/collisionmesh.h index cbd063a..4ab1c19 100644 --- a/src/model/collisionmesh.h +++ b/src/model/collisionmesh.h @@ -48,13 +48,83 @@ public: inline btTriangleMesh *triangles() { return collisionmesh_triangles; } + + /** + * @brief location of the mesh within the parent model + * location() is a point in collision model coordinate space + * that indicates the (0,0,0) location of this mesh. + * The worldspawn mesh should have location (0,0,0) + * */ + inline const math::Vector3f &location() const { + return collisionmesh_location; + } + + /** + * @brief transformation axis + * For normal groups, this is the rotation matrix of the mesh + * For rotating groups axis->forward() is the axis of the rotation. + * For movers, axis->forward() is the axis of movement. + * */ + inline const math::Axis & axis() const { + return collisionmesh_axis; + } + + inline const float speed() const { + return collisionmesh_speed; + } + inline const float distance() const { + return collisionmesh_distance; + } + + inline const float scale() const { + return collisionmesh_scale; + } + + inline const FragmentGroup::Type type() const { + return collisionmesh_type; + } + /* ---- mutators ------------------------------------------- */ /** * @brief change the group type * */ - void set_type(const FragmentGroup::Type type); + inline void set_type(const FragmentGroup::Type type) { + collisionmesh_type = type; + } + + inline void set_location(const math::Vector3f &location) { + collisionmesh_location.assign(location); + } + + inline void set_axis(const math::Axis &axis) { + collisionmesh_axis.assign(axis); + } + + /** + * @brief apply FragmentGroup parameters to the mesh + * This method applies the fragmentgroups type, location, axis, distance, speed and scale + * to the collision mesh + */ + void set_params(const FragmentGroup *group); + + /** + * @brief movement speed + * For rotating meshes this is the number of degrees per second + * For movers, this is the speed in units per second + */ + inline void set_speed(const float speed) { + collisionmesh_speed = speed; + } + + inline void set_distance(const float distance) { + collisionmesh_distance = distance; + } + + inline void set_scale(const float scale) { + collisionmesh_scale = scale; + } /** * @brief add a triangle to the collision mesh @@ -89,8 +159,8 @@ public: static void add(CollisionMesh *collisionmesh); - static bool initialized() { - return collisionmesh_initialized; + inline static bool initialized() { + return collisionmesh_initialized; } private: @@ -102,6 +172,14 @@ private: size_t collisionmesh_size; btTriangleMesh *collisionmesh_triangles; FragmentGroup::Type collisionmesh_type; + + math::Vector3f collisionmesh_location; + math::Axis collisionmesh_axis; + + float collisionmesh_speed; + float collisionmesh_scale; + float collisionmesh_distance; + }; } // namespace model diff --git a/src/model/fragment.cc b/src/model/fragment.cc index 54ffad9..b4666f4 100644 --- a/src/model/fragment.cc +++ b/src/model/fragment.cc @@ -63,7 +63,7 @@ FragmentGroup::FragmentGroup() { group_type = None; group_scale = 1.0f; - + group_distance = 0.0f; group_speed = 0.0f; group_engine = false; } diff --git a/src/model/fragment.h b/src/model/fragment.h index db5f569..fcacecc 100644 --- a/src/model/fragment.h +++ b/src/model/fragment.h @@ -98,10 +98,17 @@ public: return group_location; } + /** + * @brief movement vector for moving groups + * For rotating groups, this is the rotation axis + */ inline const math::Vector3f &movement() const { return group_movement; } + /** + * @brief local rotation matrix of the group + */ inline const math::Axis & axis() const { return group_axis; } @@ -150,17 +157,15 @@ public: /** - * @brief rotation axis - * For normal groups, this is the transformation matrix - * For rotating groups axis->forward() is the axis of the rotation. - * For movers, axis->forward() is the axis of movement. + * @brief set the rotation axis + * set the local rotation matrix of the group */ inline void set_axis(const math::Axis &axis) { group_axis.assign(axis); } /** - * @brief movement speed + * @brief set movement speed * For rotating groups this is the number of degrees per second * For movers, this is the speed in units per second */ diff --git a/src/model/mapfile.cc b/src/model/mapfile.cc index 92349c0..b05880f 100644 --- a/src/model/mapfile.cc +++ b/src/model/mapfile.cc @@ -1305,7 +1305,7 @@ void MapFile::load_fragmentgroup(Model *model, const FragmentGroup::Type class_t // add the vertexgroup to the model model->add_group(group); - // add collision triangles + // load collision triangles into a mesh if (map_load_clip) { if (map_collisiontriangles.size()) { CollisionMesh *collisionmesh = new CollisionMesh(); @@ -1315,7 +1315,10 @@ void MapFile::load_fragmentgroup(Model *model, const FragmentGroup::Type class_t Triangle *triangle = (*it); collisionmesh->add_triangle(triangle->v0() - translation, triangle->v1() - translation, triangle->v2() - translation); } - + + // apply vertexgroup params to the collision mesh + collisionmesh->set_params(group); + // add the collision mesh to the collision model model->collisionmodel()->add_mesh(collisionmesh); } @@ -1958,6 +1961,9 @@ Model * MapFile::load(std::string const &name) cmit != model->collisionmodel()->meshes().end(); cmit++) { collision_tris_count += (*cmit)->size(); collision_mesh_count++; + + // translate collision meshes + (*cmit)->set_location((*cmit)->location() - map_center); } con_debug << " " << mapfile.name() << " collision " << collision_tris_count << " triangles " << collision_mesh_count << " meshes" << std::endl; -- cgit v1.2.3