Bullet Module Demo

dermont

05-02-2011 13:39:10

I've just done a rebuild of python-ogre and see that the bullet module still doesn't have a working example. I thought I had already uploaded a demo to the tracker but apparently not. Since no one appears to check the tracker anyway I'll post here, hopefully it will help someone in testing the bullet module.

thios

05-02-2011 13:59:24

Thanks!

zaph34r

07-02-2011 10:34:04

holy cow, thats awesome

Ive been trying for days to get ogrebullet to work without any success.
Thanks a lot for this demo, there really is a shortage of demos on bullet with pythonogre.

Just a question, as all demos are using Dynamics, is pure collisions so simple noone bothers to write about it, or is it just that no one uses that?

dermont

07-02-2011 15:40:10

If you are on Windows and using the installed version of python-ogre I would advise that you download the bullet source code from:
http://code.google.com/p/bullet/downloa ... p&can=2&q=

If you are on Linux you probably already have the source.

In the top directory there is an excellent user manual Bullet_User_Manual.pdf (Chapter 3 - Quickstart) and a couple of demos in Demos/CollisionDemo and Demos/CollisionInterfaceDemo which will probably help you get started.

zaph34r

07-02-2011 17:51:26

the manual is really a good read :)

one problem i have is, when i try to use collisions only, btCollisionWorld.performDiscreteCollisionDetection() crashes python with a "R6025 - pure virtual function call", even if all i do is just create the collisions world, and thats it. Is there any function that comes to mind, which could be the culprit? Because from the samples and documentation the barebones collision setup should be pretty simple, and nothing seems necessary apart from the world itself, and the stuff required to initialize it.

EDIT: nevermind, i think i have it, i forgot to keep the collision configuration, dispatcher, etc. as instance variables, and bullet seemed to dislike that
EDIT2: or not, for a dynamics world adding the instance variables fixed the crash, for a collision world it doesnt :/ also, my rigid bodies are flying around randomly :lol: i guess this will still take a while

dermont

08-02-2011 09:52:42

If you upload your example I'll take a look, one problem I came across is the settings for the origin/rotations of getWorldTransform not being applied,e.g:


## self.collisionObjects is a list of collision objects
self.collisionObjects[0].getWorldTransform().setOrigin(bullet.btVector3(0.0,3.0,0.0))

print self.collisionObjects[0].getWorldTransform().getOrigin().x()
print self.collisionObjects[0].getWorldTransform().getOrigin().y()
print self.collisionObjects[0].getWorldTransform().getOrigin().z()
>> 0.0
>> 0.0
>> 0.0


As a workaround you can create a new_transform passing the collision object's btTransform to the constructor,apply the settings to the transform and then set collision object's setWorldTransform(new_transform). For example in the CollisionInterfaceDemo::clientResetScene():


boxAtransform = bullet.btTransform( self.collisionObjects[0].getWorldTransform() )
boxAtransform.setOrigin(bullet.btVector3(0.0,3.0,0.0))
rotA = bullet.btQuaternion(0.739,-0.204,0.587,0.257)
rotA.normalize()
boxAtransform.setRotation(rotA)

boxBtransform = bullet.btTransform( self.collisionObjects[1].getWorldTransform() )
boxBtransform.setOrigin(bullet.btVector3(0.0,4.248,0.0))

self.collisionObjects[0].setWorldTransform(boxAtransform)
self.collisionObjects[1].setWorldTransform(boxBtransform)


I'm assuming the above problem relates to python-ogre returning the const getWorldTransform method (which can't be changed), though I haven't looked at it and it may be something wrong in my code. I think excluding the const method in the code generation would resolve the above.

http://www.bulletphysics.com/Bullet/Bul ... sform.html
btTransform & getWorldTransform ()
const btTransform & getWorldTransform () const


I've attached OgreBullet_CollisionInterfaceDemo (just started and very much WIP -collision not properly implemented ), maybe it will help you resolve your initial CollisionWorld problems.

Edit:
Uploaded new version to include ContactResultCallback, unless I've missed something
it should be pretty much as the bullet demo.

zaph34r

08-02-2011 13:01:51

man, you rock :)

i managed to get it to work (read: not crash) with the first version of your CollisionInterfaceDemo, even though im still kind of baffled why it didn't work before, as i basically did everything exactly like you did :?
Only differences were
a) the btTransform const problem you mentioned
b) having used a sphere shape instead of a box (shouldn't really make a difference)
c) me not having set the initial basis for the world transform of the btCollisionObject, but as a) seems to kind of prevent that from working anyways, it must've been the transforms not being properly set than. I'll run some tests on that later to verify it.

These simple wireframe boxes must be the best thing i've seen all day :lol:

now having a look at the contact callback, thanks again for your samples

EDIT: contact callback works like a charm, finally some interactivity in my project. you just made my day :D

dermont

08-02-2011 14:26:43

Also from your above screenshot (nice by the way) you may need more efficient collision callbacks/filtering, your best bet would be to check out the bullet wiki, there are a number of articles to help you get started.

http://www.bulletphysics.org/mediawiki- ... l_Articles
http://www.bulletphysics.org/mediawiki- ... _Filtering

iwanantonowitsch

08-02-2011 17:21:50

[ive never seen that much action on this forum... so keep it going lads!]

zaph34r

08-02-2011 22:32:48

Thanks for the links, i already read up a bit on filtering and stuff, as my world is both seamless and vast. I had discarded any plans of having everything potentially interact at the same time pretty soon anyways. At least it seems bullet has no problem with a +-250k units world, which is the size of one minimal cell in my dynamic coordinate system, so i guess it will be fine. I'll better run some more precision tests though, with the different broadphases. And as long as collisions around the player are consistent, no one will notice any cut corners.

Do you have some experience with bigger worlds/objects and bullet? The different accounts i've read were not really consistent regarding object sizes, and floating point precision with huge distance/size differences, the more restrictive opinions being that having your objects outside of a size range of about 2 orders of magnitude (0.1 units to 5 units or so) already is close to some practical limit. In which case i would have to be quite creative since i have objects on the range of few meters to some kilometers, and distances to match :?

[ive never seen that much action on this forum... so keep it going lads!]
me neither :lol: but i find it quite a welcome change ;)

dermont

09-02-2011 15:26:02

You will probably get a much better response to your questions on the bullet forum (or even the main forum; a number of people use bullet in projects such as yours).

http://bulletphysics.org/Bullet/phpBB3/ ... 54&start=0
http://bulletphysics.org/Bullet/phpBB3/ ... 98&start=0
http://www.bulletphysics.org/Bullet/php ... f=9&t=1470

http://bulletphysics.org/mediawiki-1.5. ... Broadphase
http://bulletphysics.org/mediawiki-1.5. ... _aabb_tree
http://www.bulletphysics.com/Bullet/Bul ... ource.html
http://www.bulletphysics.org/mediawiki- ... ialization

AFAIK the btDefaultSerializer is not included with the bullet module. If you are building from source there is a patch for generate_code.py; otherwise you will probably have to wait for Andy's next round of updates.

This is the code to test the patch, there are some comments at the end regarding your previous "pure virtual method called" error.


import sys
sys.path.insert(0,'..')
sys.path.insert(0,'.')
import PythonOgreConfig

import ogre.renderer.OGRE as ogre
import ogre.physics.bullet as bullet
import ogre.io.OIS as OIS
import ctypes

collisionConfiguration = bullet.get_btDefaultCollisionConfiguration()
dispatcher = bullet.get_btCollisionDispatcher1(collisionConfiguration)
broadphase = bullet.btAxisSweep3(bullet.btVector3(-10000,-10000,-10000), bullet.btVector3(10000,10000,10000), 1024)
solver = bullet.btSequentialImpulseConstraintSolver()
dynamicsWorld = bullet.btDiscreteDynamicsWorld(dispatcher, broadphase , solver, collisionConfiguration)

collisionObjects = []

basisA = bullet.btMatrix3x3()
basisA.setIdentity()
basisB = bullet.btMatrix3x3()
basisB.setIdentity();

collisionObjects.append(bullet.btCollisionObject())
collisionObjects.append(bullet.btCollisionObject())
collisionObjects.append(bullet.btCollisionObject())

collisionObjects[0].getWorldTransform().setBasis(basisA)
collisionObjects[1].getWorldTransform().setBasis(basisB)

boxA = bullet.btBoxShape(bullet.btVector3(1.0,1.0,1.0))
boxA.setMargin(0.0)
collisionObjects[0].setCollisionShape(boxA)
collisionObjects[0].setUserData("BoxA")

boxB = bullet.btBoxShape(bullet.btVector3(0.5,0.5,0.5))
boxB.setMargin(0.0)
collisionObjects[1].setCollisionShape(boxB)
collisionObjects[1].setUserData("BoxB")

groundShape = bullet.btBoxShape(bullet.btVector3(50.0,50.0,50.0))
collisionObjects[2].setCollisionShape(groundShape)
collisionObjects[2].setUserData("Ground")

maxSerializeBufferSize = int(1024*1024*5)
serializer = bullet.btDefaultSerializer(maxSerializeBufferSize)
serializer.startSerialization()
for i in range(len(collisionObjects)):
c = collisionObjects
dynamicsWorld.addCollisionObject(c)
## must be 20 chars ???
name = '%20s' % ("Name" + str(i))
serializer.registerNameForPointer(ogre.getAddress(c.getCollisionShape()),name)
#serializer.serializeName(name)
c.getCollisionShape().serializeSingleShape(serializer)

serializer.finishSerialization()

## write out bullet file
buffer_size = serializer.getCurrentBufferSize()*ctypes.sizeof(ctypes.c_ubyte)
strbuffer = ctypes.create_string_buffer(buffer_size)
ctypes.memset ( strbuffer, 0, buffer_size )

ctypes.memmove( strbuffer, serializer.getBufferPointer(),
buffer_size)

import io
f=open("testFile.bullet", 'wb')
f.write(strbuffer)
f.close()


## load bullet file
fileLoader = bullet.btBulletWorldImporter(dynamicsWorld)
fileLoader.setVerboseMode(True)
fileLoader.loadFile("testFile.bullet")

## ============================================================================
## bool loadFileFromMemory(char *memoryBuffer, int len);
## loadFileFromMemory expects python string, pretty sure it should be a pointer
## ============================================================================


#fileLoader.loadFileFromMemory(strbuffer.value,ctypes.sizeof(strbuffer))


## list our collision
a = dynamicsWorld.getCollisionObjectArray()
for j in range(a.size()):
print "Object %d " %(j) , dir(a[j])

####################################################################################
##
## For users encountering the following error message:
## pure virtual method called
## terminate called without an active exception
## Aborted
####################################################################################
##
## 1) On program exit: since the collision objects we keep a reference to
## (either in a in a list or self) may have already been deleted by python
## the above error may occur when bullet attempts to remove the collison objects
## 2) You have created a bullet object and allowed it to go out of
## scope (e.g. you didn't keep a reference to the bullet object
## either by self.object or adding to a list).
##-------------------------------------------------------

while(len(collisionObjects)>0):
c = collisionObjects.pop()
dynamicsWorld.removeCollisionObject(c)
del c


Also if you are building from source there are the ogrebullet modules but they don't seem to be available, with the latest python-ogre version, for download at sourceforge.

Sorry I can't be more help.

thios

09-02-2011 20:22:18

Also if you are building from source there are the ogrebullet modules but they don't seem to be available, with the latest python-ogre version, for download at sourceforge.

They're actually packaged with bullet... :)

dermont

10-02-2011 01:34:37

Also if you are building from source there are the ogrebullet modules but they don't seem to be available, with the latest python-ogre version, for download at sourceforge.

They're actually packaged with bullet... :)

Thanks, it's been some time since I used the Windows packaged version of python-ogre.