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); }
// 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 TriangleMeshActor(Game game, Vector3 position, float scale, Texture2D heightMap, float[,] heightData) : base(game) { this.position = position; this.scale = new Vector3(1,1,1); _body = new Body(); _body.MoveTo(position, Matrix.Identity); Array2D field = new Array2D(heightData.GetUpperBound(0), heightData.GetUpperBound(1)); int upperZ = heightData.GetUpperBound(1); for (int x = 0; x < heightData.GetUpperBound(0); x++) { for (int z = 0; z < upperZ; z++) { field.SetAt(x, z, heightData[x, upperZ - 1 - z]); } } _skin = new CollisionSkin(null); float X = heightMap.Width / 2 * scale; float Z = heightMap.Height / 2 * scale; _skin.AddPrimitive(new Heightmap(field, X, -Z, scale, scale), new MaterialProperties(0.7f, 0.7f, 0.6f)); _skin.ExternalData = this; PhysicsSystem.CurrentPhysicsSystem.CollisionSystem.AddCollisionSkin(_skin); }
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); }
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); }
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); } }
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) { } }
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); }