Discussion on using Box2D for a top down game

Hi everyone,

I've thus far managed to integrate Box2D into my top down engine, as well as getting it to work and so forth. I'm still thinking exactly how to handle the physics world so that Box2D performs well and I get the results that want. This is as far as I got (within 24 hours).

Image

I made a basic convex hull using a few triangles spread around the star. This was hand done, so I didn't make an algorithm to do it mainly because I knew this idea wouldn't work. It was mainly just to test Box2D and to get it working.

As this game is top down, there is space that is defined as the play area, and space that is simple void. Most of the time I see Box2D being used for platform side scrolling type games, but I don't think that this will stop it from working in this particular case.

The hard part now, is to figure out how to generate the convex hull that will be suitable for levels. I had one idea of inversing the playable space and triangulating the triangles which represents the void space with extents just slight larger the the entire play area. This could lead to very large Box2D simulation worlds, which I read is quite bad on performance.

I could potentially split up the level into several small Box2D worlds instead, but unless I do per major sector then I would still have issues of how to generate convex hulls properly.

Anyways, I'm still thinking about it generally, and I was just branching out to this community as you guys would have used this library a lot more than I have currently... I did find a few posts about convex hull generation, but I haven't found any solid examples just yet.
Box2d can handle pretty large levels without too much fuss, both size and lots of edges. If it really cannot handle it, careful adding and removing of bodies (culling) should get you the rest of the way. Creating multiple worlds (b2World objects) is not an advised route.

You basically want to create a lot of static shapes to represent the the boundary of your play area. So yes, if you take a square larger than your play area, and subtract the play area you'd have something about right, but you'd still need to split it into multiple shapes as it would not be complex. How best to create the shapes is your department. Many people just manually input their levels, in code as in the testbed. The convex decomposition code, and related tools are a way to speed up this process, which is probably worthwile if you have a complex shape. E.g. if you have your play area as a series of vertices, you could manipulate that into its inverse quite easily, and then just fire up the java splitting code and pass it through. In any case, I wouldn't spend too long worrying about the exact amount of shapes used. Smaller shapes does mean there are more to test against, but in practise the broadphase should ensure no performance loss.

Box2d is suited fine for top down games. I suspect they are less common as people do not need full phsics engines for them so often.
Ahh yes I see. The method I might try is triangulation via ear clipping to generate the convex hull. Each shape will only consist of three edges, so a triangle each. Once the triangles are generated, I then do a second sweep to ensure that at least one of the edges of the triangle plays a part in the convex hull, as some triangles may wind up being generated that won't ever participate in the convex hull.

The reason why triangles are used, as compared to boxes or whatever, is because the level designer is allowed to make any sized shape sectors he wants. The level designer is given as much freedom as they would have in say Build or Doom engine, so thus very small wedges may exist that are blocking. So boxes of any prefixed width or height would always usually fail as it would clip through another wall.

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