RenderableEntityItem uses collision hull in contains

This commit is contained in:
Atlante45 2015-04-20 17:21:27 +02:00
parent 8db9f80779
commit 955723acf1
4 changed files with 43 additions and 0 deletions

View file

@ -415,3 +415,35 @@ void RenderableModelEntityItem::computeShapeInfo(ShapeInfo& info) {
}
}
bool RenderableModelEntityItem::contains(const glm::vec3& point) const {
bool result = EntityItem::contains(point);
if (result && _model && _model->getCollisionGeometry()) {
const QSharedPointer<NetworkGeometry> collisionNetworkGeometry = _model->getCollisionGeometry();
const FBXGeometry& collisionGeometry = collisionNetworkGeometry->getFBXGeometry();
auto checkEachPrimitive = [=](FBXMesh& mesh, QVector<int> indices, int primitiveSize) -> bool {
for (unsigned int j = 0; j < indices.size(); j += primitiveSize) {
if (!isPointBehindTrianglesPlane(point,
mesh.vertices[indices[j]],
mesh.vertices[indices[j + 1]],
mesh.vertices[indices[j + 2]])) {
return false;
}
}
return true;
};
for (auto mesh : collisionGeometry.meshes) {
for (auto part : mesh.parts) {
// run through all the triangles and quads
if (!checkEachPrimitive(mesh, part.triangleIndices, 3) ||
!checkEachPrimitive(mesh, part.quadIndices, 4)) {
return false;
}
}
}
}
return result;
}

View file

@ -56,6 +56,8 @@ public:
bool isReadyToComputeShape();
void computeShapeInfo(ShapeInfo& info);
virtual bool contains(const glm::vec3& point) const;
private:
void remapTextures();

View file

@ -203,6 +203,13 @@ glm::quat rotationBetween(const glm::vec3& v1, const glm::vec3& v2) {
return glm::angleAxis(angle, axis);
}
bool isPointBehindTrianglesPlane(glm::vec3 point, glm::vec3 p0, glm::vec3 p1, glm::vec3 p2) {
glm::vec3 v1 = p0 - p1, v2 = p2 - p1; // Non-collinear vectors contained in the plane
glm::vec3 n = glm::cross(v1, v2); // Plane's normal vector, pointing out of the triangle
float d = -glm::dot(n, p0); // Compute plane's equation constant
return (glm::dot(n, point) + d) <= 0;
}
glm::vec3 extractTranslation(const glm::mat4& matrix) {
return glm::vec3(matrix[3][0], matrix[3][1], matrix[3][2]);
}

View file

@ -71,6 +71,8 @@ float angleBetween(const glm::vec3& v1, const glm::vec3& v2);
glm::quat rotationBetween(const glm::vec3& v1, const glm::vec3& v2);
bool isPointBehindTrianglesPlane(glm::vec3 point, glm::vec3 p0, glm::vec3 p1, glm::vec3 p2);
glm::vec3 extractTranslation(const glm::mat4& matrix);
void setTranslation(glm::mat4& matrix, const glm::vec3& translation);