From 7866fcf78c9bd44a2e0e9979136e05ec2c56070e Mon Sep 17 00:00:00 2001 From: Andrew Meadows Date: Wed, 18 May 2016 23:23:11 -0700 Subject: [PATCH] trim high-point convex hulls --- .../physics/src/PhysicalEntitySimulation.cpp | 7 +++++++ libraries/physics/src/ShapeFactory.cpp | 18 ++++++++++++++++++ libraries/shared/src/ShapeInfo.cpp | 12 ++++++++++++ libraries/shared/src/ShapeInfo.h | 1 + 4 files changed, 38 insertions(+) diff --git a/libraries/physics/src/PhysicalEntitySimulation.cpp b/libraries/physics/src/PhysicalEntitySimulation.cpp index 3fbf8ffaf5..95ae45bbb2 100644 --- a/libraries/physics/src/PhysicalEntitySimulation.cpp +++ b/libraries/physics/src/PhysicalEntitySimulation.cpp @@ -217,6 +217,13 @@ void PhysicalEntitySimulation::getObjectsToAddToPhysics(VectorOfMotionStates& re } else if (entity->isReadyToComputeShape()) { ShapeInfo shapeInfo; entity->computeShapeInfo(shapeInfo); + int numPoints = shapeInfo.getMaxNumPoints(); + const int MAX_HULL_POINTS = 42; + if (numPoints > MAX_HULL_POINTS) { + glm::vec3 p = entity->getPosition(); + qDebug() << "entity" << entity->getName() << "at <" << p.x << p.y << p.z << ">" + << "has convex hull with" << numPoints << "points"; + } btCollisionShape* shape = ObjectMotionState::getShapeManager()->getShape(shapeInfo); if (shape) { EntityMotionState* motionState = new EntityMotionState(shape, entity); diff --git a/libraries/physics/src/ShapeFactory.cpp b/libraries/physics/src/ShapeFactory.cpp index de3e9cc794..8641873540 100644 --- a/libraries/physics/src/ShapeFactory.cpp +++ b/libraries/physics/src/ShapeFactory.cpp @@ -10,6 +10,7 @@ // #include +#include #include // for MILLIMETERS_PER_METER @@ -64,6 +65,23 @@ btConvexHullShape* ShapeFactory::createConvexHull(const QVector& poin correctedPoint = (points[i] - center) * relativeScale + center; hull->addPoint(btVector3(correctedPoint[0], correctedPoint[1], correctedPoint[2]), false); } + + const int MAX_HULL_POINTS = 42; + if (points.size() > MAX_HULL_POINTS) { + // create hull approximation + btShapeHull* shapeHull = new btShapeHull(hull); + shapeHull->buildHull(margin); + btConvexHullShape* newHull = new btConvexHullShape(); + const btVector3* newPoints = shapeHull->getVertexPointer(); + for (int i = 0; i < shapeHull->numVertices(); ++i) { + newHull->addPoint(newPoints[i], false); + } + delete hull; + delete shapeHull; + hull = newHull; + qDebug() << "reduced hull with" << points.size() << "points down to" << hull->getNumPoints(); // TODO: remove after testing + } + hull->recalcLocalAabb(); return hull; } diff --git a/libraries/shared/src/ShapeInfo.cpp b/libraries/shared/src/ShapeInfo.cpp index 1d0cd56b86..0974c88e73 100644 --- a/libraries/shared/src/ShapeInfo.cpp +++ b/libraries/shared/src/ShapeInfo.cpp @@ -99,6 +99,18 @@ uint32_t ShapeInfo::getNumSubShapes() const { } return 1; } + +int ShapeInfo::getMaxNumPoints() const { + int numPoints = 0; + for (int i = 0; i < _points.size(); ++i) { + int n = _points[i].size(); + if (n > numPoints) { + numPoints = n; + } + } + return numPoints; +} + float ShapeInfo::computeVolume() const { const float DEFAULT_VOLUME = 1.0f; float volume = DEFAULT_VOLUME; diff --git a/libraries/shared/src/ShapeInfo.h b/libraries/shared/src/ShapeInfo.h index 79390b6680..71fa12d5b5 100644 --- a/libraries/shared/src/ShapeInfo.h +++ b/libraries/shared/src/ShapeInfo.h @@ -61,6 +61,7 @@ public: void clearPoints () { _points.clear(); } void appendToPoints (const QVector& newPoints) { _points << newPoints; } + int getMaxNumPoints() const; float computeVolume() const;