Problem Solving: Stretched bodies

Each of the tiles pictured below are 32 pixels wide by 32 pixels high. Regardless of whether or not the images make up one long platform, or a curvy one, etc, each 32×32 pixel tile had a 32×32 pixel “body” associated with it. In this sense, a body is the “physical” object represented in a Phys2d world. Static bodies make up the world, because they don’t move. Other bodies, such as the red box I’ve pointed to, can move and are subject to gravity and physics. But I’m getting ahead of myself.

Earlier this week, I ran into a problem with the hero getting stuck between tiles. Now, he didn’t fall between them, he just simply stopped moving as if he were colliding with a wall. This was no good, as he was simply walking along a flat surface. This may be a small limitation of the Phys2d engine, but I am certainly not complaining. The solution was actually pretty simple.

Rather than create n number of bodies for n number of tiles, I scan the world and create one long, stretched body for the entire span of adjacent platform tiles. This does not cut down the number of bodies to the bare minimum, as I have a feeling I may be able to subtract the platform tiles from a much larger body, and then invert the values when the scan is done. Still, counting the number of bodies above, the static bodies, the ones that never move, I see 18. That’s certainly a lot fewer than even the number of tiles used in only the first 5 lines of the map.

Now the hero has flat surfaces to walk on and the number of bodies to keep in memory have been reduced.

Source code snippet/solution found here on the Slick forums.

Leave a Reply

 

Staypressed theme by Themocracy