// TODO: Need to add png parameter
        public HeightMapModel(Game game, Model m, Boolean t, Vector3 pos, float scale)
            : base(game, m, t, pos, scale, true)
        {
            Body = new Body(); // just a dummy. The PhysicObject uses its position to get the draw pos
            Skin = new CollisionSkin(null);

            HeightMapInfo heightMapInfo = this.model.Tag as HeightMapInfo;
            Array2D field = new Array2D(heightMapInfo.heights.GetUpperBound(0), heightMapInfo.heights.GetUpperBound(1));

            for (int x = 0; x < heightMapInfo.heights.GetUpperBound(0); x++)
            {
                for (int z = 0; z < heightMapInfo.heights.GetUpperBound(1); z++)
                {
                    field.SetAt(x, z, heightMapInfo.heights[x, z]);
                }
            }

            // move the body. The body (because its not connected to the collision
            // skin) is just a dummy. But the base class shoudl know where to
            // draw the model.
            Body.MoveTo(this.Position, Matrix.Identity);

            Skin.AddPrimitive(new Heightmap(field, 0f, 0f, 1, 1), new MaterialProperties(0.7f, 0.7f, 0.6f));

            PhysicsSystem.CurrentPhysicsSystem.CollisionSystem.AddCollisionSkin(this.Skin);
        }
        public HeightMapModel2(Game game, HeightMap info, Boolean t, Vector3 pos, float scale)
            : base(game , null, t, pos, scale)
        {
            // Game game, Model m, Boolean t, Vector3 pos, float scale, Boolean solid
            //Game game, HeightMap m, Boolean t, Vector3 pos, float scale

            this.Visible = false;
            Body = new Body();
            Skin = new CollisionSkin(null);
            //Skin.CollisionType = (int)CollisionTypes.Terrain;

            Array2D field = new Array2D(info.heights.GetUpperBound(0), info.heights.GetUpperBound(1));

            for (int x = 0; x < info.heights.GetUpperBound(0); x++)
            {
                for (int z = 0; z < info.heights.GetUpperBound(1); z++)
                {
                    field.SetAt(x, z, info.heights[x, z]);
                }
            }

            Body.MoveTo(new Vector3(info.heightmapPosition.X, info.heightmapPosition.Y, info.heightmapPosition.Y), Matrix.Identity);

            Skin.AddPrimitive(new Heightmap(field, info.heightmapPosition.X, info.heightmapPosition.Y, scale, scale), (int)MaterialTable.MaterialID.NotBouncyRough);

            Body.Immovable = true;

            PhysicsSystem.CurrentPhysicsSystem.CollisionSystem.AddCollisionSkin(Skin);
        }
        public HeightmapObject(Game game, Model model,Vector2 shift)
            : base(game, model)
        {
            body = new Body(); // just a dummy. The PhysicObject uses its position to get the draw pos
            collision = new CollisionSkin(null);

            HeightMapInfo heightMapInfo = model.Tag as HeightMapInfo;
            Array2D field = new Array2D(heightMapInfo.heights.GetUpperBound(0), heightMapInfo.heights.GetUpperBound(1));

            for (int x = 0; x < heightMapInfo.heights.GetUpperBound(0); x++)
            {
                for (int z = 0; z < heightMapInfo.heights.GetUpperBound(1); z++)
                {
                    field.SetAt(x,z,heightMapInfo.heights[x,z]);
                }
            }

            // move the body. The body (because its not connected to the collision
            // skin) is just a dummy. But the base class shoudl know where to
            // draw the model.
            body.MoveTo(new Vector3(shift.X,0,shift.Y), Matrix.Identity);

            collision.AddPrimitive(new Heightmap(field, shift.X, shift.Y, 1, 1), new MaterialProperties(0.7f,0.7f,0.6f));

            PhysicsSystem.CurrentPhysicsSystem.CollisionSystem.AddCollisionSkin(collision);
        }
        void Setup(HeightMapInfo heightMapInfo, Vector2 shift)
        {
            // A dummy. The physics object uses its position to get draw pos
            Body = new Body();

            CollisionSkin = new CollisionSkin(null);

            info = heightMapInfo;
            Array2D field = new Array2D(heightMapInfo.Heights.GetUpperBound(0), heightMapInfo.Heights.GetUpperBound(1));

            for (int x = 0; x < heightMapInfo.Heights.GetUpperBound(0); ++x)
            {
                for (int z = 0; z < heightMapInfo.Heights.GetUpperBound(1); ++z)
                {
                    field.SetAt(x, z, heightMapInfo.Heights[x, z]);
                }
            }

            // Move dummy body. The body isn't connected to the collision skin.
            // But the base class should know where to draw the model.
            Body.MoveTo(new Vector3(shift.X, 0, shift.Y), Matrix.Identity);

            CollisionSkin.AddPrimitive(new Heightmap(field, shift.X, shift.Y, 1, 1), new MaterialProperties(0.7f, 0.7f, 0.6f));

            PhysicsSystem.CurrentPhysicsSystem.CollisionSystem.AddCollisionSkin(CollisionSkin);
        }
        public HeightmapObject(Model model,Vector2 shift, Vector3 position)
            : base()
        {
            Body = new Body(); // just a dummy. The PhysicObject uses its position to get the draw pos
            Skin = new CollisionSkin(null);

            HeightMapInfo heightMapInfo = model.Tag as HeightMapInfo;
            Array2D field = new Array2D(heightMapInfo.heights.GetLength(0), heightMapInfo.heights.GetLength(1));

            for (int x = 0; x < heightMapInfo.heights.GetLength(0); x++)
            {
                for (int z = 0; z < heightMapInfo.heights.GetLength(1); z++)
                {
                    field.SetAt(x,z,heightMapInfo.heights[x,z]);
                }
            }

            // move the body. The body (because its not connected to the collision
            // skin) is just a dummy. But the base class shoudl know where to
            // draw the model.
            Body.MoveTo(new Vector3(shift.X,0,shift.Y), Matrix.Identity);

            Skin.AddPrimitive(new Heightmap(field, shift.X, shift.Y, heightMapInfo.terrainScale, heightMapInfo.terrainScale), new MaterialProperties(0.7f, 0.7f, 0.6f));

            PhysicsSystem.CurrentPhysicsSystem.CollisionSystem.AddCollisionSkin(Skin);
            CommonInit(position, new Vector3(1,1,1), model, false, 0);
        }
Beispiel #6
0
        public Array2D(Array2D arr)
        {
            if (arr == null || arr.Array == null)
                return;

            this.Array = new float[arr.Array.Length];
            this.nx = arr.Nx;
            this.nz = arr.Nz;

            Buffer.BlockCopy(arr.Array, 0, this.Array, 0, this.Array.Length*4);
        }
Beispiel #7
0
        /// <summary>
        /// Pass in an array of heights, and the axis that represents up
        /// Also the centre of the heightmap (assuming z is up), and the grid size
        /// </summary>
        /// <param name="heights"></param>
        /// <param name="x0"></param>
        /// <param name="z0"></param>
        /// <param name="dx"></param>
        /// <param name="dz"></param>
        public Heightmap(Array2D heights, float x0, float z0, float dx, float dz)
            : base((int)PrimitiveType.Heightmap)
        {
            mHeights = heights;
            this.x0 = x0;
            this.z0 = z0;
            this.dx = dx;
            this.dz = dz;

            this.xMin = x0 - (mHeights.Nx - 1) * 0.5f * dx;
            this.zMin = z0 - (mHeights.Nz - 1) * 0.5f * dz;
            this.xMax = x0 + (mHeights.Nx - 1) * 0.5f * dx;
            this.zMax = z0 + (mHeights.Nz - 1) * 0.5f * dz;

            // Save this, so we don't need to recalc every time
            this.yMin = mHeights.Min;
            this.yMax = mHeights.Max;
        }
Beispiel #8
0
        /// <summary>
        /// shifts all the elements...
        /// </summary>
        /// <param name="offsetX"></param>
        /// <param name="offsetY"></param>
        public void Shift(int offsetX, int offsetZ)
        {
            Array2D orig = new Array2D(this);

            for (int i = 0; i < this.nx; ++i)
            {
                for (int j = 0; j < this.nz; ++j)
                {
                    int i0 = (i + offsetX) % this.nx;
                    int j0 = (j + offsetZ) % this.nz;

                    this.SetAt(i0, j0, orig.GetAt(i, j));
                }
            }
        }
Beispiel #9
0
        /// <summary>
        /// Creates 2D array from mathematical function
        /// </summary>
        /// <param name="nx"></param>
        /// <param name="ny"></param>
        /// <param name="yScale"></param>
        /// <returns></returns>
        public static Array2D CreateArray(int nx, int nz, Function func)
        {
            Array2D arr = new Array2D(nx, nz);

            if(func == null)
                return arr;

            for (int xx = 0; xx < nx; xx++)
            {
                for (int zz = 0; zz < nz; zz++)
                {
                    arr.Array[xx + zz * nx] = func(xx, zz, arr);
                }

            }

            return arr;
        }
        void CreateCollisionMesh()
        {
            collision = new CollisionSkin(null);
            Array2D field = new Array2D(width, depth);
            for (int x = 0; x < width; x++)
            {
                for (int z = 0; z < depth; z++)
                {
                    field.SetAt(x, z, heightValues[x + z * width]);
                }
            }

            collision.AddPrimitive(new Heightmap(field, width*0.5f, depth*0.5f, 1, 1), new MaterialProperties(0.1f, 0.02f, 0.9f));
            scene.GetPhysicsEngine().CollisionSystem.AddCollisionSkin(collision);
        }
Beispiel #11
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 #12
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)
            {
            }
        }
Beispiel #13
0
        public IPhysicsObject CreateHeightmap(IHeightMap heightMapInfo, float scale, float shiftx, float shifty, float heightscale)
        {
            CollisionSkin collision = new CollisionSkin(null);
            Body _body = new Body();

            Array2D field = new Array2D(heightMapInfo.Size.X, heightMapInfo.Size.Y);

            for (int x = 0; x < field.Nx; x++)
            {
                for (int z = 0; z < field.Nz; z++)
                {
                    field.SetAt(x, z, heightscale * heightMapInfo.GetHeight(x,z));
                }
            }

            // move the body. The body (because its not connected to the collision
            // skin) is just a dummy. But the base class shoudl know where to
            // draw the model.
            _body.MoveTo(new Vector3(shiftx, 0, shifty), Matrix4.Identity);

            //collision.AddPrimitive(new Heightmap(field, shift.X, shift.Y, heightMapInfo.terrainScale, heightMapInfo.terrainScale), new MaterialProperties(0.7f, 0.7f, 0.6f));
            collision.AddPrimitive(new Heightmap(field, shiftx, shifty, scale, scale), new MaterialProperties(0.7f, 0.7f, 0.6f));

            _body.CollisionSkin = collision;
            _body.Immovable = true;
            _body.EnableBody();

            return new JigLibObject(_body);
        }