Bullet SWIG (Simplified Wrapper Interface Generator) 4 Lua

Post Reply
ghoulsblade
Posts: 2
Joined: Thu Jan 01, 2009 2:22 am

Bullet SWIG (Simplified Wrapper Interface Generator) 4 Lua

Post by ghoulsblade » Sat Feb 14, 2009 9:37 pm

Hi !

i just wanted to let you guys know that we're working on a fresh swig file for binding bullet to lua and possibly other scripting languages (C#,python,ruby,...)
it's early work in progress and there are still many things missing, but a few basic problems have been solved and the hello world example already works in lua,
so i though i'd post it here as it might help other people trying to make swig bindings.

required files for binding generation
http://zwischenwelt.org/svn/cliffrider/ ... e/bullet.i
http://zwischenwelt.org/svn/cliffrider/ ... tionInfo.i
http://zwischenwelt.org/svn/cliffrider/ ... internal.i
http://zwischenwelt.org/svn/cliffrider/ ... isSweep3.i
http://zwischenwelt.org/svn/cliffrider/ ... ig.debug.h

command to generate binding / wrapper :
swig -c++ -lua -Ibullet/ -Iinterfaces/ -Iinclude/ -o src/swig.bullet_L.cpp interface/bullet.i

to activate the binding :

Code: Select all

...
extern "C" {
extern int luaopen_bullet (lua_State* L);
}
...
luaopen_bullet(L);
...
bullet/Demos/HelloWorld/HelloWorld.cpp rebuilt in lua :

Code: Select all

	--~ ///collision configuration contains default setup for memory, collision setup. Advanced users can create their own configuration.
	local collisionConfiguration = bullet.btDefaultCollisionConfiguration()

	--~ ///use the default collision dispatcher. For parallel processing you can use a diffent dispatcher (see Extras/BulletMultiThreaded)
	local dispatcher = bullet.btCollisionDispatcher(collisionConfiguration)

	--~ ///the maximum size of the collision world. Make sure objects stay within these boundaries
	--~ ///Don't make the world AABB size too large, it will harm simulation quality and performance
	local worldAabbMin = bullet.btVector3(-10000,-10000,-10000)
	local worldAabbMax = bullet.btVector3(10000,10000,10000)
	local	maxProxies = 1024
	local overlappingPairCache = bullet.btAxisSweep3(worldAabbMin,worldAabbMax,maxProxies);
	
	
	--~ ///the default constraint solver. For parallel processing you can use a different solver (see Extras/BulletMultiThreaded)
	local solver = bullet.btSequentialImpulseConstraintSolver()

	local dynamicsWorld = bullet.btDiscreteDynamicsWorld(dispatcher,overlappingPairCache,solver,collisionConfiguration)

	--~ local test = dynamicsWorld:getWorldUserInfo()
	
	dynamicsWorld:setGravity(bullet.btVector3(0,-10,0))

	--~ ///create a few basic rigid bodies
	local groundShape = bullet.btBoxShape(bullet.btVector3((50.),(50.),(50.))) -- btCollisionShape*

	--~ //keep track of the shapes, we release memory at exit.
	--~ //make sure to re-use collision shapes among rigid bodies whenever possible!
	--~ btAlignedObjectArray<btCollisionShape*> collisionShapes;
	local collisionShapes = bullet.btAlignedObjectArray_btCollisionShape()

	collisionShapes:push_back(groundShape)

	local groundTransform = bullet.btTransform()
	groundTransform:setIdentity()
	groundTransform:setOrigin(bullet.btVector3(0,-56,0))
	
	
	
	if (1 == 1) then
		local mass = 0

		--~ //rigidbody is dynamic if and only if mass is non zero, otherwise static
		local isDynamic = (mass ~= 0)

		local localInertia = bullet.btVector3(0,0,0)
		if (isDynamic) then
			groundShape:calculateLocalInertia(mass,localInertia)
		end

		--~ //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
		local myMotionState = bullet.btDefaultMotionState(groundTransform)
		--~ btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,groundShape,localInertia);
		local rbInfo = bullet.btRigidBodyConstructionInfo(mass,myMotionState,groundShape,localInertia)
		local body = bullet.btRigidBody(rbInfo)

		--~ //add the body to the dynamics world
		dynamicsWorld:addRigidBody(body)
	end


	if (1 == 1) then
		--~ //create a dynamic rigidbody

		-- local colShape = bullet.btBoxShape(bullet.btVector3(1,1,1))-- btCollisionShape*
		local colShape = bullet.btSphereShape(1) -- btCollisionShape*
		collisionShapes:push_back(colShape)

		--~ /// Create Dynamic Objects
		local startTransform = bullet.btTransform()
		startTransform:setIdentity()

		local mass = 1

		--~ //rigidbody is dynamic if and only if mass is non zero, otherwise static
		local isDynamic = (mass ~= 0)

		local localInertia = bullet.btVector3(0,0,0)
		if (isDynamic) then
			colShape:calculateLocalInertia(mass,localInertia);
		end

		startTransform:setOrigin(bullet.btVector3(2,10,0))
	
		--~ //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
		local myMotionState = bullet.btDefaultMotionState(startTransform)
		local rbInfo = bullet.btRigidBodyConstructionInfo(mass,myMotionState,colShape,localInertia)
		local body = bullet.btRigidBody(rbInfo)

		dynamicsWorld:addRigidBody(body)
	end
	
	--~ /// Do some simulation
	
	--~ for (i=0;i<100;i++)
	for i = 0,100-1 do
		dynamicsWorld:stepSimulation(1/60,10);
		
		--~ //print positions of all objects
		local collisionWorld = bullet.CastTo_btCollisionWorld(dynamicsWorld) -- work around for swig-lua inheritance wrapping  bug
		for j=collisionWorld:getNumCollisionObjects()-1,0,-1 do
			local obj = collisionWorld:getCollisionObjectArray()[j] -- btCollisionObject*
			local body = bullet.btRigidBody_upcast(obj) -- btRigidBody*
			if (body and body:getMotionState()) then
				local trans = bullet.btTransform()
				body:getMotionState():getWorldTransform(trans)
				printf("world pos = %f,%f,%f\n",(trans:getOrigin():getX()),(trans:getOrigin():getY()),(trans:getOrigin():getZ()));
			end
		end
	end
a small request : when you use classes inside classes (internal/inner) that are part of the api, please try to make their names unique if possible, otherwise it could cause problems during binding.

see also SWIG HP and Bullet SWIG 4 Java(2007)




Update : 25.02 : i ran into problems due to garbage collection, things like ( myMotionState,rbInfo , body ) have to be kept, if they are destroyed while still in use due to lua grabage collection, then there will be unexpected segfaults at some later point, hard to trace.

Tully
Posts: 2
Joined: Fri Apr 10, 2009 6:23 pm

Re: Bullet SWIG (Simplified Wrapper Interface Generator) 4 Lua

Post by Tully » Fri Apr 10, 2009 7:51 pm

I too have been working on wrapping Bullet in SWIG to get python bindings. Are other people interested in this? I'm working out of the personalrobots.sf.net repo but would be happy to push back patches if there is interest from people here.

What I have so far is:
I've only worked on the LinearMath library. And to do it I ended up making many small changes to the bullet code to clean up the interface and make it clean in Python. The changes were mostly things like pulling functions into classes and making them methods, or providing %extern directives for the left multiply operators. The end result in Python looks something like this:

Code: Select all

tfoote@axx:/home/tfoote/pkg/prcore/bullet$ python
Python 2.5.2 (r252:60911, Oct  5 2008, 19:24:49) 
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import roslib; roslib.load_manifest("tf")  ## setup python paths in ROS architecture
>>> import bullet
>>> 
>>> v = bullet.Vector3(1,2,3)
>>> print v
[1.000000, 2.000000, 3.000000]
>>> m = bullet.Matrix3x3()
>>> m.setIdentity()
>>> print m
[[1.000000, 0.000000, 0.000000]|
|[0.000000, 1.000000, 0.000000]|
|[0.000000, 0.000000, 1.000000]]
>>> m.setEulerZYX(1.5,0,0)
>>> print m
[[0.070737, -0.997495, 0.000000]|
|[0.997495, 0.070737, 0.000000]|
|[-0.000000, 0.000000, 1.000000]]
>>> t = bullet.Transform()
>>> t.setOrigin(v)
>>> t.setBasis(m)
>>> 
>>> 
>>> q = bullet.Quaternion(1,0,0)# Three args = from euler angles
>>> print q
[0.000000, 0.000000, 0.479426, 0.877583]
>>> q = t * q
>>> print q
[0.000000, 0.000000, 0.948985, 0.315322]
>>> 
The swig interface Used for this is here http://personalrobots.svn.sourceforge.n ... iew=markup

The modified bullet files are:
http://personalrobots.svn.sourceforge.n ... iew=markup
http://personalrobots.svn.sourceforge.n ... iew=markup
http://personalrobots.svn.sourceforge.n ... iew=markup
http://personalrobots.svn.sourceforge.n ... iew=markup

pinballwizard
Posts: 15
Joined: Tue Jun 19, 2007 5:52 am

Re: Bullet SWIG (Simplified Wrapper Interface Generator) 4 Lua

Post by pinballwizard » Mon May 04, 2009 2:18 pm

Yes, I for one would be very much interested in Python bindings. Please keep us posted on the progress!

scaron
Posts: 2
Joined: Fri May 29, 2009 1:08 am

Re: Bullet SWIG (Simplified Wrapper Interface Generator) 4 Lua

Post by scaron » Fri Nov 20, 2009 10:38 pm

+1 for python bindings for bullet... support for windows would also be helpful.

Post Reply