Beispiel #1
0
        public Terrain( Vector3 posCenter,Vector3 size, int cellsX, int cellsZ, GraphicsDevice g, Texture2D tex)
            : base()
        {
            numVertsX = cellsX + 1;
            numVertsZ = cellsZ + 1;
            numVerts = numVertsX * numVertsZ;
            numTriX = cellsX * 2;
            numTriZ = cellsZ;
            numTris = numTriX * numTriZ;
            verts = new VertexPositionNormalTexture[numVerts];
            int numIndices = numTris * 3;
            indices = new int[numIndices];
            float cellSizeX = (float)size.X / cellsX;
            float cellSizeZ = (float)size.Z / cellsZ;
            texture = tex;

            Random r = new Random();
            Random r2 = new Random(DateTime.Now.Millisecond);
            double targetHeight = 0;
            double currentHeight = 0;
            // Fill in the vertices
            int count = 0;

            // distribute height nodes across the map.
            // for each vert, calculate the height by summing (node height / distance)
            int numHeightNodes = 10;
            heightNodes = new Vector3[numHeightNodes];
            for (int i = 0; i < numHeightNodes; i++)
            {
                heightNodes[i] = new Vector3((float)((-size.X / 2) + (r.NextDouble() * size.X)),
                                                (float)(r.NextDouble() * 0),
                                                (float)((-size.Z / 2) + (r.NextDouble() * size.Z)));
            }

            bool stretchTexture=false;
            float textureTileCount=10000;

            float worldZPosition = - (size.Z / 2);
            for (int z = 0; z < numVertsZ; z++)
            {
                float worldXPosition = - (size.X / 2);
                for (int x = 0; x < numVertsX; x++)
                {

                    if (count % numVertsX == 0)
                        targetHeight = r2.NextDouble();

                    //currentHeight += (targetHeight - currentHeight) * .009f;

                    float height = GetNodeBasedHeight(worldXPosition, worldZPosition);
                    height = 0;
                    verts[count].Position = new Vector3(worldXPosition, height, worldZPosition);
                    verts[count].Normal = Vector3.Zero;

                    if (stretchTexture)
                    {
                        verts[count].TextureCoordinate.X = (float)x / (numVertsX - 1);
                        verts[count].TextureCoordinate.Y = (float)z / (numVertsZ - 1);
                    }
                    else
                    {
                        verts[count].TextureCoordinate.X = ((float)x / (numVertsX - 1))*textureTileCount;
                        verts[count].TextureCoordinate.Y = ((float)z / (numVertsZ - 1))*textureTileCount;
                    }

                    count++;

                    // Advance in x
                    worldXPosition += cellSizeX;
                }

                currentHeight = 0;
                // Advance in z
                worldZPosition += cellSizeZ;
            }

            int index = 0;
            int startVertex = 0;
            for (int cellZ = 0; cellZ < cellsZ; cellZ++)
            {
                for (int cellX = 0; cellX < cellsX; cellX++)
                {
                    indices[index] = startVertex + 0;
                    indices[index + 1] = startVertex + 1;
                    indices[index + 2] = startVertex + numVertsX;
                    SetNormalOfTriangleAtIndices(indices[index], indices[index + 1], indices[index + 2]);
                    meshIndices.Add(new TriangleVertexIndices(index, index + 1, index + 2));
                    meshVertices.Add(verts[indices[index]].Position);
                    meshVertices.Add(verts[indices[index+1]].Position);
                    meshVertices.Add(verts[indices[index+2]].Position);
                    index += 3;

                    indices[index] = startVertex + 1;
                    indices[index + 1] = startVertex + numVertsX + 1;
                    indices[index + 2] = startVertex + numVertsX;
                    SetNormalOfTriangleAtIndices(indices[index], indices[index + 1], indices[index + 2]);
                    meshIndices.Add(new TriangleVertexIndices(index, index + 1, index + 2));
                    meshVertices.Add(verts[indices[index]].Position);
                    meshVertices.Add(verts[indices[index + 1]].Position);
                    meshVertices.Add(verts[indices[index + 2]].Position);
                    index += 3;

                    startVertex++;
                }
                startVertex++;
            }

            try
            {
                Effect = new BasicEffect(g);
                mesh = new TriangleMesh();
                //mesh.CreateMesh(meshVertices, meshIndices, 2, cellSizeX);
            }
            catch (Exception E)
            {
                System.Diagnostics.Debug.WriteLine(E.StackTrace);
            }

            this.Body = new Body();
            Skin = new CollisionSkin(Body);
            Body.CollisionSkin = Skin;
            Body.ExternalData = this;
            float heightf = 0;
            try
            {
            Array2D field = new Array2D(numVertsX, numVertsZ);

            int i = 0;
            for (int c = 0; c < verts.Length; c++)
            {
                int x = c / numVertsX;
                int z = c % numVertsX;

                if (i >= verts.Length)
                    i = (i % verts.Length)+1;
                heightf = verts[i].Position.Y + posCenter.Y; // works
                //heightf = verts[i].Position.Y;
                i += numVertsX;

                field.SetAt(x,z, heightf);
            }

               // Body.MoveTo(Position, Matrix.Identity);

                Heightmap hm = new Heightmap(field,
                                                    (-size.X / 2) / (cellsX+0) + cellSizeX/2,
                                                    (-size.Z / 2) / (cellsZ + 0) + cellSizeZ/2,
                                                    size.X / (cellsX+0),
                                                    size.Z / (cellsZ+0));
                Skin.AddPrimitive(hm, new MaterialProperties(0.7f, 0.7f, 0.6f));
                //Skin.AddPrimitive(GetMesh(), new MaterialProperties(0.7f, 0.7f, 0.6f));
                //VertexPositionColor[] wireFrame = Skin.GetLocalSkinWireframe(); // 1200 across before Z changes to from -7.5/-7.35 to -7.35/-7.2

                PhysicsSystem.CurrentPhysicsSystem.CollisionSystem.AddCollisionSkin(Skin);
                CommonInit(posCenter, new Vector3(0,0,0), null, false, 0);
            }
            catch(Exception E)
            {
                System.Diagnostics.Debug.WriteLine(E.StackTrace);
            }
        }
Beispiel #2
0
        public Terrain( Vector3 posCenter,Vector3 size, int cellsX, int cellsZ, GraphicsDevice g, Texture2D tex)
            : base()
        {
            numVertsX = cellsX + 1;
            numVertsZ = cellsZ + 1;
            numVerts = numVertsX * numVertsZ;
            numTriX = cellsX * 2;
            numTriZ = cellsZ;
            numTris = numTriX * numTriZ;
            verts = new VertexPositionNormalTexture[numVerts];
            int numIndices = numTris * 3;
            indices = new int[numIndices];
            float cellSizeX = (float)size.X / cellsX;
            float cellSizeZ = (float)size.Z / cellsZ;
            texture = tex;

            Random r = new Random();
            Random r2 = new Random(DateTime.Now.Millisecond);
            double targetHeight = 0;
            double currentHeight = 0;
            // Fill in the vertices
            int count = 0;
            //float edgeHeigh = 0;
            //float worldZPosition = posCenter.Z - (size.Z / 2);
            float worldZPosition = - (size.Z / 2);
            float height;
            float stair = 0;
            float diff = .5f;
            for (int z = 0; z < numVertsZ; z++)
            {
                //float worldXPosition = posCenter.X - (size.X / 2);
                float worldXPosition = - (size.X / 2);
                for (int x = 0; x < numVertsX; x++)
                {

                    if (count % numVertsX == 0)
                        targetHeight = r2.NextDouble();
                    //targetHeight += Math.Abs(worldZPosition);// +worldXPosition * 1.0f;

                    currentHeight += (targetHeight - currentHeight) * .009f;
                    //if(x!=0)
                    //currentHeight = (targetHeight) / ((float)x / (float)(numVertsX+1));
                    //height = (float)((r.NextDouble() + currentHeight) * size.Y);

                    //height = 1;

                    //stair += diff;
                    //if (z + x == 17)
                        //stair += 10;

                    //height += stair;
                    verts[count].Position = new Vector3(worldXPosition,(float)currentHeight, worldZPosition);
                    verts[count].Normal = Vector3.Zero;
                    verts[count].TextureCoordinate.X = (float)x / (numVertsX - 1);
                    verts[count].TextureCoordinate.Y = (float)z / (numVertsZ - 1);

                    //if (z + x == 17)
                        //stair -= 10;
                    count++;

                    // Advance in x
                    worldXPosition += cellSizeX;
                }

                currentHeight = 0;
                // Advance in z
                worldZPosition += cellSizeZ;
                //diff *= -1;
                //if (diff < 1)
                   // stair += numVertsX * diff;
            }

            int index = 0;
            int startVertex = 0;
            for (int cellZ = 0; cellZ < cellsZ; cellZ++)
            {
                for (int cellX = 0; cellX < cellsX; cellX++)
                {
                    indices[index] = startVertex + 0;
                    indices[index + 1] = startVertex + 1;
                    indices[index + 2] = startVertex + numVertsX;
                    SetNormalOfTriangleAtIndices(indices[index], indices[index + 1], indices[index + 2]);
                    meshIndices.Add(new TriangleVertexIndices(index, index + 1, index + 2));
                    meshVertices.Add(verts[indices[index]].Position);
                    meshVertices.Add(verts[indices[index+1]].Position);
                    meshVertices.Add(verts[indices[index+2]].Position);
                    index += 3;

                    indices[index] = startVertex + 1;
                    indices[index + 1] = startVertex + numVertsX + 1;
                    indices[index + 2] = startVertex + numVertsX;
                    SetNormalOfTriangleAtIndices(indices[index], indices[index + 1], indices[index + 2]);
                    meshIndices.Add(new TriangleVertexIndices(index, index + 1, index + 2));
                    meshVertices.Add(verts[indices[index]].Position);
                    meshVertices.Add(verts[indices[index + 1]].Position);
                    meshVertices.Add(verts[indices[index + 2]].Position);
                    index += 3;

                    startVertex++;
                }
                startVertex++;
            }

            try
            {
                Effect = new BasicEffect(g);
                mesh = new TriangleMesh();
                //mesh.CreateMesh(meshVertices, meshIndices, 2, cellSizeX);
            }
            catch (Exception E)
            {
            }

            this.Body = new Body();
            Skin = new CollisionSkin(Body);
            Body.CollisionSkin = Skin;
            Body.ExternalData = this;
            float heightf = 0;
            try
            {
            Array2D field = new Array2D(numVertsX, numVertsZ);

            int i = 0;
            for (int c = 0; c < verts.Length; c++)
            {
                int x = c / numVertsX;
                int z = c % numVertsX;

                if (i >= verts.Length)
                    i = (i % verts.Length)+1;
                heightf = verts[i].Position.Y + posCenter.Y;
                //heightf = verts[i].Position.Y;
                i += numVertsX;

                field.SetAt(x,z, heightf);
            }

               // Body.MoveTo(Position, Matrix.Identity);

                Heightmap hm = new Heightmap(field,
                                                    (-size.X / 2) / (cellsX+0) + cellSizeX/2,
                                                    (-size.Z / 2) / (cellsZ + 0) + cellSizeZ/2,
                                                    size.X / (cellsX+0),
                                                    size.Z / (cellsZ+0));
                Skin.AddPrimitive(hm, new MaterialProperties(0.7f, 0.7f, 0.6f));
                //Skin.AddPrimitive(GetMesh(), new MaterialProperties(0.7f, 0.7f, 0.6f));
                //VertexPositionColor[] wireFrame = Skin.GetLocalSkinWireframe(); // 1200 across before Z changes to from -7.5/-7.35 to -7.35/-7.2

                PhysicsSystem.CurrentPhysicsSystem.CollisionSystem.AddCollisionSkin(Skin);
                CommonInit(posCenter, new Vector3(0,0,0), null, false);
            }
            catch(Exception E)
            {
            }
        }