example that shows how to register your own collision algorithms.

This commit is contained in:
ejcoumans
2006-09-17 01:39:54 +00:00
parent be91e38bcc
commit a98b7f1a95
4 changed files with 384 additions and 0 deletions

View File

@@ -0,0 +1,81 @@
/*
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 "SphereSphereCollisionAlgorithm.h"
#include "CollisionDispatch/CollisionDispatcher.h"
#include "CollisionShapes/SphereShape.h"
#include "CollisionDispatch/CollisionObject.h"
SphereSphereCollisionAlgorithm::SphereSphereCollisionAlgorithm(PersistentManifold* mf,const CollisionAlgorithmConstructionInfo& ci,BroadphaseProxy* proxy0,BroadphaseProxy* proxy1)
: CollisionAlgorithm(ci),
m_ownManifold(false),
m_manifoldPtr(mf)
{
if (!m_manifoldPtr && m_dispatcher->NeedsCollision(*proxy0,*proxy1))
{
m_manifoldPtr = m_dispatcher->GetNewManifold(proxy0->m_clientObject,proxy1->m_clientObject);
m_ownManifold = true;
}
}
SphereSphereCollisionAlgorithm::~SphereSphereCollisionAlgorithm()
{
if (m_ownManifold)
{
if (m_manifoldPtr)
m_dispatcher->ReleaseManifold(m_manifoldPtr);
}
}
void SphereSphereCollisionAlgorithm::ProcessCollision (BroadphaseProxy* proxy0,BroadphaseProxy* proxy1,const DispatcherInfo& dispatchInfo)
{
if (!m_manifoldPtr)
return;
CollisionObject* col0 = static_cast<CollisionObject*>(proxy0->m_clientObject);
CollisionObject* col1 = static_cast<CollisionObject*>(proxy1->m_clientObject);
SphereShape* sphere0 = (SphereShape*)col0->m_collisionShape;
SphereShape* sphere1 = (SphereShape*)col1->m_collisionShape;
SimdVector3 diff = col0->m_worldTransform.getOrigin()- col1->m_worldTransform.getOrigin();
float len = diff.length();
SimdScalar radius0 = sphere0->GetRadius();
SimdScalar radius1 = sphere1->GetRadius();
///iff distance positive, don't generate a new contact
if ( len > (radius0+radius1))
return;
///distance (negative means penetration)
SimdScalar dist = len - (radius0+radius1);
SimdVector3 normalOnSurfaceB = diff / len;
///point on A (worldspace)
SimdVector3 pos0 = col0->m_worldTransform.getOrigin() - radius0 * normalOnSurfaceB;
///point on B (worldspace)
SimdVector3 pos1 = col1->m_worldTransform.getOrigin() + radius1* normalOnSurfaceB;
/// report a contact. internally this will be kept persistent, and contact reduction is done
ManifoldResult* resultOut = m_dispatcher->GetNewManifoldResult(col0,col1,m_manifoldPtr);
resultOut->AddContactPoint(normalOnSurfaceB,pos1,dist);
m_dispatcher->ReleaseManifoldResult(resultOut);
}
float SphereSphereCollisionAlgorithm::CalculateTimeOfImpact(BroadphaseProxy* proxy0,BroadphaseProxy* proxy1,const DispatcherInfo& dispatchInfo)
{
//not yet
return 1.f;
}

View File

@@ -0,0 +1,54 @@
/*
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 SPHERE_SPHERE_COLLISION_ALGORITHM_H
#define SPHERE_SPHERE_COLLISION_ALGORITHM_H
#include "BroadphaseCollision/CollisionAlgorithm.h"
#include "BroadphaseCollision/BroadphaseProxy.h"
#include "CollisionDispatch/CollisionCreateFunc.h"
class PersistentManifold;
/// SphereSphereCollisionAlgorithm provides sphere-sphere collision detection.
/// Other features are frame-coherency (persistent data) and collision response.
/// Also provides the most basic sample for custom/user CollisionAlgorithm
class SphereSphereCollisionAlgorithm : public CollisionAlgorithm
{
bool m_ownManifold;
PersistentManifold* m_manifoldPtr;
public:
SphereSphereCollisionAlgorithm(const CollisionAlgorithmConstructionInfo& ci)
: CollisionAlgorithm(ci) {}
virtual void ProcessCollision (BroadphaseProxy* proxy0,BroadphaseProxy* proxy1,const DispatcherInfo& dispatchInfo);
virtual float CalculateTimeOfImpact(BroadphaseProxy* proxy0,BroadphaseProxy* proxy1,const DispatcherInfo& dispatchInfo);
SphereSphereCollisionAlgorithm(PersistentManifold* mf,const CollisionAlgorithmConstructionInfo& ci,BroadphaseProxy* proxy0,BroadphaseProxy* proxy1);
virtual ~SphereSphereCollisionAlgorithm();
struct CreateFunc :public CollisionAlgorithmCreateFunc
{
virtual CollisionAlgorithm* CreateCollisionAlgorithm(CollisionAlgorithmConstructionInfo& ci, BroadphaseProxy* proxy0,BroadphaseProxy* proxy1)
{
return new SphereSphereCollisionAlgorithm(0,ci,proxy0,proxy1);
}
};
};
#endif //SPHERE_SPHERE_COLLISION_ALGORITHM_H

View File

@@ -0,0 +1,214 @@
/*
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 "CcdPhysicsEnvironment.h"
#include "CcdPhysicsController.h"
#include "CollisionShapes/BoxShape.h"
#include "CollisionShapes/SphereShape.h"
#include "CollisionShapes/Simplex1to4Shape.h"
#include "Dynamics/RigidBody.h"
#include "BroadphaseCollision/AxisSweep3.h"
#include "ConstraintSolver/SequentialImpulseConstraintSolver.h"
#include "CollisionDispatch/CollisionDispatcher.h"
#include "BroadphaseCollision/SimpleBroadphase.h"
#include "CollisionShapes/TriangleMeshShape.h"
#include "CollisionShapes/TriangleIndexVertexArray.h"
#include "CollisionShapes/BvhTriangleMeshShape.h"
#include "CollisionShapes/TriangleMesh.h"
#include "IDebugDraw.h"
#include "GLDebugDrawer.h"
#include "PHY_Pro.h"
#include "UserCollisionAlgorithm.h"
#include "GL_ShapeDrawer.h"
#include "GlutStuff.h"
//The user defined collision algorithm
#include "SphereSphereCollisionAlgorithm.h"
GLDebugDrawer debugDrawer;
static const int NUM_VERTICES = 5;
static const int NUM_TRIANGLES=4;
SimdVector3 gVertices[NUM_VERTICES];
int gIndices[NUM_TRIANGLES*3];
const float TRIANGLE_SIZE=80.f;
///User can override this material combiner by implementing gContactAddedCallback and setting body0->m_collisionFlags |= CollisionObject::customMaterialCallback;
inline SimdScalar calculateCombinedFriction(float friction0,float friction1)
{
SimdScalar friction = friction0 * friction1;
const SimdScalar MAX_FRICTION = 10.f;
if (friction < -MAX_FRICTION)
friction = -MAX_FRICTION;
if (friction > MAX_FRICTION)
friction = MAX_FRICTION;
return friction;
}
inline SimdScalar calculateCombinedRestitution(float restitution0,float restitution1)
{
return restitution0 * restitution1;
}
int main(int argc,char** argv)
{
UserCollisionAlgorithm* userCollisionAlgorithm = new UserCollisionAlgorithm;
userCollisionAlgorithm->initPhysics();
userCollisionAlgorithm->setCameraDistance(30.f);
return glutmain(argc, argv,640,480,"Static Concave Mesh Demo",userCollisionAlgorithm);
}
void UserCollisionAlgorithm::initPhysics()
{
#define TRISIZE 10.f
int vertStride = sizeof(SimdVector3);
int indexStride = 3*sizeof(int);
const int NUM_VERTS_X = 50;
const int NUM_VERTS_Y = 50;
const int totalVerts = NUM_VERTS_X*NUM_VERTS_Y;
const int totalTriangles = 2*(NUM_VERTS_X-1)*(NUM_VERTS_Y-1);
SimdVector3* gVertices = new SimdVector3[totalVerts];
int* gIndices = new int[totalTriangles*3];
int i;
for ( i=0;i<NUM_VERTS_X;i++)
{
for (int j=0;j<NUM_VERTS_Y;j++)
{
gVertices[i+j*NUM_VERTS_X].setValue((i-NUM_VERTS_X*0.5f)*TRIANGLE_SIZE,2.f*sinf((float)i)*cosf((float)j),(j-NUM_VERTS_Y*0.5f)*TRIANGLE_SIZE);
}
}
int index=0;
for ( i=0;i<NUM_VERTS_X-1;i++)
{
for (int j=0;j<NUM_VERTS_Y-1;j++)
{
gIndices[index++] = j*NUM_VERTS_X+i;
gIndices[index++] = j*NUM_VERTS_X+i+1;
gIndices[index++] = (j+1)*NUM_VERTS_X+i+1;
gIndices[index++] = j*NUM_VERTS_X+i;
gIndices[index++] = (j+1)*NUM_VERTS_X+i+1;
gIndices[index++] = (j+1)*NUM_VERTS_X+i;
}
}
TriangleIndexVertexArray* indexVertexArrays = new TriangleIndexVertexArray(totalTriangles,
gIndices,
indexStride,
totalVerts,(float*) &gVertices[0].x(),vertStride);
CollisionShape* trimeshShape = new BvhTriangleMeshShape(indexVertexArrays);
ConstraintSolver* solver = new SequentialImpulseConstraintSolver;
CollisionDispatcher* dispatcher = new CollisionDispatcher();
OverlappingPairCache* broadphase = new SimpleBroadphase();
dispatcher->RegisterCollisionCreateFunc(SPHERE_SHAPE_PROXYTYPE,SPHERE_SHAPE_PROXYTYPE,new SphereSphereCollisionAlgorithm::CreateFunc);
m_physicsEnvironmentPtr = new CcdPhysicsEnvironment(dispatcher,broadphase);
bool isDynamic = false;
float mass = 0.f;
SimdTransform startTransform;
startTransform.setIdentity();
startTransform.setOrigin(SimdVector3(0,-2,0));
CcdPhysicsController* staticTrimesh = LocalCreatePhysicsObject(isDynamic, mass, startTransform,trimeshShape);
//enable custom material callback
staticTrimesh->GetRigidBody()->m_collisionFlags |= CollisionObject::customMaterialCallback;
{
for (int i=0;i<10;i++)
{
CollisionShape* sphereShape = new SphereShape(1);
startTransform.setOrigin(SimdVector3(1,2*i,1));
CcdPhysicsController* boxRigidBody = LocalCreatePhysicsObject(true, 1, startTransform,sphereShape);
}
}
m_physicsEnvironmentPtr->setGravity(-1,-10,1);
m_physicsEnvironmentPtr->setDebugDrawer(&debugDrawer);
}
void UserCollisionAlgorithm::clientMoveAndDisplay()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
float deltaTime = 1.f/60.f;
m_physicsEnvironmentPtr->proceedDeltaTime(0.f,deltaTime);
renderme();
glFlush();
glutSwapBuffers();
}
void UserCollisionAlgorithm::clientResetScene()
{
int numObj = m_physicsEnvironmentPtr->GetNumControllers();
//skip ground
for (int i=1;i<numObj;i++)
{
CcdPhysicsController* ctrl = m_physicsEnvironmentPtr->GetPhysicsController(i);
ctrl->setPosition(1,2*i,1);
ctrl->setOrientation(0,0,0,1);
ctrl->SetLinearVelocity(0,0,0,0);
ctrl->SetAngularVelocity(0,0,0,0);
}
}
void UserCollisionAlgorithm::displayCallback(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
renderme();
glFlush();
glutSwapBuffers();
}

View File

@@ -0,0 +1,35 @@
/*
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 USER_COLLISION_ALGORITHM_DEMO_H
#define USER_COLLISION_ALGORITHM_DEMO_H
#include "DemoApplication.h"
///UserCollisionAlgorithmDemo shows how to register and use your own collision algorithm for a certain shape pair type
class UserCollisionAlgorithm : public DemoApplication
{
public:
void initPhysics();
virtual void clientMoveAndDisplay();
virtual void displayCallback();
virtual void clientResetScene();
};
#endif //USER_COLLISION_ALGORITHM_DEMO_H