Game Development Community

Chipmunk Physics in iTGB

by Benjamin Cole · in iTorque 2D · 09/07/2010 (2:33 pm) · 87 replies

Hi There,

I am working on replacing iTGB's physics with something lighter and friendlier and running into some difficulties. I am just learning how XCode structures projects, so it's been like pulling teeth so far.

I'm not delving into the actual T2D source, and I'm a little puzzled as to what actually needs to be stripped and replaced. Ideally, I'd like to keep the tourqueScript callbacks, and have chipmunk or Box2D silently replace the physics, even if I lose some of the additional functionality that these platforms offer (really, I'm only doing circle on circle and circle on tilemap collisions anyway).

I'm trying to figure out where the physics scene in TGB is setup, where objects are added, and where the physics steps are actually called. I'm assuming each object is created as a t2dSceneObject, and the physics setup and step should be in the t2dSceneGraph... but I'm having trouble finding out where this actually occurs. I'm not a C++ expert, but I know my way around OOP and the source is pretty readable.

Can anyone who has done this point me in the right direction?
Page «Previous 1 2 3 4 5 Last »
#1
09/07/2010 (11:37 pm)
Benjamin

I was about to post about Box2D and just saw your old post www.torquepowered.com/community/forums/viewthread/119307 and must have missed it.

I have installed Box2D in the Visual Studio version and it works.

-What version of iTGB are you using mac/win.
-What can I do to help you get box2d working?
#2
09/08/2010 (1:49 am)
Hi Dean,

I'm working with Xcode on the Mac so I can build for the iPhone. I was working with Box2D, and was able to get it compiling and linking with Xcode based on the now famous Michael Wörister tutorial - but I wasn't able to get it actually seemingly effect the scene.

Since I didn't know exactly what I was doing, and was just using his behaviors and wrappers it's a difficult fix, and I'm not actually sure I need that layer of control anyway. I have used box2d AS3 before, so I am basically familiar with it, but I did my own implementation so I knew how to control it.

I'm actually now having trouble getting Box2d to compile to a static library on the iPhone... hence the move to chipmunk where I was able to compile without a problem.

I figure any physics implementation should be pretty similar though. My current plan is to create a class to hold physics scene in the t2dSceneGraph, use the addToScene method to push them into the scene, and grab the properties from a t2dSceneObject.

I suppose on a tick, I'll update the physics scene, and set the new positions of the screen objects accordingly. .. I'm not sure if I'm way off on this or not.

I have 3 current problems:
1) Where is the current physics model actually being set up and called from? I thought it would happen in t2dSceneGraph::processTick(), but it looks like this controls a lot more than just physics, and I can't seem to "just" turn off physics here.

2) I have not tested it, but I'm assuming that the getCollisionDetectionMode property will tell me if I should set the physics object up as a sphere, cube, or mesh. I'm not sure about how tilemap collisions are being set up however.

3) I'm using sensors currently to trigger events. I want to make sure I don't lose these as well, but I'm not sure how tightly they are coupled to the existing collision/physics model. I'm having a hard time parsing what's actually going on here.
#3
09/10/2010 (12:02 pm)
Hello i´m beging not setup up a mackbook pro for iphone development and box2d is something I would like to add, can you share the steps to add it to iTGB1.4
thanks
#4
09/10/2010 (3:57 pm)
Hi there. Thought I'd drop an update:

I have Chipmunk working with my scene, although I'm still running into some problems. My solutions are kind of hack-y. As soon as I get it properly working I'll document my steps.

The main problem I'm running into is disabling torque's physics operations without disabling things like "setPosition()". I'm finding the physics code, and all of the integration steps extremely difficult to follow. If anyone has any experience with this, I'd love to know what you've done to fix this!

My performance is on the bad end of acceptable right now - but I feel like if I could totally remove all of T2D's collision checks and bounds checking I'd be peachy.
#5
09/10/2010 (6:32 pm)
Benjamin, just curious, are you swapping out the Torque physics because you have finished optimising and still cant get the performance you require or is it because Chipmunk offers something that Torque doesnt?
#6
09/10/2010 (6:51 pm)
Hi Scott,

I hadn't finished optimizing the Torque physics, but I decided to swap because I was getting some very inaccurate simulations out of the built-in solver. I tried both strictly using collision responses, and just using the collision callbacks and adjusting the vectors "by-hand".

This is only for a simple tilemap collision and "billard ball" type physics, but spheres were getting caught on the corners of spheres, and spheres were getting caught in tile collision (even at low speeds).

Chipmunk isn't noticeably faster (actually, it seems slightly slower so far - but I'm running the simulation at 60fps, I haven't optimized at all, and I'm still using 10 iterations), but I'm already seeing a much more accurate simulation.

- Ben
#7
09/10/2010 (7:05 pm)
Box2D *used* to be faster, but I dunno what it's like now. I think it has more options you can toggle on and off, though, potentially gaining somewhat more speed.

Framerate in the simulation has as much to do with device framerate as the weather in Antarctica has to do with the number of words per minute a secretary in Brighton can type.
#8
09/10/2010 (7:09 pm)
Ha! Yes, I know that. I meant to say that I'm calling the update every tick in the scenegraph (currently around 60 times a second). I hadn't done any serious profiling, but I imagine I could do this less often and get less stuttering.
#9
09/10/2010 (9:59 pm)
Bah! Now I'm getting really bad (and weird) tunneling through static shapes. I'm adding static square bodies to the scene based on the position of the tilemap collisions... even just creeping along.

At least it's "kinda" working :).
#10
09/13/2010 (3:04 am)
GOT IT! WOOHOO!

Holy crap is this so much better than the built in solver! I'm working on making it seamless, but the basic configuration is working perfectly now! It's fast, solid, and easy to use. The only issue (resolved now) was that I wasn't using the setters correctly, and was trying to modify the physic body properties by setting the m and p properties of the physics bodies.

Now that that was (finally) fixed, it's beautiful! I do have some really sloppy code, and a LOT of the base t2d stuff commented out -- but a bit of clean-up work and I'll put together a quick guide on how I did it.

I'm feeing better about C++ now at least ;). I kind of went into it blind.
#11
09/13/2010 (3:49 am)
Yayification! Looking forward to a resource, then :)
#12
09/13/2010 (5:06 am)
Quick Q to all C++ guys out there: I am not keeping a reference to the shapes I am creating in my Chipmunk Scene. Do I need to keep these references and manually remove and delete them, or is T2D managing my memory for me? I'm only loading 1 test level right now, and don't appear to be leaking memory while I'm running the scene but I'm worried about leaking over destroying and adding many Chipmunk Physics Spaces.

This may be Chipmunk Specific - but if anyone knows, I'd like to... uh... know too.
#13
09/13/2010 (2:40 pm)
Nice work Benjamin, I'm looking forward to the results as well :)
#14
09/13/2010 (2:49 pm)
@Benjamin: Not sure about Chipmunk, but you're definitely not using CocoaTouch or iT2D memory management, so you need some way to keep track and destroy objects as needed. You'll need to have a look in the Chipmunk source or ask the author, or when all else fails, read any documentation there might be :)

Also try the static analyser and see if it can spot anything.
#15
09/13/2010 (3:00 pm)
I just checked out the Chipmunk page again, and there's an Objective-C wrapper available now. That gives you the benefits of the Cocoa memory management.
#16
09/13/2010 (5:14 pm)
There is no garbage collection for iPhone applications - it isn't ideal for mobile applications.
#17
09/13/2010 (5:42 pm)
I don't know much about the TGB memory manager, and I wasn't sure if this was doing any garbage collection. Unlike the Box2D setup, I didn't remove this from my project. Also, I'm keeping my physics simulation in my T2dSceneGraph.

It's no big deal to remove the references myself, I just didn't want to duplicate work if TGB was already doing this for me somewhere.

I'll put up some tests of how it's running.
#18
09/13/2010 (5:52 pm)
I was referring to iPhone OS stuff, but the Torque memory manager is by default disabled via compiler flags in iTorque too. Handling the Box2D memory yourself would be the way to go.

By the way, kudos on getting this working given you're by your own admission "not a C++ expert". :D
#19
09/13/2010 (6:02 pm)
Har, I wouldn't even consider myself a C++ beginner. I know Java pretty well though, and Actionscript. So aside from having to manually manage memory it seems pretty similar.

Edit: Meant to say "I'm not good at C++ at all", not "I'm a cocky such-and-such".
#20
09/14/2010 (1:08 am)
@Craig: Cocoa memory management is more than just garbage collection. A memory pool you handle yourself might be helpful, and the ObjC API might be easier than the default Chipmunk API. I know the C API hasn't always been the greatest ;)
Page «Previous 1 2 3 4 5 Last »