Page 1 of 1

Polygon Shapes in Tilemap Based Multi-Layer Levels

Posted: Tue Jul 19, 2011 4:02 pm
by RPG Hacker
Hi there!

I just recently started using Chipmunk Physics since I didn't feel like coding the Physics and Collision Detection for my game myself. I'm using the FreePascal binding, which means the version I'm using is 5.3.2 (so several new functions aren't available to me yet).

Anyways, in my game I have support for an unlimited amount of layers, which can also use tilemaps. The main purpose of this is to have moving level segments, like in Super Mario World. I thought about a way of utilizing this system using Chipmunk. My idea was to let each layer have it's own Body (or Static Body, if a newer version of the binding gets released) with each tile being a Polygon Shape. That way I could easily edit a tile while a level is being progressed and I could easily assign certain properties to each tile.

However: In the documentation it is said that Polygon Shapes use the most performance. Since I'm planning to have somewhat big levels in my game and don't feel like always removing all Polygon Shapes that are offscreen (especially since I want a very few sprites to continue progressing, even when offscreen) I wonder if using this kind of system would reqiures too much performance. After all having two ore more big layers in a level would mean lots of Polygon Shapes. I don't want to start coding a system that won't work out, anyways.

I also wonder if when using multiple Static Bodies like that in a level the Static Bodies would influence each other (like pushing each other aside or something like that). Right now it probably doesn't matter, since this version of the binding doesn't support custom Static Bodies yet and I have to add the Shapes seperately, anyways. I'd still like to know, in case a new version is released.

Also I'd like to know if anyone has a good method for moving layers like that around. Directly setting the position vector does work, but the sprites that are currently on the layer don't move with it correctly. If moving a layer down, for example, the sprites start falling and landing on it over and over. Since I don't add the layers' Bodies to the Space, but only their Polygon Shapes, I guess I can't use most of the special Body functions, either, or can I?

I'd really appreciate any suggestion.

Re: Polygon Shapes in Tilemap Based Multi-Layer Levels

Posted: Tue Jul 19, 2011 7:34 pm
by slembcke
Well collisions with static shapes are largely free. They don't ever have to be updated because they don't move, and they aren't checked against other static shapes. A shape per tile might use a lot of memory though.

However when you start moving an entire layer with hundreds of shapes attached to it, it would get probably get pretty expensive. This isn't the sort of thing that Chipmunk's collision detection is tuned for. It's sort of a pain to do, but you would be much better off tracing around the edges of your tiles using a small set of line segments. That would be a lot less expensive, but it really just comes down to how many collision shapes you have active at a time.

Re: Polygon Shapes in Tilemap Based Multi-Layer Levels

Posted: Wed Jul 20, 2011 4:23 am
by RPG Hacker
slembcke wrote:However when you start moving an entire layer with hundreds of shapes attached to it, it would get probably get pretty expensive. This isn't the sort of thing that Chipmunk's collision detection is tuned for. It's sort of a pain to do, but you would be much better off tracing around the edges of your tiles using a small set of line segments. That would be a lot less expensive, but it really just comes down to how many collision shapes you have active at a time.
Hmm... I see. I'll guess I'll stick to simple movements then with complete layers or only update a very few sprites (like, only those on the screen or only the player). I guess for complex movements I'll use small elevator sprites or something like that. Would those be easier to do, btw.? Like, if one Body is standing on another Body which is used as an elevator, would the position updating of the Body standing on it work any easier?

Re: Polygon Shapes in Tilemap Based Multi-Layer Levels

Posted: Wed Jul 20, 2011 8:58 am
by slembcke
Yes, things like moving platforms are easy enough. Basically you just create an infinite mass rogue body. (rogue == don't add it to the space because you'll simulate it yourself). Then update the position and velocity each frame. The velocity must equal (newPosition - oldPosition)/timestep or objects will mush into or ping off the surface.

Re: Polygon Shapes in Tilemap Based Multi-Layer Levels

Posted: Wed Jul 20, 2011 2:23 pm
by RPG Hacker
Wait... so would the same thing work with a complete layer if I replaced the Static Shapes with regular Shapes? After all they're also Rogue Bodies. I know that using lots of regular Shapes would cost a lot of performance, but I could make it so that only a very few tiles use regular shapes. Like, for example, only the tiles you can actually stand on and only on layers that actually move (or have a different scroll speed than x1). I guess moving layers usually don't turn out as big as regular layers, anyways, therefore I wouldn't have lots of collision Shapes to simulate. Would that work?

Also I have another question. Let's say I wanted to make a solid block of 16x16 pixels and wanted to prevent small sprites to get stuck in it by a bug or whatever. In other words: A "filled in" block. Could I use Segment Shapes for this with a thickness of 16, or do I get the thickness value wrong? And do Block Shapes or Polygon Shapes work, or do they only create the outlines of the block?

Re: Polygon Shapes in Tilemap Based Multi-Layer Levels

Posted: Wed Jul 20, 2011 3:44 pm
by slembcke
No. Static shapes never check for collisions against anything. Normal shapes check for collisions amongst themselves, then against the static shapes. So although a moving layer like that isn't really static, it would be cheaper to reindex all of the shapes each frame manually than to have Chipmunk treat them as normal shapes.

The problem is the sheer number of moving shapes, each one has to have it's collision detection data updated every single frame. Anything you can do to cut that number down is good. Chipmunk performs all of it's collision detection in world space, as that's the fastest way to do it in the general case. Very few physics games have massive numbers of objects attached to a single moving body.

"fat" segment shapes are pill shaped, not rectangular. You would have to use a polygon shape instead.

Re: Polygon Shapes in Tilemap Based Multi-Layer Levels

Posted: Wed Jul 20, 2011 4:17 pm
by RPG Hacker
slembcke wrote:No. Static shapes never check for collisions against anything. Normal shapes check for collisions amongst themselves, then against the static shapes. So although a moving layer like that isn't really static, it would be cheaper to reindex all of the shapes each frame manually than to have Chipmunk treat them as normal shapes.

The problem is the sheer number of moving shapes, each one has to have it's collision detection data updated every single frame. Anything you can do to cut that number down is good. Chipmunk performs all of it's collision detection in world space, as that's the fastest way to do it in the general case. Very few physics games have massive numbers of objects attached to a single moving body.
I see. Well, I guess I'll just stick to sprites falling down then when a layer moves or maybe instead of using layers for this in the first place I'll just split it down into multiple big sprites. After all, only because the SNES used layers for this, I don't have to. :P
slembcke wrote:"fat" segment shapes are pill shaped, not rectangular. You would have to use a polygon shape instead.
So Polygon Shapes do include the room in the inside of the Shape and not just the outline? That's all I want to know. I want to prevent the player from getting stuck in the ground when, for example, moving too fast. From what I understand the probability of this happening is reduced if the ground is bigger.

Re: Polygon Shapes in Tilemap Based Multi-Layer Levels

Posted: Wed Jul 20, 2011 4:49 pm
by slembcke
Yes sorry. I misread your question, polygons and circles are solid, filled shapes that objects can't get stuck inside unless they get unsolvably pinned between two or more of them.

Re: Polygon Shapes in Tilemap Based Multi-Layer Levels

Posted: Thu Jul 28, 2011 4:47 pm
by moxie
What if they are unsolvably pinned between two or more? What techniques can I use to get them unstuck?

Re: Polygon Shapes in Tilemap Based Multi-Layer Levels

Posted: Thu Jul 28, 2011 9:36 pm
by slembcke
Not much you can do there except yank it out using an even bigger force. The forces from pinning something between two objects are never quite infinite, so it's always possible for another collision/constraint/force to get them out. It usually looks bad though.

Detecting that an object is being crushed is probably a much better solution.