Terrain edge turned quads

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
perneky
Posts: 1
Joined: Mon Aug 23, 2010 10:05 am

Terrain edge turned quads

Post by perneky »

Hi all,

I'm experimenting with bepu physics for my little project and I'm really impressed. However I have a big problem with the terrain physics object.

For the terrain quads in bepu I can choose lowerLeftUpperRight or upperLeftLowerRight (as far as I know) for all the quads in the terrain. But for my terrain, some of my quads are lowerLeftUpperRight while others are upperLeftLowerRight. Is there any way to build a terrain like this without building it form triangles?

Thank you.
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Terrain edge turned quads

Post by Norbo »

Not at this time; the Terrain is mainly designed to be created from a heightmap in a very regular style with consistent graphics. For meshes which deviate from a pattern, the StaticTriangleGroup is recommended.
winterkewl
Posts: 3
Joined: Sun Feb 07, 2010 5:22 am

Re: Terrain edge turned quads

Post by winterkewl »

Related to this, is there a tutorial or somewhere in the docs that explains how to actually specify the height-map a Terrain object should use? It seems like in the Terrain Demo that the Heights field is what I need to pack the height-map data into.

Is it required that I create my own processor to turn my 2d texture into a 2 dimensional array?
Is it as simple as checking the color of each pixel and assigning a height value to each one in this 2d array?
Has anyone got a sample of this working that I may take a look at?

Also, is there any way to tile said heightmap?

Currently my project uses a grayscale image to create the actual renderable terrain but the engine allows this heightmap to be tiled infintely, is there anyway to do the same thing with the collision Terrain object?

Thanks this physics api is really great!
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Terrain edge turned quads

Post by Norbo »

It seems like in the Terrain Demo that the Heights field is what I need to pack the height-map data into.

Is it required that I create my own processor to turn my 2d texture into a 2 dimensional array?
Is it as simple as checking the color of each pixel and assigning a height value to each one in this 2d array?
Yes, the heights variable that is passed into the terrain.SetData method is what you'd need to fill up. You could use a processor to extract heights, but it's not required. The terrain doesn't care about where the individual heights come from, so you could get it from anywhere- the demo, of course, uses a simple function.

If you'd rather not mess with a custom processor, you could extract it using GetData on the texture.

Here's an example using a modified Terrain demo:

Code: Select all

//x and y, in terms of heightmaps, refer to their local x and y coordinates.  In world space, they correspond to x and z.
            //Setup the heights of the terrain.
            float xSpacing = 8f;
            float ySpacing = 8f;
            Texture2D sourceImage = game.Content.Load<Texture2D>("heightmap");
            int xLength = sourceImage.Width;
            int yLength = sourceImage.Height;

            Color[] colorData = new Color[xLength * yLength];
            sourceImage.GetData<Color>(colorData);

            var heights = new float[xLength, yLength];
            for (int i = 0; i < xLength; i++)
            {
                for (int j = 0; j < yLength; j++)
                {

                    Color color = colorData[j * xLength + i];
                    //Adding together the RGB channels has no particular meaning- it's just to create some difference in height.
                    //This could be done using non-Color textures, too.  For example, a grayscale image would just have one value to directly use.
                    heights[i, j] = (color.R + color.G + color.B) / 10f;

                }
            }
            //Create the terrain.
            var terrain = new Terrain(new Vector3(-xLength * xSpacing / 2, 0, -yLength * ySpacing / 2));

            terrain.SetData(heights, QuadFormats.LowerLeftUpperRight, xSpacing, ySpacing);
            Space.Add(terrain);
            terrain.UseFaceNormalWithinAngle = (float)Math.PI / 2; //This terrain shape is very flat, so the triangle normals can be safely used in every case.
            //Generally, it is fine to use the triangle normals, but if the terrain has extremely sharp cliffs and features you may notice
            //some 'stickiness' or other more severe problems.  Triangles can be set to use the normal contact normals always by setting the terrain.tryToUseTriangleNormals to false.
            //The benefit of using the triangles' normals is smoother behavior for sliding objects at the boundary between triangles.

            game.ModelDrawer.Add(terrain);
Also, is there any way to tile said heightmap?
Not automatically, but you should be able to re-use the heights array you created for the first terrain and create or move pooled terrains to appropriate locations based on the movement of the player. When the 'tile' is no longer necessary, you could remove it from the space to be re-used later.

The Terrain is one of many things being changed substantially in v0.15.0. In addition to becoming faster and more robust, it should be easier to use (a more controlled API) and provide some new tricks.
winterkewl
Posts: 3
Joined: Sun Feb 07, 2010 5:22 am

Re: Terrain edge turned quads

Post by winterkewl »

Thanks very much for the excellent reply and examples! I'll give this a go when I get home.
Post Reply