Game Development Community

Box2D Integration on Google Code

by Michael Woerister · in Torque Game Builder · 01/23/2009 (6:27 am) · 144 replies

Hi all,

as you may or may not know I created a wrapper for Box2D that let's you use it in TGB through script and behaviors. I wrote my bachelor thesis on this topic and now that it got approved I'd like to share the code with the community.

So here is my question to Garage Games: Is it OK if I put code and thesis up on Google Code (or Sourceforge)? It obviously contains some references to TGB's C++ code base and I don't want break the EULA. But having a public Subversion repository there would make it way easier to maintain the resource.

A few bits of information on the wrapper:
- It requires TGB Pro because Box2D is written in C++.
- It does not silently replace TGB's internal physics engine. It has to be used explicitly.
- It allows for binding t2dSceneObjects to Box2D bodies and shapes.
- I tried to make it as usable as possible in the time I had but it's not for sissies ;)
- It's a good idea to read the thesis to get an overview.

So, what do you say?

-Michael
#21
02/07/2009 (9:51 am)
Hey guys,

I posted a short FAQ on the google code page:
code.google.com/p/tgb-box2d-integration/wiki/FAQ

-Michael
#22
02/08/2009 (4:44 pm)
Michael,

Amazing work! As I implied before, I don't really have time to mess with TGB at the moment, but I was wondering if, currently, it would be conceivable to have a rag doll character with your Box2D wrapper?

Thanks,
Kevin
#23
02/09/2009 (6:33 am)
It depends on what you mean exactly. If your question is: Can I create a ragdoll in script like the one in the Box2d Flash demo, then the answer is yes: The wrapper supports polygon and circle shapes, prismatic, revolute, distance, gear, and pulley joints.

If your question is: Can I create a ragdoll character in the scene editor without writing a single line of code, then the answer is no: You can only create joints in script (or c++).
#24
02/09/2009 (1:54 pm)
WOW.

I really hope that GG has paid you the big bucks for creating an opening for their engine that was previously unexplored.

Thank you so much Michael. I often had dreams about TGB having super physics.

Now I just have to make a game, ;D
#25
02/10/2009 (5:05 am)
Quote:I really hope that GG has paid you the big bucks...
Nope, still a starving student ;)
#26
02/10/2009 (7:07 am)
If you can create joints in script I reckon you can also create them with behaviors :) Just throwing it out there.

Edit: Oops looks like you already do that.
#27
02/10/2009 (7:40 am)
Quote:
If you can create joints in script I reckon you can also create them with behaviors :) Just throwing it out there.
Yes, at least for joints that connect 2 bodies it would be possible (there is also the b2GearJoint that connects to 2 joints). The first reason I decided not to provide Behaviors for joint creation is that it's a bit tedious because you would have to connect to bodies by name. Therefore you would have to name every object that is referenced by a joint.

However, there is another reason. If one makes an actual game using the wrapper they would be smart not to use the Box2dBodyBehavior and Box2dShapeBehavior directly anyway. Instead it would be better to create game specific Behaviors that create the Box2d entities behind the scenes.

In the game the wrapper was created for originally we had a RotatingPlatformBehavior. When the level was loaded in the game the Behavior created a Box2dBodyBehavior and a Box2dShapeBehavior and with these created body and shape for the rotating platform. At last the behavior created a RevoluteJoint that pinned the platform to the ground body.
The Box2dBodyBehavior and Box2dShapeBehavior were rarely used directly even for simple static objects. By having a StaticPlatformBehavior the level designer was spared from having to set up collision filter settings and the like because the Behavior could do that.

The Box2d setup of objects was hidden behind a layer of abstraction. And because this is (or should be) the case in most projects using the wrapper, I decided to not even bother with adding support for joints in the editor.
#28
02/13/2009 (9:24 am)
Hey Michael,

I read through your paper last night. You did a great job explaining not only what was need but why it was needed and I really enjoyed reading about the different options you considered.

In the end, it seems that you took the best path and I'm enjoying tinkering with it.

I noticed that if you mount an object (t2dStaticSprite) to the a polygon that has the physics behaviors applied, then copy them, the mount doesn't copy as well. Seems like a TGB issue though.

For those also playing around with it (without reading the paper), make sure you set the density of the objects to something non-zero or you won't get any action. Could be nice to have this default to something non-zero...

Patrick

#29
02/13/2009 (9:37 am)
Quote:I read through your paper last night. You did a great job explaining not only what was need but why it was needed and I really enjoyed reading about the different options you considered.
Thanks for the praise! I'm glad you liked it.

Quote:I noticed that if you mount an object (t2dStaticSprite) to the a polygon that has the physics behaviors applied, then copy them, the mount doesn't copy as well. Seems like a TGB issue though.
Yes that's just how TGB works. I would recommend writing functions ins script that do the copying "manually". The footnotes of the paper's appendix might be interesting for you too.

Quote:For those also playing around with it (without reading the paper), make sure you set the density of the objects to something non-zero or you won't get any action. Could be nice to have this default to something non-zero...
Yes, a mass of zero is Box2D's way of marking a body as immovable. Maybe it would be a good idea to default the value to something else but I don't want differ from regular Box2D with the default values. I'll think about it.
#30
02/17/2009 (3:10 pm)
I quickly did the implementation of Box2d (and that certainly why I bug somewhere).

Your guide is very well explained even if I don't know why objects refuse to move at all (and I know it's not because of mass or uncheck automass parameter, but rather because I probably miss something in the build VC5 project. I know it because replacing "Box2D Integration Sample.exe" with a build exe of mine give me unmoving result...).

But since I'm sure it's working and since I completely love the way you can interact directly inside TGB on Box2d parameters I'm wondering if you are going to give a try to the SVN version of Box2D ?

There is a TOP feature called "Edge Shapes" that give you the possibly to draw bound of any concave or convex form which is very handy for level design.
I can't even imagine using box2d again without.

I will give a second try tomorrow but I really lack of time these days, sorry :(

But : I was waiting your work since ages !
Thanks
#31
02/18/2009 (4:44 am)
Quote:I don't know why objects refuse to move at all
Did you add the source files in the "Cpp" directory to your TGBGame project and recompile? Do you get any error messages in the console?

Quote:But since I'm sure it's working and since I completely love the way you can interact directly inside TGB on Box2d parameters I'm wondering if you are going to give a try to the SVN version of Box2D ?
Since I am doing all of this in my spare time I cannot promise anything here. The original plan is to wait until there is another major Box2D release. But I already was thinking about rudimentary supporting some features that can be found in Box2D's svn repo (LineJoint for example).
I'll take a look at it but mind that my time is limited.

Quote:There is a TOP feature called "Edge Shapes" ...
Yes, I've seen that. It would be quite easy to write a wrapper for it, but to really make it useful it would have to have editor support, wouldn't it? How would you integrate that? With t2dShapeVectors maybe? It seems kind of a hack but it would be possible.
#32
02/18/2009 (1:33 pm)
Quote:Did you add the source files in the "Cpp" directory to your TGBGame project and recompile? Do you get any error messages in the console?
Yes and everything build up correctly through I must apply this patch to correct the few error messages :
http://www.garagegames.com/community/forums/viewthread/79755

Other than that : no errors nor warnings.

I will try with a Fresh new version of TGB and post news later on.
Quote:I'll take a look at it but mind that my time is limited.
Ok, no problem at all take your time and do it right. I don't know if box2d will officially upgrade again in the next months through. The Svn trunk is very stable since I'm using it inside my own Engine.
Quote:It would be quite easy to write a wrapper for it, but to really make it useful it would have to have editor support, wouldn't it?
I would already be very happy if you could access this new define object via torqueScript. About TGB interface integration I have to re-learn how to use it. I was waiting for many years now to be able playing with a full physics engine, so I never choose TGB as my engine of choice. But maybe this will change thanks to you !

The TGB Team should REALLY think about Box2d or ways to help his integration if there is license problem...
But if your solution cover most of the box2d situations I'll adopt it right now !
#33
02/18/2009 (2:40 pm)
It's like if Box2d project wasn't include into every created main TGB exe...
I miss about 100Ko on each compared to yours.
But I do compile Box2d, add the include/library/box2d.lib and checked the TGBGame box2D additional Dependancies !
Of course I also did the few tweak asked before and after.
The TorqueGameBuilder.exe grow for about 2mo so I suppose this is because Box2d is inside.

The only way I can use your integration obviously is by replacing my "gameData\T2DProject\TGBGame.exe" by your "Box2D Integration Sample.exe".
Could this be a valid temporary work around ?
#34
02/19/2009 (12:53 am)
Maybe you have only integrated Box2d in the debug build?

Quote:The TorqueGameBuilder.exe grow for about 2mo so I suppose this is because Box2d is inside.
Box2d does not have to be added to the TorqueGameBuilder project. I always only link it to TGBGame. Be sure you do all modifications only to the TGBGame project.

Quote:The only way I can use your integration obviously is by replacing my "gameData\T2DProject\TGBGame.exe" by your "Box2D Integration Sample.exe".
Could this be a valid temporary work around ?
Yes, you can do that. It is not the most recent version (does not contain GearJoint and PulleyJoint) but it should do the trick.

However, if you plan to use contact callbacks any time in the future, you will have to do your own build because they can only be used from C++. Script is just not fast enough to handle a few hundred callback calls every update step.
#35
02/19/2009 (3:38 am)
Quote:Box2d does not have to be added to the TorqueGameBuilder project. I always only link it to TGBGame. Be sure you do all modifications only to the TGBGame project.
I'm nearly certain that I only checked the TGBGame box2D additional Dependancies !
Maybe this come from somewhere else.
I will double check this when home.

About the TGBGame.exe I'm still searching... Fresh Install and redo step by step the implementation did not work for me.
Quote:However, if you plan to use contact callbacks any time in the future, you will have to do your own build because they can only be used from C++. Script is just not fast enough to handle a few hundred callback calls every update step.

Well that was my very next question ^^
Maybe a default b2DebugDraw implementation for TGB could be considered since it's mostly default drawing methods.
Unfortunatly about b2ContactListener I have no idea how I could interact from C++ to TorqueScript... Need to think about the best way to send ContactPoint in my TorqueScript game too.
#36
02/19/2009 (5:04 am)
I find my "error" !
This one is a dumb one...
In fact, don't ask me why, as no download for box2D v2.0.1 was available in googleCode I did not look further via SVN trunk. So I download box2D v2.0.1 from the official website (and wonder a lot on how you sucessfully wrap this with only 4 torqueScript files ^^).
That's because I didn't use Torquescript anymore since years and don't know how it work internally with C++.
I will use your SVN version when home.

I probably only have to read papers on how Torquescript can interract with C++ code. In the Box2d Manual there is an example with Buffered B2ContactPoint into array.
But if torquescript is too slow to process many ContactPoint Callback. Does that mean a part of my game shall be in C++ ? This would be bad...
#37
02/19/2009 (10:37 am)
Well... I add the files and mimic the tree organisation inside TGBGame project but now got those errors :(

1>------ Début de la génération : Projet : TGBGame, Configuration : Release Win32 ------
1>Compilation en cours...
1>Box2dJointEdgeRef.cc
1>C:\Program Files\Microsoft Visual Studio 9.0\VC\include\crtdbg.h(1123) : warning C4229: anachronisme utilisé : modificateurs de données ignorés
1>C:\Program Files\Microsoft Visual Studio 9.0\VC\include\crtdbg.h(1123) : error C2365: 'operator new' : redéfinition ; la précédente définition était 'fonction'
1>C:\Program Files\Microsoft Visual Studio 9.0\VC\include\crtdbg.h(1123) : error C2078: initialiseurs trop nombreux
1>C:\Program Files\Microsoft Visual Studio 9.0\VC\include\crtdbg.h(1123) : error C2440: 'initialisation' : impossible de convertir de 'int' en 'void *'
1> La conversion d'un type intégral en type pointeur nécessite reinterpret_cast, un cast de style C ou un cast de style fonction
1>C:\Program Files\Microsoft Visual Studio 9.0\VC\include\crtdbg.h(1123) : error C2143: erreur de syntaxe : absence de ';' avant '['
1>C:\Program Files\Microsoft Visual Studio 9.0\VC\include\crtdbg.h(1123) : error C3409: bloc d'attributs vide non autorisé
1>C:\Program Files\Microsoft Visual Studio 9.0\VC\include\crtdbg.h(1123) : error C2226: erreur de syntaxe : type inattendu 'size_t'
1>C:\Program Files\Microsoft Visual Studio 9.0\VC\include\crtdbg.h(1126) : warning C4229: anachronisme utilisé : modificateurs de données ignorés
1>C:\Program Files\Microsoft Visual Studio 9.0\VC\include\crtdbg.h(1126) : error C2365: 'operator new' : redéfinition ; la précédente définition était 'fonction'
1>C:\Program Files\Microsoft Visual Studio 9.0\VC\include\crtdbg.h(1126) : error C2078: initialiseurs trop nombreux

And more...
#38
02/19/2009 (11:44 am)
Did you add the TORQUE_DISABLE_MEMORY_MANAGER preprocessor definition?
#39
02/19/2009 (11:50 am)
I change :
Quote:void* FN_CDECL operator new(dsize_t, void* ptr)
{
return (ptr);
}

to

Quote:
#ifndef TORQUE_DISABLE_MEMORY_MANAGER
void* FN_CDECL operator new(dsize_t, void* ptr)
{
return (ptr);
}
#endif

in "platformWin32/winMemory.cc" as required.

But that's all for the Torque's Memory Manager. I don't know if I need to do something more.

All those annoying error is why I don't want C++...
#40
02/19/2009 (11:59 am)
Hum I try to add
Quote:#define TORQUE_DISABLE_MEMORY_MANAGER
to "enginesourceplatformWin32platformWin32.h" and "enginesourceplatformplatform.h".

No success either :(

[EDIT]
But I suppose It's not a good way so I remove this move and find the preprocessor directive in MSVC++.
Now I know where to add this directive.
And now I just read your message :) Thanks