createFixture optimization ( static C struct definitions?)

Hello, I have a tile based game that loads and unloads geometry from the level on the fly as the player move around.

I took a look at this post and it seems like I have a slightly different constraint that forces me to not break the geometry up into smaller chunks.
viewtopic.php?f=9&t=8387&p=35942&hilit=optimize+createfixture#p35942

so basically I have long chains that represent the collision of a vector drawn world. some chains can be 2000 verts in length and since the shapes are organic, they can overlap each other.

on an iPad 1, the createFixture call can take up to 0.06 seconds for some of the longer chains. the game currently runs at 60fps so I get a hit of 4 frames while this single load takes place.

ideally I want to get it running on ipod 2nd gen so there is a ton of optimization I need to do.

most of the data is currently defined in c structs and it would be pretty easy to make a static definition at compile time of the fixtures and shapes if it wasn't a class. Is there any way to get around this?

It seems like the time is linear based on the number of verts in the chain so cutting it up doesnt look like it would speed it up at all.

the other option I thought of, was to create a world or body that was not in use, create the fixture in a separate thread, and then move it over to the main world when I need it. I can't really split the game into multiple worlds that each represent a grid in the level. the shapes overlap and it would be a mess if I had to deal with multiple worlds at the same time.

Any other ideas for speeding up the creation of a fixture?

I'm currently using 2.2.1

Thanks
I'm puzzled as to why this loading cannot be broken up. If you can load one chain with 2000 vertices, surely you can load ten chains of 200 vertices? The only way you can keep things smooth is by spreading the load over a longer time span.
irresistible force wrote:I'm puzzled as to why this loading cannot be broken up. If you can load one chain with 2000 vertices, surely you can load ten chains of 200 vertices? The only way you can keep things smooth is by spreading the load over a longer time span.


While that is definitely one potential solution but I would like to explore other solutions first.

breaking it up even more would create some non trivial refactoring in our engine which I would like to avoid.

Each platform is a game object and our current grid/culling engine works on objects, not sub objects. Each platform has it's own run time logic associated with it, so it can move/rotate/etc and breaking apart each platform into multiple game logics would make it messy when I want to move them as a single entity. The other way to break them up would be to make each platform have multiple fixtures and have it do it's own culling which is also not ideal.

It seems that as game worlds get bigger and we want higher fidelity in our games, that it would be good to have some fast paths in order to load geometry on the fly. It would be great to have a way to precalc as much as possible into a data file and just load it into memory (with a light class wrapper) so box2d doesn't have to process as much. I'm sure there must be some kind of initialization where new geometry needs to be inserted into some efficient run-time data structures that can't be avoided but there must be a way to optimize that path.

some other thoughts I just had was to precreate a pool of fixtures and just change the shapes in and out. not sure if this would speed it up.

Also, what about my earlier questions. is there anyway to use multiple threads and exchange fixtures between bodies once it's created?

or any way to statically define some of this before hand?
I fixed CreateFixture's slowdown by preloading the broadphase tree and manually updating the world's b2Body list each frame. To do this, you'll need to create all bodies as inactive, create all fixtures, and then manually update the b2Body::m_flag back to active. Each time I render, I query an aabb and add those bodies to the world's body list, just by adding it to the world's b2Body list (each body has an m_next member...).

If a body is referenced in a collision or raycast but isn't in the world's list, the behavior is undefined, but I haven't seen any issues so far, other than it not responding correctly to collisions. For that issue, just make a window of activity larger than what the camera can see, so that you are not seeing oddities as objects become enabled.
pTymN wrote:I fixed CreateFixture's slowdown by preloading the broadphase tree and manually updating the world's b2Body list each frame. To do this, you'll need to create all bodies as inactive, create all fixtures, and then manually update the b2Body::m_flag back to active. Each time I render, I query an aabb and add those bodies to the world's body list, just by adding it to the world's b2Body list (each body has an m_next member...).

If a body is referenced in a collision or raycast but isn't in the world's list, the behavior is undefined, but I haven't seen any issues so far, other than it not responding correctly to collisions. For that issue, just make a window of activity larger than what the camera can see, so that you are not seeing oddities as objects become enabled.


Thanks, I am doing something similar. I am creating the bodies and fixtures at the start of the level. This isnt ideal because it takes up loading time and also memory but I think it's better than splitting everything up into smaller chains. I did a test, our minimum required device is the iPod 2nd Gen, and you can only create a chain with 200 verts and keep it under 1/60th of a second. and that doesnt account for any other processing you might have going on that would cause you to skip a frame. 200 vert blocks is way to small for our world.

Original thread: https://box2d.org/forum/viewtopic.php?t=8683