This commit is contained in:
erwin coumans
2016-05-06 13:58:21 -07:00
9 changed files with 152 additions and 53 deletions

View File

@@ -192,6 +192,10 @@ ENDIF()
OPTION(BUILD_BULLET3 "Set when you want to build Bullet 3" ON) OPTION(BUILD_BULLET3 "Set when you want to build Bullet 3" ON)
OPTION(BUILD_PYBULLET "Set when you want to build pybullet (experimental Python bindings for Bullet)" OFF) OPTION(BUILD_PYBULLET "Set when you want to build pybullet (experimental Python bindings for Bullet)" OFF)
IF(BUILD_PYBULLET)
FIND_PACKAGE(PythonLibs 2.7 REQUIRED)
SET(BUILD_SHARED_LIBS ON CACHE BOOL "Shared Libs" FORCE)
ENDIF(BUILD_PYBULLET)
IF(BUILD_BULLET3) IF(BUILD_BULLET3)
IF(APPLE) IF(APPLE)

View File

@@ -83,8 +83,10 @@ static ExampleEntry gDefaultExamples[]=
ExampleEntry(0,"API"), ExampleEntry(0,"API"),
ExampleEntry(1,"Basic Example","Create some rigid bodies using box collision shapes. This is a good example to familiarize with the basic initialization of Bullet. The Basic Example can also be compiled without graphical user interface, as a console application. Press W for wireframe, A to show AABBs, I to suspend/restart physics simulation. Press D to toggle auto-deactivation of the simulation. ", BasicExampleCreateFunc), ExampleEntry(1,"Basic Example","Create some rigid bodies using box collision shapes. This is a good example to familiarize with the basic initialization of Bullet. The Basic Example can also be compiled without graphical user interface, as a console application. Press W for wireframe, A to show AABBs, I to suspend/restart physics simulation. Press D to toggle auto-deactivation of the simulation. ", BasicExampleCreateFunc),
ExampleEntry(1,"Rolling Friction", "Damping is often not good enough to keep rounded objects from rolling down a sloped surface. Instead, you can set the rolling friction of a rigid body. Generally it is best to leave the rolling friction to zero, to avoid artifacts.", RollingFrictionCreateFunc), ExampleEntry(1,"Rolling Friction", "Damping is often not good enough to keep rounded objects from rolling down a sloped surface. Instead, you can set the rolling friction of a rigid body. Generally it is best to leave the rolling friction to zero, to avoid artifacts.", RollingFrictionCreateFunc),
ExampleEntry(1,"Constraints","Show the use of the various constraints in Bullet. Press the L key to visualize the constraint limits. Press the C key to visualize the constraint frames.", ExampleEntry(1,"Constraints","Show the use of the various constraints in Bullet. Press the L key to visualize the constraint limits. Press the C key to visualize the constraint frames.",

View File

@@ -44,20 +44,24 @@ int main(int argc, char** argv)
///-----initialization_end----- ///-----initialization_end-----
///create a few basic rigid bodies
btCollisionShape* groundShape = new btBoxShape(btVector3(btScalar(50.),btScalar(50.),btScalar(50.)));
//keep track of the shapes, we release memory at exit. //keep track of the shapes, we release memory at exit.
//make sure to re-use collision shapes among rigid bodies whenever possible! //make sure to re-use collision shapes among rigid bodies whenever possible!
btAlignedObjectArray<btCollisionShape*> collisionShapes; btAlignedObjectArray<btCollisionShape*> collisionShapes;
///create a few basic rigid bodies
//the ground is a cube of side 100 at position y = -56.
//the sphere will hit it at y = -6, with center at -5
{
btCollisionShape* groundShape = new btBoxShape(btVector3(btScalar(50.),btScalar(50.),btScalar(50.)));
collisionShapes.push_back(groundShape); collisionShapes.push_back(groundShape);
btTransform groundTransform; btTransform groundTransform;
groundTransform.setIdentity(); groundTransform.setIdentity();
groundTransform.setOrigin(btVector3(0,-56,0)); groundTransform.setOrigin(btVector3(0,-56,0));
{
btScalar mass(0.); btScalar mass(0.);
//rigidbody is dynamic if and only if mass is non zero, otherwise static //rigidbody is dynamic if and only if mass is non zero, otherwise static
@@ -113,7 +117,7 @@ int main(int argc, char** argv)
///-----stepsimulation_start----- ///-----stepsimulation_start-----
for (i=0;i<100;i++) for (i=0;i<150;i++)
{ {
dynamicsWorld->stepSimulation(1.f/60.f,10); dynamicsWorld->stepSimulation(1.f/60.f,10);
@@ -178,9 +182,5 @@ int main(int argc, char** argv)
//next line is optional: it will be cleared by the destructor when the array goes out of scope //next line is optional: it will be cleared by the destructor when the array goes out of scope
collisionShapes.clear(); collisionShapes.clear();
///-----cleanup_end-----
printf("Press a key to exit\n");
getchar();
} }

View File

@@ -1076,6 +1076,20 @@ b3KeyboardCallback X11OpenGLWindow::getKeyboardCallback()
return m_data->m_keyboardCallback; return m_data->m_keyboardCallback;
} }
int X11OpenGLWindow::getWidth() const
{
if (m_data)
return m_data->m_glWidth;
return 0;
}
int X11OpenGLWindow::getHeight() const
{
if (m_data)
return m_data->m_glHeight;
return 0;
}
#include <stdio.h> #include <stdio.h>
int X11OpenGLWindow::fileOpenDialog(char* filename, int maxNameLength) int X11OpenGLWindow::fileOpenDialog(char* filename, int maxNameLength)

View File

@@ -65,9 +65,14 @@ public:
virtual void setWindowTitle(const char* title); virtual void setWindowTitle(const char* title);
virtual int getWidth() const;
virtual int getHeight() const;
int fileOpenDialog(char* filename, int maxNameLength); int fileOpenDialog(char* filename, int maxNameLength);
}; };
#endif #endif

View File

@@ -4,7 +4,7 @@ INCLUDE_DIRECTORIES(
${BULLET_PHYSICS_SOURCE_DIR}/src ${BULLET_PHYSICS_SOURCE_DIR}/src
${BULLET_PHYSICS_SOURCE_DIR}/examples ${BULLET_PHYSICS_SOURCE_DIR}/examples
${BULLET_PHYSICS_SOURCE_DIR}/examples/ThirdPartyLibs ${BULLET_PHYSICS_SOURCE_DIR}/examples/ThirdPartyLibs
/usr/include/python2.7 ${PYTHON_INCLUDE_DIRS}
) )
SET(pybullet_SRCS SET(pybullet_SRCS
@@ -18,7 +18,7 @@ ADD_LIBRARY(pybullet ${pybullet_SRCS})
SET_TARGET_PROPERTIES(pybullet PROPERTIES VERSION ${BULLET_VERSION}) SET_TARGET_PROPERTIES(pybullet PROPERTIES VERSION ${BULLET_VERSION})
SET_TARGET_PROPERTIES(pybullet PROPERTIES SOVERSION ${BULLET_VERSION}) SET_TARGET_PROPERTIES(pybullet PROPERTIES SOVERSION ${BULLET_VERSION})
TARGET_LINK_LIBRARIES(pybullet BulletExampleBrowserLib BulletSoftBody BulletDynamics BulletCollision BulletInverseDynamicsUtils BulletInverseDynamics LinearMath OpenGLWindow gwen Bullet3Common Python) TARGET_LINK_LIBRARIES(pybullet BulletExampleBrowserLib BulletSoftBody BulletDynamics BulletCollision BulletInverseDynamicsUtils BulletInverseDynamics LinearMath OpenGLWindow gwen Bullet3Common ${PYTHON_LIBRARIES})
ENDIF (BUILD_SHARED_LIBS) ENDIF (BUILD_SHARED_LIBS)

View File

@@ -790,23 +790,50 @@ void btCollisionWorld::objectQuerySingleInternal(const btConvexShape* castShape,
} }
} }
} else { } else {
///@todo : use AABB tree or other BVH acceleration structure!
if (collisionShape->isCompound()) if (collisionShape->isCompound())
{ {
BT_PROFILE("convexSweepCompound"); struct btCompoundLeafCallback : btDbvt::ICollide
const btCompoundShape* compoundShape = static_cast<const btCompoundShape*>(collisionShape);
int i=0;
for (i=0;i<compoundShape->getNumChildShapes();i++)
{ {
btTransform childTrans = compoundShape->getChildTransform(i); btCompoundLeafCallback(
const btCollisionShape* childCollisionShape = compoundShape->getChildShape(i); const btCollisionObjectWrapper* colObjWrap,
btTransform childWorldTrans = colObjWorldTransform * childTrans; const btConvexShape* castShape,
const btTransform& convexFromTrans,
const btTransform& convexToTrans,
btScalar allowedPenetration,
const btCompoundShape* compoundShape,
const btTransform& colObjWorldTransform,
ConvexResultCallback& resultCallback)
:
m_colObjWrap(colObjWrap),
m_castShape(castShape),
m_convexFromTrans(convexFromTrans),
m_convexToTrans(convexToTrans),
m_allowedPenetration(allowedPenetration),
m_compoundShape(compoundShape),
m_colObjWorldTransform(colObjWorldTransform),
m_resultCallback(resultCallback) {
}
const btCollisionObjectWrapper* m_colObjWrap;
const btConvexShape* m_castShape;
const btTransform& m_convexFromTrans;
const btTransform& m_convexToTrans;
btScalar m_allowedPenetration;
const btCompoundShape* m_compoundShape;
const btTransform& m_colObjWorldTransform;
ConvexResultCallback& m_resultCallback;
public:
void ProcessChild(int index, const btTransform& childTrans, const btCollisionShape* childCollisionShape)
{
btTransform childWorldTrans = m_colObjWorldTransform * childTrans;
struct LocalInfoAdder : public ConvexResultCallback { struct LocalInfoAdder : public ConvexResultCallback {
ConvexResultCallback* m_userCallback; ConvexResultCallback* m_userCallback;
int m_i; int m_i;
LocalInfoAdder (int i, ConvexResultCallback *user) LocalInfoAdder(int i, ConvexResultCallback *user)
: m_userCallback(user), m_i(i) : m_userCallback(user), m_i(i)
{ {
m_closestHitFraction = m_userCallback->m_closestHitFraction; m_closestHitFraction = m_userCallback->m_closestHitFraction;
@@ -815,7 +842,7 @@ void btCollisionWorld::objectQuerySingleInternal(const btConvexShape* castShape,
{ {
return m_userCallback->needsCollision(p); return m_userCallback->needsCollision(p);
} }
virtual btScalar addSingleResult (btCollisionWorld::LocalConvexResult& r, bool b) virtual btScalar addSingleResult(btCollisionWorld::LocalConvexResult& r, bool b)
{ {
btCollisionWorld::LocalShapeInfo shapeInfo; btCollisionWorld::LocalShapeInfo shapeInfo;
shapeInfo.m_shapePart = -1; shapeInfo.m_shapePart = -1;
@@ -829,13 +856,52 @@ void btCollisionWorld::objectQuerySingleInternal(const btConvexShape* castShape,
} }
}; };
LocalInfoAdder my_cb(i, &resultCallback); LocalInfoAdder my_cb(index, &m_resultCallback);
btCollisionObjectWrapper tmpObj(colObjWrap,childCollisionShape,colObjWrap->getCollisionObject(),childWorldTrans,-1,i); btCollisionObjectWrapper tmpObj(m_colObjWrap, childCollisionShape, m_colObjWrap->getCollisionObject(), childWorldTrans, -1, index);
objectQuerySingleInternal(castShape, convexFromTrans,convexToTrans, objectQuerySingleInternal(m_castShape, m_convexFromTrans, m_convexToTrans, &tmpObj, my_cb, m_allowedPenetration);
&tmpObj,my_cb, allowedPenetration); }
void Process(const btDbvtNode* leaf)
{
// Processing leaf node
int index = leaf->dataAsInt;
btTransform childTrans = m_compoundShape->getChildTransform(index);
const btCollisionShape* childCollisionShape = m_compoundShape->getChildShape(index);
ProcessChild(index, childTrans, childCollisionShape);
}
};
BT_PROFILE("convexSweepCompound");
const btCompoundShape* compoundShape = static_cast<const btCompoundShape*>(collisionShape);
btVector3 fromLocalAabbMin, fromLocalAabbMax;
btVector3 toLocalAabbMin, toLocalAabbMax;
castShape->getAabb(colObjWorldTransform.inverse() * convexFromTrans, fromLocalAabbMin, fromLocalAabbMax);
castShape->getAabb(colObjWorldTransform.inverse() * convexToTrans, toLocalAabbMin, toLocalAabbMax);
fromLocalAabbMin.setMin(toLocalAabbMin);
fromLocalAabbMax.setMax(toLocalAabbMax);
btCompoundLeafCallback callback(colObjWrap, castShape, convexFromTrans, convexToTrans,
allowedPenetration, compoundShape, colObjWorldTransform, resultCallback);
const btDbvt* tree = compoundShape->getDynamicAabbTree();
if (tree) {
const ATTRIBUTE_ALIGNED16(btDbvtVolume) bounds = btDbvtVolume::FromMM(fromLocalAabbMin, fromLocalAabbMax);
tree->collideTV(tree->m_root, bounds, callback);
} else {
int i;
for (i=0;i<compoundShape->getNumChildShapes();i++)
{
const btCollisionShape* childCollisionShape = compoundShape->getChildShape(i);
btTransform childTrans = compoundShape->getChildTransform(i);
callback.ProcessChild(i, childTrans, childCollisionShape);
}
} }
} }
} }

View File

@@ -22,6 +22,8 @@ subject to the following restrictions:
#include "LinearMath/btQuaternion.h" #include "LinearMath/btQuaternion.h"
#include "LinearMath/btSerializer.h" #include "LinearMath/btSerializer.h"
#include "btConvexPolyhedron.h"
#include "LinearMath/btConvexHullComputer.h"
btConvexHullShape ::btConvexHullShape (const btScalar* points,int numPoints,int stride) : btPolyhedralConvexAabbCachingShape () btConvexHullShape ::btConvexHullShape (const btScalar* points,int numPoints,int stride) : btPolyhedralConvexAabbCachingShape ()
{ {
@@ -121,10 +123,17 @@ btVector3 btConvexHullShape::localGetSupportingVertex(const btVector3& vec)const
} }
void btConvexHullShape::optimizeConvexHull()
{
btConvexHullComputer conv;
conv.compute(&m_unscaledPoints[0].getX(), sizeof(btVector3),m_unscaledPoints.size(),0.f,0.f);
int numVerts = conv.vertices.size();
m_unscaledPoints.resize(0);
for (int i=0;i<numVerts;i++)
{
m_unscaledPoints.push_back(conv.vertices[i]);
}
}

View File

@@ -55,8 +55,7 @@ public:
return getUnscaledPoints(); return getUnscaledPoints();
} }
void optimizeConvexHull();
SIMD_FORCE_INLINE btVector3 getScaledPoint(int i) const SIMD_FORCE_INLINE btVector3 getScaledPoint(int i) const
{ {