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
#61
07/22/2009 (11:48 pm)
You pass %newLevelObject.CloneObjectName to cloneObject(), which has the value "Triangle". I suppose there is no object with the name "Triangle". So either you change the above object creation code so that the object to be cloned is assigned the name "Triangle":
%object = new t2dStaticSprite()
{
    name = "Triangle"; // A "prototype" field is not actually needed.
};
This way the TorqueScript interpreter can find the object when it calls cloneWithBehaviors() on it.

But you can also skip naming the object and just pass in %object directly for %cloneObjectName. This will yield the same result.

#62
07/23/2009 (12:43 am)
Hi Michael,

It seems that I have been unclear in explaining the problem.

Scenario #1:
There is an object called "Triangle" in TGB.
I have added Box2DBody and Box2DShape behaviors to it (though the GUI interface).
When I run the project, the AddToScene method gets called, and on termination, RemoveToScene method gets called. (I know this because I've put in an ECHO in both of the functions.)

Scenario #2:
There is an object called "Triangle1" in TGB.
I have added Box2DBody and Box2DShape behaviors to it (though TorqueScript).

This is the code:
function AddObject()
{
   %object = Triangle1.clone();
   %object.setPosition(0 SPC 0);
   
   %bodyBehaviorInstance = Box2dBodyBehavior.createInstance();  
   %ShapeInstance = Box2dShapeBehavior.createInstance();
   
   %object.addBehavior(%bodyBehaviorInstance);  
   %object.addBehavior(%ShapeInstance);
}

When I run the project, the ECHO in AddToScene method DOESN'T print for "Triangle1" object. However, on termination, the ECHO in RemoveToScene method is printed for it.

#63
07/23/2009 (1:08 am)
Yeah, I am still not sure what exactly you are trying to do here. Can you explain a bit more why you are doing all this. Otherwise it is a bit hard to help you. What is your goal with all this and where do problems occur.
#64
07/23/2009 (1:32 am)
I've been trying to figure out how to assign behaviors to an object via TorqueScript.

Here's what I'm currently trying to do:
I have an object called “Triangle” to which I have assigned Box2DBody and Box2DShape behaviors (via the TGB GUI).
Triangle has density set to 5.

Now, in TorqueScript, I clone this object using the following code:
function AddObject()
{
   %object = Triangle.cloneWithBehaviors();
   %object.setPosition(0 SPC 0);
}

When I run the project, the object gets cloned, and is drawn on the SceneGraph. However, unlike the original object, it doesn't fall downwards.
#65
07/23/2009 (1:48 am)
Do call createBody() and createShape() on the cloned object? If not, try the following:
function AddObject()
{
    %object = Triangle.cloneWithBehaviors();
    %object.setPosition( "0 0" );
    
    $YourBox2DSceneGraph.worldRef.createBody( %object );
    %object.bodyRef.createShape( %object );
}
#66
07/23/2009 (2:11 am)
It's started working... The last two lines of code you provided, did it!

Thank you very much!
#67
07/23/2009 (2:19 am)
That's great! Good luck with your project.

If you have not seen it already, here is short faq providing some examples on creating Box2D objects in script:
code.google.com/p/tgb-box2d-integration/wiki/FAQ
#68
08/10/2009 (4:54 am)
Hi Michael.

I see this project is not updated anymore ?
How was your thesis ?

Did you sucess to implement every new features of the current Box2D SVN (like edgechain for example) ?

Do you think it will be usable for real production ?

Wondering if Torque 2D will features something like this one day...

Thanks
#69
08/17/2009 (4:59 am)
Yeah, I haven't updated the code in a while now. There were no bug reports and my spare time was rather limited the last 6 months. The thesis was received well - thanks for asking :)

I didn't really look into porting 2.0.2 yet. I'll try to find the time to assess how much work it would be this week. If it turns out that I could do it in one or two days total then I'll probably update the code within the next weeks.

Any post-2.0.2 feature I will not integrate, as Box2D is undergoing some pretty big changes at the moment (see www.box2d.org/forum/viewtopic.php?f=2&t=3201). It will try to fix any reported bugs though.

Aside from that I think the code runs stable enough to be used in a commercial project.
#70
08/17/2009 (8:04 am)
Good news then !

Indeed, I think Box2D 2.0.2 is more than needed for real Box2D project.
Hope you succeed in this portage! Every new features until 2.0.2 work similarly and shall be portable while as you say the next big improvement is more doubtful even with my own engine.

I wish that your wrap for this engine was more officially/community supported.
#71
08/27/2009 (6:28 am)
I have taken a look at what would have to be done in order to support Box2D 2.0.2 and it seems like a bit too much work for me at the moment.

2.0.2 brings not only edge-shapes but also quite a few API changes and the controller framework. There are several time-consuming issues:

- There are many types of Controllers, some of which I don't understand from scratch. So in addition to just porting I would have to do some research to even be able to create a test-setup.

- Currently shapes are created by reading data from the scene-object that represents the shape in the T2D scenegraph. The polygon shape reads the collision poly of the scene-object, the circle shape reads the collision circle shape of the scene-object. For edge shapes there is no corresponding data in the scene-object it is bound to, which jumbles the way of creating shapes using the wrapper. This means a bigger re-factoring of the existing code would be needed before edge-shapes could be integrated proberly. The Box2dShapeBehavior would have to be split into a Box2dPolygonShapeBehavior, a Box2dCircleShapeBehavior, and a Box2dEdgeShapeBehavior. I would have to update the existing test cases and more importantly I would have to create and maintain two sets of documentation, or make breaking changes to the 2.0.1 branch.

None of the above would be a really hard task in terms of programming. However the codebase is complex enough and this is still C++, so a lot of testing would be required to verify that everything works. Writing test code would probably a bigger task than changing the wrapper itself. When I originally wrote the wrapper it was used immediately in a project over the course of a few months, so testing was not much of an issue as bugs would become visible rather quickly. Now it's different.
I would estimate that providing a clean update to 2.0.2 would take at least one to two weeks of work if not more. This is clearly more than I can spare right now.
#72
10/30/2009 (2:37 am)
Sorry if I'm bringing up an old topic, but I'm trying to implement Michael's Box2D wrapper and for the life of me I can't make it work. I've followed the instructions on his thesis, I've read as much as I can find on other problems in implementing it, and I'm not sure where else to go with this.

I've just attempted a second compile, got all the way to building the pyramid example scene, and the Box2D Bodies won't initialize. (Page 28, step 11.)

The error report looks like this:

    Compiling D:/DevelopmentTools/TorqueWorking/Pyramid/game/data/levels/Pyramid.t2d...
    Loading compiled script D:/DevelopmentTools/TorqueWorking/Pyramid/game/data/levels/Pyramid.t2d.
    game/gameScripts/Box2dScene.cs (127): Unable to instantiate non-conobject class Box2dWorldRef.
    game/gameScripts/Box2dScene.cs (155): Unable to find object: '0' attempting to call function 'createBody'
    Box2dScene::createBody() - Error creating Box2dBody
    game/gameScripts/Box2dScene.cs (155): Unable to find object: '0' attempting to call function 'createBody'
    Box2dScene::createBody() - Error creating Box2dBody
    Invalid bodyRef!
    game/gameScripts/Box2dScene.cs (96): Unable to find object: '' attempting to call function 'createShape'
    Invalid bodyRef!
    game/gameScripts/Box2dScene.cs (96): Unable to find object: '' attempting to call function 'createShape'

I'm wondering if this has to do with the "disable TGB's memory manager" step, but I followed that exactly per the procedure, and I don't see what went wrong.

I'm compiling with Visual C++ 2008 Express, using Box2D Version 2.0.1, TGB version 1.7.4. Also using Microsoft Platform SDK for Windows Server 2003 R2 and Microsoft SDK Ver. 6.1


Just to go step-by-step here, I made installed a new copy of TGB Pro, compiled with no errors, then started following the thesis step-by-step.

I'm suspicious of the memory-manager step because of the error message I listed above, and because at the step in the thesis where it's implemented, I could not compile it as written. This is odd, but here's what happened:

I first wrote the code out as-is on page 13 of the thesis:

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

I couldn't compile it.
I then looked at the source Michael referenced, right here, and used the code Pat Wilson had listed:

# #if !defined(TORQUE_DISABLE_MEMORY_MANAGER)  
# void* FN_CDECL operator new(dsize_t, void* ptr)  
# {  
#    return (ptr);  
# }  
# #else  
# #include <new>  
# #endif

Which is the same, except for the #include <new>.

I was able to compile the source as-is, and continued with the thesis. But I ran into the error message I listed above, where I couldn't create a Box2d body. I eventually went back to winMemory.cc and rewrote it back the way Michael's thesis had it shown, and for some reason (unknown to me) the code compiled this time. I still got the error, and couldn't create Box2d bodies or shapes.

I'm really not sure what I'm doing wrong, as it seems others have been able to get it to run. So I'll ask, even if you don't know how to fix my problem, if you could at least show me how you've done it. I feel like I went way over my head in trying to implement this, even with practically all the work done for me (Michael Woerister, thanks so much for doing this! Even if I can't get it to run, thanks very much!) but I would very much appreciate hearing from someone who has compiled this successfully.

Thank you!
#73
10/30/2009 (8:43 am)
Hey Jubal,

from the error messages I would suspect that you did not add the c++ files of the wrapper to your Visual Studio project. These are the files in "Cpp" folder of the svn repository (Box2dBodyRef.h, Box2dBodyRef.cc, Box2dShapeRef.h,...). In the TGBGame project, right-click on the T2D folder, create a new "Box2D" folder and add these files to your project.

If it still does not work, make sure it actually was compiled without errors. Otherwise you might run an old version of TGBGame.exe still lying on the disk. Another thing to look out for is not to mix up DEBUG and RELEASE builds. If you compile in DEBUG mode, the output executable will be called TGBGame_DEBUG.exe (or something like that). If you then hit the play-button in TGB it will still use TGBGame.exe which was not built with the new code yet. So if you want to run your game from TGB you must compile it in RELEASE mode.

As for the memory manager: I don't think that could cause such a problem. If it compiles in one of the two ways you mentioned, it should be fine.

Tell me if that solved your problem. I'm glad to help you get it to run.

-Michael
#74
10/30/2009 (1:13 pm)
Thanks for replying! I'll try your suggestions when I get a chance, as I'm at work right now.

I thought I had added those files, but I may have missed a step, so I'll dig back into the project and make sure they're there. If it still doesn't work, I'll try to list all the steps that I've gone through in setting it up, in case I'm doing something wrong. Thanks for helping!
#76
10/30/2009 (5:26 pm)
Doesn't mean much for current projects but for future projects, woot!
#77
10/30/2009 (5:57 pm)
Thanks for the news, Michael Perry! I've been getting frustrated after digging into Torque Game Builder's physics engine, seeing it's limitations that make rigid body physics very difficult to implement in a usable way (At least in ver. 1.7.4) and got depressed further from reading earlier posts that seemed flaky on the idea of GarageGames ever adding it to a future release... and now I see this! This is amazing!

In my humble opinion, even just simple Newtonian physics adds a tremendous sense of realism, and intuitiveness, to many, many kinds games. It's something we deal with every day, heh, in every step we take. (Literally!) It's great to see you're implementing one of the best, most fully-featured open-source physics simulations I know of, and my only disappointment is waiting for the release.

I'll still try to get Michael Woerister's wrapper working in the meantime. Now I have to wonder how much development I'll have to hold off on before Torque2D is ready for release...
#78
10/30/2009 (8:31 pm)
I got it! Thanks Michael Woerister! I guess this shows I really am new to TGB. Anyway, the problem was the game executable I was using wasn't the updated version. The compile was fine, I just wasn't running it. I copied the debug executable into my game directory and it works!

So to clarify, if I only create a debug version of TorqueGameBuilder and TGBGame, then while running it, I make a new game and choose to copy the executable into that folder, it's going to copy the release version, not the debug version. If I want a debug game I either have to build the project without copying the executable, or copy the debug one manually. If that is how it works, then I'm hopefully learning something! And it was such a simple mistake... (If you're reading this and attempting the same thing for the first time, and are new to TGB, I hope this will help you avoid a similar mistake.)

Anyway, thanks again Michael for going to all this work! I've played around in Box2D before, and had some game design ideas for it, but I don't really have the know-how or time to implement it the way you have. This makes things much more streamlined.

Oh, and here's hoping Torque2D will build on this as well. It would be great to see the engine fully-implemented. Anyway, thanks all!
#79
10/31/2009 (6:02 am)
Quote:I guess this shows I really am new to TGB.
Don't worry. Things like these can happen to anybody. I am glad you got it to work.

Quote:Torque 2D Developer Blog - Box2D Implementation
Of course once this is out, it will be the much better supported solution for using Box2D in T2D. My integration is more of a compromise that glues together two system that only fit together 70%. Whereas this seems to be a Box2D integration from grounds up :)
#80
01/22/2010 (6:03 pm)
Michael, fantastic resource. I just finished porting this to iTGB v1.3 and your implementation works very well on the iPhone. Thank you for taking the time to put this together and sharing it! I will make sure your name is listed in our next game under the "Special Thanks To" section.