Box2D Forums

It is currently Sat May 25, 2013 2:03 pm

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: Thu Jan 12, 2012 12:49 pm 
Offline

Joined: Thu Jan 12, 2012 11:45 am
Posts: 3
I would appreciate a comment on how sane my approach is from someone who knows the library internals. :-)

This is regarding the C++ library. I have a 2D simulation that has layers. There is a physical simulation on multiple layers. Objects on one layer never collide with another layer.

My implementation of Box2D for this environment is to have a b2World for each layer. This is especially nice because I can specify different Box2D simulation resolutions for different layers to make scene-specific perf tradeoffs.

A typical level in my game has 15 or so layers so I am only spawning b2Worlds for each layer that needs it in order to conserve resources.

There are times in the gameplay where an object jumps from one layer to another. In the b2World, I am making a copy of the object in one b2World and removing it from the other. I suspect I'll need to eke out what forces and torques are being applied to the object so that it is a clean transition.

Thanks for reading!

Michael Labbe


Top
 Profile  
 
PostPosted: Thu Jan 12, 2012 6:44 pm 
Offline

Joined: Tue Jun 24, 2008 8:25 pm
Posts: 1517
Location: Tokyo
It seemed quite sane until you said 15 layers. I don't think 15 would be a problem, but I am wondering if you know about the possibilities for collision flags of fixtures. You can set the group id for fixtures so that entire groups of objects will never collide with each other, or more selectively with the collision category/mask bit values. These are 16 bit values but can safely be changed to 32 bit if you need more control. There is also the ShouldCollide callback you can set as well.

The only time you would really need to have a separate world is as you say, when you want them to use different step times/frequencies. Making objects jump between layers would be easier if all you had to do was change their fixture's collision settings.


Top
 Profile  
 
PostPosted: Sat Jan 14, 2012 7:21 pm 
Offline

Joined: Tue Sep 25, 2007 2:22 pm
Posts: 483
Separate worlds may hurt memory usage slightly, but you'll reap performance gains if you have many objects. One SVG becomes one b2World, and separate worlds was the cleanest way to manage these separate layers.

I also, perhaps unusually, use Box2D for my UI system, including all dialog boxes. It was very easy to map WM_MOUSE_DOWN or UIView.touchesBegan and associated events to a Flash UI button behavior. UI is the main use case where I want to activate and deactivate b2Worlds throughout gameplay.


Top
 Profile  
 
PostPosted: Sat Jan 14, 2012 7:49 pm 
Offline

Joined: Tue Jun 24, 2008 8:25 pm
Posts: 1517
Location: Tokyo
oh yeah...
if the worlds have a lot of calculation involved, separating them would be an easy way to get multi-threading for better performance on multi-core CPUs.


Top
 Profile  
 
PostPosted: Wed Jan 18, 2012 12:34 am 
Offline

Joined: Thu Jan 12, 2012 11:45 am
Posts: 3
irresistible force wrote:
It seemed quite sane until you said 15 layers. I don't think 15 would be a problem, but I am wondering if you know about the possibilities for collision flags of fixtures.


Thanks for pointing this out. The most reasonable implementation for me seems to be a single b2World for the 4 layers that gameplay happens on. The rest of the layers are graphics-only. I burn a nybble on collision checks for the 4 gameplay layers.

I can spawn additional rbWorlds for effects layers that need physical simulation outside of the gameplay world.

irresistible force wrote:
if the worlds have a lot of calculation involved, separating them would be an easy way to get multi-threading for better performance on multi-core CPUs.


This will become important to me in a couple weeks because I have multiple, concurrent game levels simulating on the server. I tried looking around on the net for reports of people running multiple b2Worlds on concurrent threads but I did not see any. Have you witnessed anyone getting this to work?


Top
 Profile  
 
PostPosted: Wed Jan 18, 2012 2:23 am 
Offline

Joined: Tue Jun 24, 2008 8:25 pm
Posts: 1517
Location: Tokyo
I have not seen any either, but as long as the worlds are completely independent it should be just a matter of creating a thread in the normal way for whatever OS you're on.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: Exabot [Bot] and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group