Bullet collision detection

filmore

23-05-2011 17:05:22

I'm trying to use Bullet physics and collision detection in my project, but I'm having some issues. Since all the documentation and bullet forums have C++ examples, I try and translate it to Python. I am unable to translate this snippet, though which will allow me to detect collisions
//Assume world->stepSimulation or world->performDiscreteCollisionDetection has been called

int numManifolds = world->getDispatcher()->getNumManifolds();
for (int i=0;i<numManifolds;i++)
{
btPersistentManifold* contactManifold = world->getDispatcher()->getManifoldByIndexInternal(i);
btCollisionObject* obA = static_cast<btCollisionObject*>(contactManifold->getBody0());
btCollisionObject* obB = static_cast<btCollisionObject*>(contactManifold->getBody1());

int numContacts = contactManifold->getNumContacts();
for (int j=0;j<numContacts;j++)
{
btManifoldPoint& pt = contactManifold->getContactPoint(j);
if (pt.getDistance()<0.f)
{
const btVector3& ptA = pt.getPositionWorldOnA();
const btVector3& ptB = pt.getPositionWorldOnB();
const btVector3& normalOnB = pt.m_normalWorldOnB;
}
}
}

Specifically the lines with the 'static_cast'.

I modified a bit and came up with this code which works if I comment the obA and obB lines. However, I need those to specify the two objects that I'm interested in. I get this error when they are uncommented - "TypeError: No to_python (by-value) converter found for C++ type: void const *"

for i in range(numManifolds):
contactManifold = dispatcher.getManifoldByIndexInternal(i)
obA = contactManifold.getBody0()
obB = contactManifold.getBody1()

numContacts = contactManifold.getNumContacts()
for j in range(numContacts):
pt = contactManifold.getContactPoint(j)
if (pt.getDistance()<0):
print "collision"


Another issue I have is that the setUserPointer method doesn't seem to work. I get this error for it - "AttributeError: 'btRigidBody' object has no attribute 'setUserPointer'"

dermont

23-05-2011 18:19:38



I modified a bit and came up with this code which works if I comment the obA and obB lines. However, I need those to specify the two objects that I'm interested in. I get this error when they are uncommented - "TypeError: No to_python (by-value) converter found for C++ type: void const *"


I think it's wrapped as getBodyAsObject0/1, for example:

obA = contactManifold.getBodyAsObject0()
obB = contactManifold.getBodyAsObject1()


Another issue I have is that the setUserPointer method doesn't seem to work. I get this error for it - "AttributeError: 'btRigidBody' object has no attribute 'setUserPointer'"


Again I think getUserPointer/setUserPointer are wrapped in python-ogre as setUserData/getUserData. I'll have to check on that.

There are a couple of examples here which may or may not help.
viewtopic.php?f=3&t=13986

filmore

23-05-2011 19:12:15

Thank you dermont. I have your example from before and helped me a lot in understanding bullet. :D It works now.
btw, Are there any other changes like this, if so, where can I find them?

dermont

23-05-2011 19:52:23

Mostly changes such as these are handled in the hand_made_wrappers, you can check on the following link. It may be a bit difficult to decipher but it should give you a general idea.

http://python-ogre.svn.sourceforge.net/ ... iew=markup

For example from the above you could swap your code:

self.CollisionWorld.performDiscreteCollisionDetection()
numManifolds = self.CollisionWorld.getDispatcher().getNumManifolds()
for i in range(numManifolds):
contactManifold = dispatcher.getManifoldByIndexInternal(i)
obA = contactManifold.getBody0()
obB = contactManifold.getBody1()

numContacts = contactManifold.getNumContacts()
for j in range(numContacts):
pt = contactManifold.getContactPoint(j)



myCollisionList = self.CollisionWorld.getCollidingObjects()
for j in range(len(myCollisionList)):
print "ColObject0 ", type(myCollisionList[j][0])
print "ColObject1 ", type(myCollisionList[j][1])
print "Num Contatcs ", myCollisionList[j][2]

filmore

24-05-2011 18:53:12

That's much simpler! Thanks again.

filmore

05-06-2011 14:12:48

I tried using the getCollidingObjectsAndPoints() method from the page you gave me instead of getCollidingObjects() to get the points, but I get this error:


TypeError: No to_python (by-value) converter found for C++ type: class boost::python::list


I managed to get the points using the method described in the wiki, but I thought I'd report this.

Note:getCollidingObjects() works fine.

andy

06-06-2011 01:49:59

Could you create a ticket on source-forge with this bug, and ideally attach a minimal piece of code used to test it..

Likely that the wrapper needs to be tweaked and will look at this in the next go round..

Andy