| 107 | } |
| 108 | |
| 109 | Handle::CollisionShapeHandle PhysicsSystem::makeCollisionShapeFromMesh(const Meshes::WorldStaticMesh& mesh, CollisionShape::ECollisionType type, const std::string& name) |
| 110 | { |
| 111 | if (m_ShapeCache.find(name) != m_ShapeCache.end()) |
| 112 | return m_ShapeCache[name]; |
| 113 | |
| 114 | // Init collision |
| 115 | btTriangleMesh* wm = new btTriangleMesh; |
| 116 | |
| 117 | for (size_t s = 0; s < mesh.mesh.m_SubmeshStarts.size(); s++) |
| 118 | { |
| 119 | if (!mesh.mesh.m_SubmeshMaterials[s].m_NoCollision) |
| 120 | { |
| 121 | for (size_t j = 0; j < mesh.mesh.m_SubmeshStarts[s].m_NumIndices; j += 3) |
| 122 | { |
| 123 | size_t i = mesh.mesh.m_SubmeshStarts[s].m_StartIndex + j; |
| 124 | |
| 125 | // TODO: Filter no-collision materials |
| 126 | auto& v0 = mesh.mesh.m_Vertices[mesh.mesh.m_Indices[i]].Position; |
| 127 | auto& v1 = mesh.mesh.m_Vertices[mesh.mesh.m_Indices[i + 1]].Position; |
| 128 | auto& v2 = mesh.mesh.m_Vertices[mesh.mesh.m_Indices[i + 2]].Position; |
| 129 | |
| 130 | // Convert to btvector |
| 131 | btVector3 v[] = {{v0.x, v0.y, v0.z}, |
| 132 | {v1.x, v1.y, v1.z}, |
| 133 | {v2.x, v2.y, v2.z}}; |
| 134 | wm->addTriangle(v[0], v[1], v[2]); |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | if (wm->getNumTriangles() == 0) |
| 140 | { |
| 141 | delete wm; |
| 142 | return Handle::CollisionShapeHandle::makeInvalidHandle(); |
| 143 | } |
| 144 | |
| 145 | Handle::CollisionShapeHandle csh = m_CollisionShapeAllocator.createObject(); |
| 146 | CollisionShape& cs = getCollisionShape(csh); |
| 147 | cs.collisionShape = new btBvhTriangleMeshShape(wm, true); |
| 148 | cs.shapeType = CollisionShape::TriangleMesh; |
| 149 | cs.collisionType = type; |
| 150 | |
| 151 | cs.collisionShape->setUserIndex(csh.index); |
| 152 | |
| 153 | if (!name.empty()) |
| 154 | m_ShapeCache[name] = csh; |
| 155 | |
| 156 | return csh; |
| 157 | } |
| 158 | |
| 159 | Handle::CollisionShapeHandle PhysicsSystem::makeCollisionShapeFromMesh(const std::vector<ZenLoad::WorldTriangle>& triangles, |
| 160 | CollisionShape::ECollisionType type, |
no test coverage detected