added first draft of C-API, btConvexHullShape takes float* instead of btPoint*, added getGravity to btRigidBody

This commit is contained in:
ejcoumans
2006-10-11 06:07:14 +00:00
parent 919e8def68
commit a337372905
13 changed files with 350 additions and 12 deletions

View File

@@ -70,7 +70,7 @@ public:
startTransform.setIdentity();
startTransform.setOrigin(btVector3(0,0,-10.f));
//this create an internal copy of the vertices
btCollisionShape* shape = new btConvexHullShape(&vertices[0],vertices.size());
btCollisionShape* shape = new btConvexHullShape(&(vertices[0].getX()),vertices.size());
btRigidBody* body = m_demoApp->localCreateRigidBody(mass, startTransform,shape);
}

159
Demos/OpenPL_Demo/CApi.cpp Normal file
View File

@@ -0,0 +1,159 @@
/*
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.
*/
/*
Draft high-level generic physics C-API. For low-level access, use the physics SDK native API's.
Work in progress, functionality will be added on demand.
If possible, use the richer Bullet C++ API, by including <src/btBulletDynamicsCommon.h>
*/
#include "Bullet-C-Api.h"
#include "btBulletDynamicsCommon.h"
/*
Create and Delete a Physics SDK
*/
plPhysicsSdkHandle plNewBulletSdk()
{
return 0;
}
void plDeletePhysicsSdk(plPhysicsSdkHandle physicsSdk)
{
}
/* Dynamics World */
plDynamicsWorldHandle plCreateDynamicsWorld(plPhysicsSdkHandle physicsSdk)
{
return (plDynamicsWorldHandle) new btDiscreteDynamicsWorld;
}
void plDeleteDynamicsWorld(plDynamicsWorldHandle world)
{
btDynamicsWorld* dynamicsWorld = reinterpret_cast< btDynamicsWorld* >(world);
delete dynamicsWorld;
}
void plStepSimulation(plDynamicsWorldHandle world, plReal timeStep)
{
btDynamicsWorld* dynamicsWorld = reinterpret_cast< btDynamicsWorld* >(world);
assert(dynamicsWorld);
dynamicsWorld->stepSimulation(timeStep);
}
void plAddRigidBody(plDynamicsWorldHandle world, plRigidBodyHandle object)
{
btDynamicsWorld* dynamicsWorld = reinterpret_cast< btDynamicsWorld* >(world);
assert(dynamicsWorld);
btRigidBody* body = reinterpret_cast< btRigidBody* >(object);
assert(body);
dynamicsWorld->addRigidBody(body);
}
void plRemoveRigidBody(plDynamicsWorldHandle world, plRigidBodyHandle object)
{
btDynamicsWorld* dynamicsWorld = reinterpret_cast< btDynamicsWorld* >(world);
assert(dynamicsWorld);
btRigidBody* body = reinterpret_cast< btRigidBody* >(object);
assert(body);
dynamicsWorld->removeRigidBody(body);
}
/* Rigid Body */
plRigidBodyHandle plCreateRigidBody( void* user_data, float mass, plCollisionShapeHandle cshape )
{
btTransform trans;
trans.setIdentity();
btVector3 localInertia;
btCollisionShape* shape = reinterpret_cast<btCollisionShape*>( cshape);
assert(shape);
shape->calculateLocalInertia(mass,localInertia);
btRigidBody* body = new btRigidBody(mass, trans,shape,localInertia);
body->m_userObjectPointer = user_data;
return (plRigidBodyHandle) body;
}
void plDeleteRigidBody(plRigidBodyHandle cbody)
{
btRigidBody* body = reinterpret_cast< btRigidBody* >(cbody);
assert(body);
delete body;
}
/* Collision Shape definition */
plCollisionShapeHandle plNewSphereShape(plReal radius)
{
return (plCollisionShapeHandle) new btSphereShape(radius);
}
plCollisionShapeHandle plNewBoxShape(plReal x, plReal y, plReal z)
{
return (plCollisionShapeHandle) new btBoxShape(btVector3(x,y,z));
}
plCollisionShapeHandle plNewCapsuleShape(plReal radius, plReal height)
{
//capsule is convex hull of 2 spheres, so use btMultiSphereShape
btVector3 inertiaHalfExtents(radius,height,radius);
const int numSpheres = 2;
btVector3 positions[numSpheres] = {btVector3(0,height,0),btVector3(0,-height,0)};
btScalar radi[numSpheres] = {radius,radius};
return (plCollisionShapeHandle) new btMultiSphereShape(inertiaHalfExtents,positions,radi,numSpheres);
}
plCollisionShapeHandle plNewConeShape(plReal radius, plReal height)
{
return (plCollisionShapeHandle) new btConeShape(radius,height);
}
plCollisionShapeHandle plNewCylinderShape(plReal radius, plReal height)
{
return (plCollisionShapeHandle) new btCylinderShape(btVector3(radius,height,radius));
}
void plDeleteShape(plCollisionShapeHandle cshape)
{
btCollisionShape* shape = reinterpret_cast<btCollisionShape*>( cshape);
assert(shape);
delete shape;
}
void plSetScaling(plCollisionShapeHandle cshape, plVector3 cscaling)
{
btCollisionShape* shape = reinterpret_cast<btCollisionShape*>( cshape);
assert(shape);
btVector3 scaling(cscaling[0],cscaling[1],cscaling[2]);
shape->setLocalScaling(scaling);
}
void plSetPosition(plRigidBodyHandle object, const plVector3 position)
{
}
void plSetOrientation(plRigidBodyHandle object, const plQuaternion orientation)
{
}
//plRigidBodyHandle plRayCast(plDynamicsWorldHandle world, const plVector3 rayStart, const plVector3 rayEnd, plVector3 hitpoint, plVector3 normal);
// extern plRigidBodyHandle plObjectCast(plDynamicsWorldHandle world, const plVector3 rayStart, const plVector3 rayEnd, plVector3 hitpoint, plVector3 normal);

View File

@@ -0,0 +1,44 @@
#include "Bullet-C-Api.h"
int main()
{
float timeStep = 1.f/60.f;
/* initialize */
plPhysicsSdkHandle sdk = plNewBulletSdk();
plDynamicsWorldHandle world = plCreateDynamicsWorld(sdk);
float radius = 1.f;
plCollisionShapeHandle collisionShape = plNewSphereShape(radius);
void* user_data = 0;/* can point to a graphics object */
float mass = 1.f;
plRigidBodyHandle body = plCreateRigidBody(user_data, mass, collisionShape );
plAddRigidBody(world, body);
plStepSimulation(world,0.1f);
/* cleanup */
plRemoveRigidBody(world, body);
plDeleteRigidBody(body);
plDeleteShape( collisionShape);
plDeleteDynamicsWorld( world);
plDeletePhysicsSdk(sdk);
return 0;
}

View File

@@ -114,7 +114,7 @@ void Raytracer::initPhysics()
};
// btMultiSphereShape* multiSphereShape = new btMultiSphereShape(inertiaHalfExtents,positions,radi,NUM_SPHERES);
btConvexHullShape* convexHullShape = new btConvexHullShape(positions,3);
btConvexHullShape* convexHullShape = new btConvexHullShape(&positions[0].getX(),3);
//choose shape

View File

@@ -1,9 +1,118 @@
/*
Bullet Continuous Collision Detection and Physics Library
Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
///This is just a placeholder for now.
///
///It will contain a C API for high-level Bullet Collision Detection and Physics library.
///
///for now, only C++ interface is available, see btBulletCollisionCommon.h and btBulletDynamicsCommon.h
///
///http://bullet.sf.net
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.
*/
/*
Draft high-level generic physics C-API. For low-level access, use the physics SDK native API's.
Work in progress, functionality will be added on demand.
If possible, use the richer Bullet C++ API, by including "btBulletDynamicsCommon.h"
*/
#ifndef BULLET_C_API_H
#define BULLET_C_API_H
#define PL_DECLARE_HANDLE(name) typedef struct name##__ { int unused; } *name
typedef float plReal;
typedef plReal plVector3[3];
typedef plReal plQuaternion[4];
#ifdef __cplusplus
extern "C" {
#endif
/* Particular physics SDK */
PL_DECLARE_HANDLE(plPhysicsSdkHandle);
/* Dynamics world, belonging to some physics SDK */
PL_DECLARE_HANDLE(plDynamicsWorldHandle);
/* Rigid Body that can be part of a Dynamics World */
PL_DECLARE_HANDLE(plRigidBodyHandle);
/* Collision Shape/Geometry, property of a Rigid Body */
PL_DECLARE_HANDLE(plCollisionShapeHandle);
/* Constraint for Rigid Bodies */
PL_DECLARE_HANDLE(plConstraintHandle);
/*
Create and Delete a Physics SDK
*/
extern plPhysicsSdkHandle plNewBulletSdk(); //this could be also another sdk, like ODE, PhysX etc.
extern void plDeletePhysicsSdk(plPhysicsSdkHandle physicsSdk);
/* Dynamics World */
extern plDynamicsWorldHandle plCreateDynamicsWorld(plPhysicsSdkHandle physicsSdk);
extern void plDeleteDynamicsWorld(plDynamicsWorldHandle world);
extern void plStepSimulation(plDynamicsWorldHandle, plReal timeStep);
extern void plAddRigidBody(plDynamicsWorldHandle world, plRigidBodyHandle object);
extern void plRemoveRigidBody(plDynamicsWorldHandle world, plRigidBodyHandle object);
/* Rigid Body */
extern plRigidBodyHandle plCreateRigidBody( void* user_data, float mass, plCollisionShapeHandle cshape );
extern void plDeleteRigidBody(plRigidBodyHandle body);
/* Collision Shape definition */
extern plCollisionShapeHandle plNewSphereShape(plReal radius);
extern plCollisionShapeHandle plNewBoxShape(plReal x, plReal y, plReal z);
extern plCollisionShapeHandle plNewCapsuleShape(plReal radius, plReal height);
extern plCollisionShapeHandle plNewConeShape(plReal radius, plReal height);
extern plCollisionShapeHandle plNewCylinderShape(plReal radius, plReal height);
extern void plDeleteShape(plCollisionShapeHandle shape);
extern void plSetScaling(plCollisionShapeHandle shape, plVector3 scaling);
/* SOLID has Response Callback/Table/Management */
/* PhysX has Triggers, User Callbacks and filtering */
/* ODE has the typedef void dNearCallback (void *data, dGeomID o1, dGeomID o2); */
typedef void plUpdatedPositionCallback(void* userData, plRigidBodyHandle rbHandle, plVector3 pos);
typedef void plUpdatedOrientationCallback(void* userData, plRigidBodyHandle rbHandle, plQuaternion orientation);
extern void plSetPosition(plRigidBodyHandle object, const plVector3 position);
extern void plSetOrientation(plRigidBodyHandle object, const plQuaternion orientation);
typedef struct plRayCastResult {
plRigidBodyHandle m_body;
plCollisionShapeHandle m_shape;
plVector3 m_positionWorld;
plVector3 m_normalWorld;
} plRayCastResult;
extern int plRayCast(plDynamicsWorldHandle world, const plVector3 rayStart, const plVector3 rayEnd, plRayCastResult res);
/* Sweep API */
/* extern plRigidBodyHandle plObjectCast(plDynamicsWorldHandle world, const plVector3 rayStart, const plVector3 rayEnd, plVector3 hitpoint, plVector3 normal); */
/* Continuous Collision Detection API */
#ifdef __cplusplus
}
#endif
#endif //BULLET_C_API_H

View File

@@ -18,7 +18,8 @@ subject to the following restrictions:
#include "LinearMath/btQuaternion.h"
btConvexHullShape ::btConvexHullShape (btPoint3* points,int numPoints,int stride)
btConvexHullShape ::btConvexHullShape (const float* points,int numPoints,int stride)
{
m_points.resize(numPoints);
unsigned char* pointsBaseAddress = (unsigned char*)points;

View File

@@ -30,7 +30,10 @@ class btConvexHullShape : public btPolyhedralConvexShape
std::vector<btPoint3> m_points;
public:
btConvexHullShape(btPoint3* points,int numPoints, int stride=sizeof(btPoint3));
///this constructor optionally takes in a pointer to points. Each point is assumed to be 3 consecutive float (x,y,z), the striding defines the number of bytes between each point, in memory.
///It is easier to not pass any points in the constructor, and just add one point at a time, using addPoint.
///btConvexHullShape make an internal copy of the points.
btConvexHullShape(const float* points=0,int numPoints=0, int stride=sizeof(btPoint3));
void addPoint(const btPoint3& point)
{

View File

@@ -191,6 +191,12 @@ void btDiscreteDynamicsWorld::setGravity(const btVector3& gravity)
}
}
void btDiscreteDynamicsWorld::removeRigidBody(btRigidBody* body)
{
removeCollisionObject(body);
}
void btDiscreteDynamicsWorld::addRigidBody(btRigidBody* body)
{
body->setGravity(m_gravity);

View File

@@ -126,6 +126,8 @@ public:
virtual void addRigidBody(btRigidBody* body);
virtual void removeRigidBody(btRigidBody* body);
};
#endif //BT_DISCRETE_DYNAMICS_WORLD_H

View File

@@ -56,6 +56,8 @@ class btDynamicsWorld : public btCollisionWorld
virtual void addRigidBody(btRigidBody* body) = 0;
virtual void removeRigidBody(btRigidBody* body) = 0;
};
#endif //BT_DYNAMICS_WORLD_H

View File

@@ -89,6 +89,11 @@ public:
void setGravity(const btVector3& acceleration);
const btVector3& getGravity() const
{
return m_gravity;
}
void setDamping(btScalar lin_damping, btScalar ang_damping);
inline const btCollisionShape* getCollisionShape() const {

View File

@@ -89,6 +89,11 @@ void btSimpleDynamicsWorld::setGravity(const btVector3& gravity)
}
}
void btSimpleDynamicsWorld::removeRigidBody(btRigidBody* body)
{
removeCollisionObject(body);
}
void btSimpleDynamicsWorld::addRigidBody(btRigidBody* body)
{
body->setGravity(m_gravity);

View File

@@ -70,6 +70,8 @@ public:
virtual void addRigidBody(btRigidBody* body);
virtual void removeRigidBody(btRigidBody* body);
virtual void updateAabbs();
};