refactoring of TriangleMeshShape, introduced ConcaveShape, which allows for StaticPlaneShape and future landscape/heightfield shape

This commit is contained in:
ejcoumans 2006-07-03 22:41:19 +00:00
parent e190b3cb79
commit 62593f8b99
14 changed files with 277 additions and 61 deletions

View File

@ -43,6 +43,9 @@ CONCAVE_SHAPES_START_HERE,
//keep all the convex shapetype below here, for the check IsConvexShape in broadphase proxy!
TRIANGLE_MESH_SHAPE_PROXYTYPE,
EMPTY_SHAPE_PROXYTYPE,
STATIC_PLANE_PROXYTYPE,
CONCAVE_SHAPES_END_HERE,
COMPOUND_SHAPE_PROXYTYPE,
MAX_BROADPHASE_COLLISION_TYPES

View File

@ -87,7 +87,7 @@ public:
virtual void ReleaseManifoldResult(ManifoldResult*)=0;
virtual void DispatchAllCollisionPairs(class BroadphasePair* pairs,int numPairs,DispatcherInfo& dispatchInfo)=0;
virtual void DispatchAllCollisionPairs(struct BroadphasePair* pairs,int numPairs,DispatcherInfo& dispatchInfo)=0;
virtual int GetNumManifolds() const = 0;

View File

@ -19,11 +19,10 @@ subject to the following restrictions:
#include "CollisionShapes/MultiSphereShape.h"
#include "ConvexConvexAlgorithm.h"
#include "BroadphaseCollision/BroadphaseProxy.h"
#include "CollisionShapes/TriangleShape.h"
#include "CollisionShapes/ConcaveShape.h"
#include "CollisionDispatch/ManifoldResult.h"
#include "NarrowPhaseCollision/RaycastCallback.h"
#include "CollisionShapes/TriangleMeshShape.h"
#include "CollisionShapes/TriangleShape.h"
ConvexConcaveCollisionAlgorithm::ConvexConcaveCollisionAlgorithm( const CollisionAlgorithmConstructionInfo& ci,BroadphaseProxy* proxy0,BroadphaseProxy* proxy1)
: CollisionAlgorithm(ci),m_convex(*proxy0),m_concave(*proxy1),
@ -142,7 +141,7 @@ void ConvexConcaveCollisionAlgorithm::ProcessCollision (BroadphaseProxy* ,Broadp
CollisionObject* convexBody = static_cast<CollisionObject* >(m_convex.m_clientObject);
CollisionObject* triBody = static_cast<CollisionObject* >(m_concave.m_clientObject);
if (triBody->m_collisionShape->GetShapeType() == TRIANGLE_MESH_SHAPE_PROXYTYPE)
if (triBody->m_collisionShape->IsConcave())
{
if (!m_dispatcher->NeedsCollision(m_convex,m_concave))
@ -151,11 +150,11 @@ void ConvexConcaveCollisionAlgorithm::ProcessCollision (BroadphaseProxy* ,Broadp
CollisionObject* triOb = static_cast<CollisionObject*>(m_concave.m_clientObject);
TriangleMeshShape* triangleMesh = static_cast<TriangleMeshShape*>( triOb->m_collisionShape);
ConcaveShape* concaveShape = static_cast<ConcaveShape*>( triOb->m_collisionShape);
if (convexBody->m_collisionShape->IsConvex())
{
float collisionMarginTriangle = triangleMesh->GetMargin();
float collisionMarginTriangle = concaveShape->GetMargin();
m_ConvexTriangleCallback.SetTimeStepAndCounters(collisionMarginTriangle,dispatchInfo);
@ -165,7 +164,7 @@ void ConvexConcaveCollisionAlgorithm::ProcessCollision (BroadphaseProxy* ,Broadp
m_ConvexTriangleCallback.m_manifoldPtr->SetBodies(m_convex.m_clientObject,m_concave.m_clientObject);
triangleMesh->ProcessAllTriangles( &m_ConvexTriangleCallback,m_ConvexTriangleCallback.GetAabbMin(),m_ConvexTriangleCallback.GetAabbMax());
concaveShape->ProcessAllTriangles( &m_ConvexTriangleCallback,m_ConvexTriangleCallback.GetAabbMin(),m_ConvexTriangleCallback.GetAabbMax());
}
@ -210,12 +209,12 @@ float ConvexConcaveCollisionAlgorithm::CalculateTimeOfImpact(BroadphaseProxy* ,B
SimdVector3 aabbMin (-1e30f,-1e30f,-1e30f);
SimdVector3 aabbMax (SIMD_INFINITY,SIMD_INFINITY,SIMD_INFINITY);
if (triBody->m_collisionShape->GetShapeType() == TRIANGLE_MESH_SHAPE_PROXYTYPE)
if (triBody->m_collisionShape->IsConcave())
{
CollisionObject* concavebody = (CollisionObject* )m_concave.m_clientObject;
TriangleMeshShape* triangleMesh = (TriangleMeshShape*) concavebody->m_collisionShape;
ConcaveShape* triangleMesh = (ConcaveShape*) concavebody->m_collisionShape;
if (triangleMesh)
{

View File

@ -48,24 +48,12 @@ public:
{
SimdVector3 halfExtents = GetHalfExtents();
SimdVector3 margin(GetMargin(),GetMargin(),GetMargin());
halfExtents -= margin;
SimdVector3 supVertex;
supVertex = SimdPoint3(vec.x() < SimdScalar(0.0f) ? -halfExtents.x() : halfExtents.x(),
vec.y() < SimdScalar(0.0f) ? -halfExtents.y() : halfExtents.y(),
vec.z() < SimdScalar(0.0f) ? -halfExtents.z() : halfExtents.z());
if ( GetMargin()!=0.f )
{
SimdVector3 vecnorm = vec;
if (vecnorm .length2() < (SIMD_EPSILON*SIMD_EPSILON))
{
vecnorm.setValue(-1.f,-1.f,-1.f);
}
vecnorm.normalize();
supVertex+= GetMargin() * vecnorm;
}
return supVertex;
}

View File

@ -59,9 +59,13 @@ public:
}
bool IsConcave() const
{
return (GetShapeType() > CONCAVE_SHAPES_START_HERE);
return ((GetShapeType() > CONCAVE_SHAPES_START_HERE) &&
(GetShapeType() < CONCAVE_SHAPES_END_HERE));
}
bool IsCompound() const
{
return (GetShapeType() == COMPOUND_SHAPE_PROXYTYPE);
}
virtual void setLocalScaling(const SimdVector3& scaling) =0;
virtual const SimdVector3& getLocalScaling() const =0;

View File

@ -0,0 +1,28 @@
/*
Bullet Continuous Collision Detection and Physics Library
Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it freely,
subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "ConcaveShape.h"
ConcaveShape::ConcaveShape() : m_collisionMargin(0.f)
{
}
ConcaveShape::~ConcaveShape()
{
}

View File

@ -0,0 +1,51 @@
/*
Bullet Continuous Collision Detection and Physics Library
Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it freely,
subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#ifndef CONCAVE_SHAPE_H
#define CONCAVE_SHAPE_H
#include "CollisionShapes/CollisionShape.h"
#include "BroadphaseCollision/BroadphaseProxy.h" // for the types
#include "TriangleCallback.h"
///Concave shape proves an interface concave shapes that can produce triangles that overlapping a given AABB.
///Static triangle mesh, infinite plane, height field/landscapes are example that implement this interface.
class ConcaveShape : public CollisionShape
{
protected:
float m_collisionMargin;
public:
ConcaveShape();
virtual ~ConcaveShape();
virtual void ProcessAllTriangles(TriangleCallback* callback,const SimdVector3& aabbMin,const SimdVector3& aabbMax) const = 0;
virtual float GetMargin() const {
return m_collisionMargin;
}
virtual void SetMargin(float collisionMargin)
{
m_collisionMargin = collisionMargin;
}
};
#endif //CONCAVE_SHAPE_H

View File

@ -16,7 +16,7 @@ subject to the following restrictions:
#ifndef EMPTY_SHAPE_H
#define EMPTY_SHAPE_H
#include "CollisionShape.h"
#include "ConcaveShape.h"
#include "SimdVector3.h"
#include "SimdTransform.h"
@ -27,8 +27,9 @@ subject to the following restrictions:
/// EmptyShape is a collision shape without actual collision detection. It can be replaced by another shape during runtime
class EmptyShape : public CollisionShape
/// EmptyShape is a collision shape without actual collision detection.
///It can be replaced by another shape during runtime
class EmptyShape : public ConcaveShape
{
public:
EmptyShape();
@ -53,22 +54,13 @@ public:
virtual int GetShapeType() const { return EMPTY_SHAPE_PROXYTYPE;}
virtual void SetMargin(float margin)
{
m_collisionMargin = margin;
}
virtual float GetMargin() const
{
return m_collisionMargin;
}
virtual char* GetName()const
{
return "Empty";
}
private:
SimdScalar m_collisionMargin;
protected:
SimdVector3 m_localScaling;

View File

@ -0,0 +1,98 @@
/*
Bullet Continuous Collision Detection and Physics Library
Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it freely,
subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "StaticPlaneShape.h"
#include "SimdTransformUtil.h"
StaticPlaneShape::StaticPlaneShape(const SimdVector3& planeNormal,SimdScalar planeConstant)
:m_planeNormal(planeNormal),
m_planeConstant(planeConstant),
m_localScaling(0.f,0.f,0.f)
{
}
StaticPlaneShape::~StaticPlaneShape()
{
}
void StaticPlaneShape::GetAabb(const SimdTransform& t,SimdVector3& aabbMin,SimdVector3& aabbMax) const
{
SimdVector3 infvec (1e30f,1e30f,1e30f);
SimdVector3 center = m_planeNormal*m_planeConstant;
aabbMin = center + infvec*m_planeNormal;
aabbMax = aabbMin;
aabbMin.setMin(center - infvec*m_planeNormal);
aabbMax.setMax(center - infvec*m_planeNormal);
aabbMin.setValue(-1e30f,-1e30f,-1e30f);
aabbMax.setValue(1e30f,1e30f,1e30f);
}
void StaticPlaneShape::ProcessAllTriangles(TriangleCallback* callback,const SimdVector3& aabbMin,const SimdVector3& aabbMax) const
{
SimdVector3 halfExtents = (aabbMax - aabbMin) * 0.5f;
SimdScalar radius = halfExtents.length();
SimdVector3 center = (aabbMax + aabbMin) * 0.5f;
//this is where the triangles are generated, given AABB and plane equation (normal/constant)
SimdVector3 tangentDir0,tangentDir1;
SimdPlaneSpace1(m_planeNormal,tangentDir0,tangentDir1);
SimdVector3 supVertex0,supVertex1;
SimdVector3 projectedCenter = center - (m_planeNormal.dot(center) - m_planeConstant)*m_planeNormal;
SimdVector3 triangle[3];
triangle[0] = projectedCenter + tangentDir0*radius + tangentDir1*radius;
triangle[1] = projectedCenter + tangentDir0*radius - tangentDir1*radius;
triangle[2] = projectedCenter - tangentDir0*radius - tangentDir1*radius;
callback->ProcessTriangle(triangle,0,0);
triangle[0] = projectedCenter - tangentDir0*radius - tangentDir1*radius;
triangle[1] = projectedCenter - tangentDir0*radius + tangentDir1*radius;
triangle[2] = projectedCenter + tangentDir0*radius + tangentDir1*radius;
callback->ProcessTriangle(triangle,0,1);
}
void StaticPlaneShape::CalculateLocalInertia(SimdScalar mass,SimdVector3& inertia)
{
//moving concave objects not supported
inertia.setValue(0.f,0.f,0.f);
}
void StaticPlaneShape::setLocalScaling(const SimdVector3& scaling)
{
m_localScaling = scaling;
}
const SimdVector3& StaticPlaneShape::getLocalScaling() const
{
return m_localScaling;
}

View File

@ -0,0 +1,61 @@
/*
Bullet Continuous Collision Detection and Physics Library
Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it freely,
subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#ifndef STATIC_PLANE_SHAPE_H
#define STATIC_PLANE_SHAPE_H
#include "CollisionShapes/ConcaveShape.h"
///StaticPlaneShape simulates an 'infinite' plane by dynamically reporting triangles approximated by intersection of the plane with the AABB.
///Assumed is that the other objects is not also infinite, so a reasonable sized AABB.
class StaticPlaneShape : public ConcaveShape
{
protected:
SimdVector3 m_localAabbMin;
SimdVector3 m_localAabbMax;
SimdVector3 m_planeNormal;
SimdVector3 m_localScaling;
SimdScalar m_planeConstant;
public:
StaticPlaneShape(const SimdVector3& planeNormal,SimdScalar planeConstant);
virtual ~StaticPlaneShape();
virtual int GetShapeType() const
{
return STATIC_PLANE_PROXYTYPE;
}
virtual void GetAabb(const SimdTransform& t,SimdVector3& aabbMin,SimdVector3& aabbMax) const;
virtual void ProcessAllTriangles(TriangleCallback* callback,const SimdVector3& aabbMin,const SimdVector3& aabbMax) const;
virtual void CalculateLocalInertia(SimdScalar mass,SimdVector3& inertia);
virtual void setLocalScaling(const SimdVector3& scaling);
virtual const SimdVector3& getLocalScaling() const;
//debugging
virtual char* GetName()const {return "STATICPLANE";}
};
#endif //STATIC_PLANE_SHAPE_H

View File

@ -23,8 +23,7 @@ subject to the following restrictions:
#include "stdio.h"
TriangleMeshShape::TriangleMeshShape(StridingMeshInterface* meshInterface)
: m_meshInterface(meshInterface),
m_collisionMargin(CONVEX_DISTANCE_MARGIN)
: m_meshInterface(meshInterface)
{
RecalcLocalAabb();
}

View File

@ -16,30 +16,24 @@ subject to the following restrictions:
#ifndef TRIANGLE_MESH_SHAPE_H
#define TRIANGLE_MESH_SHAPE_H
#include "CollisionShapes/CollisionShape.h"
#include "BroadphaseCollision/BroadphaseProxy.h" // for the types
#include "StridingMeshInterface.h"
#include "TriangleCallback.h"
#include "CollisionShapes/ConcaveShape.h"
#include "CollisionShapes/StridingMeshInterface.h"
///Concave triangle mesh. Uses an interface to access the triangles to allow for sharing graphics/physics triangles.
class TriangleMeshShape : public CollisionShape
class TriangleMeshShape : public ConcaveShape
{
protected:
StridingMeshInterface* m_meshInterface;
SimdVector3 m_localAabbMin;
SimdVector3 m_localAabbMax;
float m_collisionMargin;
public:
TriangleMeshShape(StridingMeshInterface* meshInterface);
virtual ~TriangleMeshShape();
virtual SimdVector3 LocalGetSupportingVertex(const SimdVector3& vec) const;
virtual SimdVector3 LocalGetSupportingVertexWithoutMargin(const SimdVector3& vec)const
@ -68,16 +62,6 @@ public:
//debugging
virtual char* GetName()const {return "TRIANGLEMESH";}
virtual float GetMargin() const {
return m_collisionMargin;
}
virtual void SetMargin(float collisionMargin)
{
m_collisionMargin = collisionMargin;
}
};

View File

@ -25,7 +25,7 @@ subject to the following restrictions:
#include "CollisionShapes/BoxShape.h"
#include "CollisionShapes/SphereShape.h"
#include "CollisionShapes/ConeShape.h"
#include "CollisionShapes/StaticPlaneShape.h"
#include "CollisionShapes/Simplex1to4Shape.h"
#include "CollisionShapes/EmptyShape.h"
@ -82,7 +82,7 @@ const int maxOverlap = 65535;
#ifdef _DEBUG
const int numObjects = 120;
const int numObjects = 20;
#else
const int numObjects = 120;
#endif
@ -112,6 +112,8 @@ CollisionShape* shapePtr[numShapes] =
///See http://www.continuousphysics.com/Bullet/phpBB2/viewtopic.php?t=346
new BoxShape (SimdVector3(450,10,450)),
//new StaticPlaneShape(SimdVector3(0,1,0),10),
new BoxShape (SimdVector3(CUBE_HALF_EXTENTS,CUBE_HALF_EXTENTS,CUBE_HALF_EXTENTS)),
new SphereShape (CUBE_HALF_EXTENTS- 0.05f),

View File

@ -25,6 +25,13 @@ subject to the following restrictions:
#define SimdRecipSqrt(x) ((float)(1.0f/SimdSqrt(float(x)))) /* reciprocal square root */
inline SimdVector3 SimdAabbSupport(const SimdVector3& halfExtents,const SimdVector3& supportDir)
{
return SimdVector3(supportDir.x() < SimdScalar(0.0f) ? -halfExtents.x() : halfExtents.x(),
supportDir.y() < SimdScalar(0.0f) ? -halfExtents.y() : halfExtents.y(),
supportDir.z() < SimdScalar(0.0f) ? -halfExtents.z() : halfExtents.z());
}
inline void SimdPlaneSpace1 (const SimdVector3& n, SimdVector3& p, SimdVector3& q)
{