Fix issues related to local scaling in ConvexMeshShape

This commit is contained in:
Daniel Chappuis 2016-01-12 22:48:14 +01:00
parent a64d737f67
commit a5f39de375
3 changed files with 22 additions and 40 deletions

View File

@ -102,7 +102,7 @@ class ConcaveMeshRaycastCallback : public DynamicAABBTreeRaycastCallback {
// Class ConcaveMeshShape // Class ConcaveMeshShape
/** /**
* This class represents a concave mesh shape. Note that collision detection * This class represents a static concave mesh shape. Note that collision detection
* with a concave mesh shape can be very expensive. You should use only use * with a concave mesh shape can be very expensive. You should use only use
* this shape for a static mesh. * this shape for a static mesh.
*/ */
@ -129,14 +129,6 @@ class ConcaveMeshShape : public ConcaveShape {
/// Private assignment operator /// Private assignment operator
ConcaveMeshShape& operator=(const ConcaveMeshShape& shape); ConcaveMeshShape& operator=(const ConcaveMeshShape& shape);
/// Return a local support point in a given direction with the object margin
virtual Vector3 getLocalSupportPointWithMargin(const Vector3& direction,
void** cachedCollisionData) const;
/// Return a local support point in a given direction without the object margin
virtual Vector3 getLocalSupportPointWithoutMargin(const Vector3& direction,
void** cachedCollisionData) const;
/// Raycast method with feedback information /// Raycast method with feedback information
virtual bool raycast(const Ray& ray, RaycastInfo& raycastInfo, ProxyShape* proxyShape) const; virtual bool raycast(const Ray& ray, RaycastInfo& raycastInfo, ProxyShape* proxyShape) const;
@ -188,23 +180,6 @@ inline size_t ConcaveMeshShape::getSizeInBytes() const {
return sizeof(ConcaveMeshShape); return sizeof(ConcaveMeshShape);
} }
// Return a local support point in a given direction with the object margin
inline Vector3 ConcaveMeshShape::getLocalSupportPointWithMargin(const Vector3& direction,
void** cachedCollisionData) const {
// Should not be used
assert(false);
return Vector3(0, 0, 0);
}
// Return a local support point in a given direction without the object margin
inline Vector3 ConcaveMeshShape::getLocalSupportPointWithoutMargin(const Vector3& direction,
void** cachedCollisionData) const {
// Should not be used
assert(false);
return Vector3(0.0, 0.0, 0.0);
}
// Return the local bounds of the shape in x, y and z directions. // Return the local bounds of the shape in x, y and z directions.
// This method is used to compute the AABB of the box // This method is used to compute the AABB of the box
/** /**
@ -232,7 +207,7 @@ inline void ConcaveMeshShape::setLocalScaling(const Vector3& scaling) {
initBVHTree(); initBVHTree();
} }
// Return the local inertia tensor of the sphere // Return the local inertia tensor of the shape
/** /**
* @param[out] tensor The 3x3 inertia tensor matrix of the shape in local-space * @param[out] tensor The 3x3 inertia tensor matrix of the shape in local-space
* coordinates * coordinates

View File

@ -38,8 +38,7 @@ using namespace reactphysics3d;
* @param stride Stride between the beginning of two elements in the vertices array * @param stride Stride between the beginning of two elements in the vertices array
* @param margin Collision margin (in meters) around the collision shape * @param margin Collision margin (in meters) around the collision shape
*/ */
ConvexMeshShape::ConvexMeshShape(const decimal* arrayVertices, uint nbVertices, int stride, ConvexMeshShape::ConvexMeshShape(const decimal* arrayVertices, uint nbVertices, int stride, decimal margin)
decimal margin)
: ConvexShape(CONVEX_MESH, margin), mNbVertices(nbVertices), mMinBounds(0, 0, 0), : ConvexShape(CONVEX_MESH, margin), mNbVertices(nbVertices), mMinBounds(0, 0, 0),
mMaxBounds(0, 0, 0), mIsEdgesInformationUsed(false) { mMaxBounds(0, 0, 0), mIsEdgesInformationUsed(false) {
assert(nbVertices > 0); assert(nbVertices > 0);
@ -268,6 +267,10 @@ void ConvexMeshShape::recalculateBounds() {
if (mVertices[i].z < mMinBounds.z) mMinBounds.z = mVertices[i].z; if (mVertices[i].z < mMinBounds.z) mMinBounds.z = mVertices[i].z;
} }
// Apply the local scaling factor
mMaxBounds = mMaxBounds * mScaling;
mMinBounds = mMinBounds * mScaling;
// Add the object margin to the bounds // Add the object margin to the bounds
mMaxBounds += Vector3(mMargin, mMargin, mMargin); mMaxBounds += Vector3(mMargin, mMargin, mMargin);
mMinBounds -= Vector3(mMargin, mMargin, mMargin); mMinBounds -= Vector3(mMargin, mMargin, mMargin);

View File

@ -42,11 +42,6 @@ namespace reactphysics3d {
// Declaration // Declaration
class CollisionWorld; class CollisionWorld;
// TODO : Make possible to create a ConvexMeshShape using a TriangleMesh as for
// the ConcaveMeshShape
// TODO : Check that scaling factor is working after the change of the previous TODO
// Class ConvexMeshShape // Class ConvexMeshShape
/** /**
* This class represents a convex mesh shape. In order to create a convex mesh shape, you * This class represents a convex mesh shape. In order to create a convex mesh shape, you
@ -99,6 +94,9 @@ class ConvexMeshShape : public ConvexShape {
/// Recompute the bounds of the mesh /// Recompute the bounds of the mesh
void recalculateBounds(); void recalculateBounds();
/// Set the scaling vector of the collision shape
virtual void setLocalScaling(const Vector3& scaling);
/// Return a local support point in a given direction with the object margin /// Return a local support point in a given direction with the object margin
virtual Vector3 getLocalSupportPointWithMargin(const Vector3& direction, virtual Vector3 getLocalSupportPointWithMargin(const Vector3& direction,
void** cachedCollisionData) const; void** cachedCollisionData) const;
@ -154,6 +152,12 @@ class ConvexMeshShape : public ConvexShape {
void setIsEdgesInformationUsed(bool isEdgesUsed); void setIsEdgesInformationUsed(bool isEdgesUsed);
}; };
/// Set the scaling vector of the collision shape
inline void ConvexMeshShape::setLocalScaling(const Vector3& scaling) {
ConvexShape::setLocalScaling(scaling);
recalculateBounds();
}
// Return the number of bytes used by the collision shape // Return the number of bytes used by the collision shape
inline size_t ConvexMeshShape::getSizeInBytes() const { inline size_t ConvexMeshShape::getSizeInBytes() const {
return sizeof(ConvexMeshShape); return sizeof(ConvexMeshShape);
@ -200,12 +204,12 @@ inline void ConvexMeshShape::addVertex(const Vector3& vertex) {
mNbVertices++; mNbVertices++;
// Update the bounds of the mesh // Update the bounds of the mesh
if (vertex.x > mMaxBounds.x) mMaxBounds.x = vertex.x; if (vertex.x * mScaling.x > mMaxBounds.x) mMaxBounds.x = vertex.x * mScaling.x;
if (vertex.x < mMinBounds.x) mMinBounds.x = vertex.x; if (vertex.x * mScaling.x < mMinBounds.x) mMinBounds.x = vertex.x * mScaling.x;
if (vertex.y > mMaxBounds.y) mMaxBounds.y = vertex.y; if (vertex.y * mScaling.y > mMaxBounds.y) mMaxBounds.y = vertex.y * mScaling.y;
if (vertex.y < mMinBounds.y) mMinBounds.y = vertex.y; if (vertex.y * mScaling.y < mMinBounds.y) mMinBounds.y = vertex.y * mScaling.y;
if (vertex.z > mMaxBounds.z) mMaxBounds.z = vertex.z; if (vertex.z * mScaling.z > mMaxBounds.z) mMaxBounds.z = vertex.z * mScaling.z;
if (vertex.z < mMinBounds.z) mMinBounds.z = vertex.z; if (vertex.z * mScaling.z < mMinBounds.z) mMinBounds.z = vertex.z * mScaling.z;
} }
// Add an edge into the convex mesh by specifying the two vertex indices of the edge. // Add an edge into the convex mesh by specifying the two vertex indices of the edge.