Using TextureToVerticies sample to create level...

Topics: Developer Forum
Apr 14, 2011 at 4:12 PM

I have been having fun figuring out farseer.  I have hacked up the Ragdoll sample pretty good to get the ragdoll doing things and moving around on a level that I took from the Car sample in the advance demos) but this level is a line really that the ragoll doesn't fall thru.  I now want to make it more "platformer" like.  Camera follows the ragdoll where I drag him (Thats cool) but the line as a level ground is hard to see and I would like something closer to tile based graphics.  I am not sure what the best approach is, some ideas I have are:

1) Create a static bodies (like the obstacles in the intial ragdoll demo) and make a whole level out of those pieced together.

2) Use something I saw in the TextureToVerticies demo to build a giant texture (or several big ones) that make up the level (ground other objects etc.) and take whats done in the texture to verticies sample to create a level out of these large textures.

3) No three, the only ideas I have really are 1 & 2, so maybe 3 is an option I have thought of yet?  I look forward to ideas on where to go next...

 

Thanks,

Shane

p.s. if I am not in the right forum or the subject is bad here (not sure what I want is called) then let me know this so I can make the proper edits too.

 

Apr 14, 2011 at 6:19 PM

Depending on what you want your levels to look like you'll have to consider if a tile based system is right for your game.  For instance, if you need rising edges, small curves, or various angled ramps... it may be trickier to do with a tile based approach. If you don't care about square edged-blocky like levels then tiles would probably be fine.

 

I actually made a tile based level for my game originally but then I realized that it wouldn't fulfill what I needed.  So I've written a prototype tile based system and a vertex based terrain.  I start with my level data in an image.

 

In either case, I made fixtures for my terrain as polygons. I did this in the tile based system (as opposed to making them all squares) because I wanted 45 degree ramps.  When the level is created in the game, I would process the data by trying to make as few fixtures as possible... that means I would combine the same tiles that lay next to each other into one single fixture. This gives you a collection of fixtures, which you can use their position and sizes to render your tiles. 

 

My current implementation takes my level data and uses the marching square algorithm on it. (see the destructable terrain example) It then decomposes it (which essentially breaks down the data to produce polygons).  I make a fixture for each polygon.

Those polygons can then be broken down into triangles.  With the triangles, you can throw them in a vertex buffer for rendering.  See my question for further details on what I did http://farseerphysics.codeplex.com/discussions/250043 . This results in a collection of fixtures and vertexbuffer(s).  If you know/learn how to texture polygons you can then have a more diverse Terrain.  It will be a bit more complex than the tile approach, but its more versatile.  Mine produces a level that is similar to a Worms style terrain.

 

I'd also recommend that if your terrain isn't changing, you can just have one body and add all your static terrain fixtures to it.

Apr 14, 2011 at 7:35 PM

Thanks for the response I will chew on that other discussion though words like triangulator and that are running at me and im like yikes.  I do like the idea of being Worms style terrain.  Tile map was a bad use of words as indeed I would like slopes etc.

 

I am wondering if i could just build a level editor that lets me build polygons around terrain graphics and use that data to build the ground that the player cannot fall through or things are colliding with.  This just became complex but I think its easily understood.  I will keep chewing on what you sent here.  The destructable terrain example, thats not the one with the letters you can bash into and explode apart is it?  Will research but thanks for the input!

 

Apr 14, 2011 at 7:41 PM

Yep couldn't find the destructible terrain stuff.  What I thought it was the breakable bodies and explosions.  I guess i am not sure where this destructible terrain sample is but am now quite curious.

Apr 14, 2011 at 8:05 PM

Actually found it int he Testbed project...lots to go over its not drawing the stuff like I would expect so I am now digging through this. (never even knew the testbed thing existed.  Great projects thanks!

 

Apr 14, 2011 at 8:38 PM

OKay been tinkering with it.  It seems I have to figure out how to attach a graphic (that can be used also to form or shape my terrain) as an image though in some sort of draw method of my main program.  The coordinates or how to do that I am not overly sure...

These testbed samples so far though are quite intresting.

Apr 15, 2011 at 2:03 PM

There is a lot of examples in the testbed, it's a good place to learn what the engine is capable of producing.

 

If I am understanding you correctly, you want to use your terrain data (in texture form) as the rendering texture too.  This may be difficult, but not impossible, because the physics coordinate system is different from the world coordinate system.  If you're not aware, the physics coordinates are much smaller than xna's world coordinates.  So there is a scaling issue that may make it hard to get them to match up...though I've seen posts on the forum about people doing that, so I know you can find something out there.

 

Doing that can cause difficulty because you can't just take the vertices from your fixtures and set them as-is into vertex buffers. It wasn't covered in the other forum post, but you'll have to convert the fixture vertices to world space before you save them in a vertex buffer.   I use different data to create my fixtures and to render my terrain.  My terrain comes from a black and white (or maybe its black and transparent (i can't remember)) texture that is filled in with black representing solid ground and white being empty.  http://img851.imageshack.us/i/mapsn.png/   I then use other textures to render my polygons, so think of a repeating texture of rocks being stamped across my terrain.  That may not be ideal for your game.

 

I fear that my explanation may send you off into a deep hole if you're not familiar with many of these words or concepts I'm throwing out here.  You may want to take things in smaller steps rather than just following my approach.  Maybe you'd come up with something better once you learn more about the physics engine.

 

If you are still considering what I do, then I'd recommend you implement these things one at a time and then move on because they build off each other.

Forgive me if you already know how to do these things...

 

1. Render a triangle to the screen http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series1/The_first_triangle.php

2. Understand world space http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series1/World_space.php

3. Understand indices http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series1/Indices.php

4. Texturing polygons http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series3/Textured_triangle.php

5. Use Vertex and Index Buffers http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series1/VertexBuffer_and_IndexBuffer.php and texturing them http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series3/Triangle_strip.php

 

Doing those tutorials will teach you how to render textured polygons to the screen.  As a learning experience you can then work backwards, by converting those vertices to physics space and making fixtures for them.  Doing all of this should teach you how my rendering system works.  You can decide at that point if you want to dive into the decomposition and marching squares examples.

Apr 15, 2011 at 2:30 PM

Thanks again for the response.  Your way of stamping the textures actually works for me, I only went with the other possibility cause to me it seemed a simple step from loading the texture as terrain data and then just drawing it.  I see now that is not the case.  In my head it was a simple stamp of a the large texture at the same xy i stamp the terrain data.  So I guess I have no choice but to dive deep into the graphics renderers.  I remember all these vocabulary words from my brief and unsuccessful jaunt into 3d graphics, but now I guess its time to get serious about it.  Thanks for the links I will chew on these.   I am worried once I get this all up the camera I have (That came as default with the SimpleSamples examples) won't cut it for the platformer like game I am thinking of, though won't know till I get everything else in place and get it all stood up and working.