Best Way to Create 'Infinite Ground'?

General discussion about the R.U.B.E editor
Post Reply
therealcos
Posts: 16
Joined: Tue Aug 20, 2013 7:30 am

Best Way to Create 'Infinite Ground'?

Post by therealcos »

I have a sort of side-scrolling game in which a hero moves from left to right over a ground that I'd like to be dynamically generated and infinite.

To accomplish this, I have 2 large sensors that are off screen and follow the hero - one in back (to the left) and one in front (to the right). Right now the idea is to create a chain shape for the ground and use SetNextVertex to create the next parts of the ground when the hero moves. When the front sensor is not touching any part of the track, I'll create a new vertex on the chain so there is always ground for the hero to traverse on.

The problem is with the back sensor and destroying vertices in order to conserve memory. Ideally I'd like to destroy/remove a vertex from the chain once it hits the back sensor, but I can't find any way to do this. Is the only option here to destroy the whole chain shape and create a new one with each step? I also considered using individual edge shapes so that each new part of the ground is its own body, but then I read in the box2d manual that there are certain issues with "ghost" collisions with internal vertices when its done this way.

I'm sure this is a pretty common setup for games - what do you recommend is the best way to go about creating this infinite ground?
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: Best Way to Create 'Infinite Ground'?

Post by iforce2d »

Actually I think individual edge shapes would be a good solution for that. You can always set the ghost vertices yourself if you have smooth curving shapes and you need to avoid catching on internal vertices. As far as I can tell there is no specific function to do so, but the vertex0 and vertex3 related members of b2EdgeShape are all public: https://code.google.com/p/box2d/source/ ... Shape.h#57

The ghost vertices would need to be placed so that the line v0-v1 covers the previous edge, and v2-v3 covers the next edge. If the angle between adjacent edges is sharper than 90 degrees, you can leave out the ghost vertices there because they tend to give unwanted reactions.

I would first try just using individual edge shapes with no ghost vertices at all. As you can see in the video at the bottom of this topic, I found it extremely difficult to get internal edges to catch even without using ghost vertices.
Post Reply