public TriangleMeshObject(Game game, Model model, Matrix orientation, Vector3 position) : base(game, model) { body = new Body(); collision = new CollisionSkin(null); triangleMesh = new TriangleMesh(); List <Vector3> vertexList = new List <Vector3>(); List <TriangleVertexIndices> indexList = new List <TriangleVertexIndices>(); ExtractData(vertexList, indexList, model); triangleMesh.CreateMesh(vertexList, indexList, 4, 1.0f); collision.AddPrimitive(triangleMesh, new MaterialProperties(0.8f, 0.7f, 0.6f)); PhysicsSystem.CurrentPhysicsSystem.CollisionSystem.AddCollisionSkin(collision); // Transform collision.ApplyLocalTransform(new JigLibX.Math.Transform(position, orientation)); // we also need to move this dummy, so the object is *rendered* at the correct positiob body.MoveTo(position, orientation); }
public HeightmapObject(Game game, Model model, Vector2 shift) : base(game, model) { Body = new Body(); Collision = new CollisionSkin(null); var heightMapInfo = model.Tag as HeightMapInfo; var field = new Array2D(heightMapInfo.Heights.GetLength(0), heightMapInfo.Heights.GetLength(1)); for (var x = 0; x < heightMapInfo.Heights.GetLength(0); x++) { for (var z = 0; z < heightMapInfo.Heights.GetLength(1); z++) { field.SetAt(x, z, heightMapInfo.Heights[x, z]); } } Body.MoveTo(new Vector3(shift.X, 0, shift.Y), Matrix.Identity); Collision.AddPrimitive(new Heightmap(field, shift.X, shift.Y, heightMapInfo.TerrainScale, heightMapInfo.TerrainScale), new MaterialProperties(0.7f, 0.7f, 0.6f)); PhysicsSystem.CurrentPhysicsSystem.CollisionSystem.AddCollisionSkin(Collision); }
List <Vector3> heightVertices = new List <Vector3>(); // overlapping verts 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 void AddPrimitive(Primitive primitive, MaterialProperties materialProperties) { collision.AddPrimitive(primitive, materialProperties); }
public TriangleMeshObject(XModel model, Matrix orientation, Vector3 position) { body = new Body(); collision = new CollisionSkin(null); triangleMesh = new TriangleMesh(); foreach (ModelMesh mesh in model.Model.Meshes) { //declare an array of correct size to hold the models vertices VertexPositionNormalTexture[] vertices = new VertexPositionNormalTexture[mesh.VertexBuffer.SizeInBytes / VertexPositionNormalTexture.SizeInBytes]; //do again, debug this see which one is right?????????? vertices = new VertexPositionNormalTexture[mesh.VertexBuffer.SizeInBytes / mesh.MeshParts[0].VertexStride]; //get a copy of the vertices mesh.VertexBuffer.GetData <VertexPositionNormalTexture>(vertices); List <Vector3> verts = new List <Vector3>(); //Loop through each vertex and store in vector array for (int k = 0; k < vertices.Length; k++) { verts.Add(vertices[k].Position); //may have to transform position relative to something?????? } //Create a list of TriangleVertexIndices from the Index buffer taking into account it could be ints or shorts List <TriangleVertexIndices> indexList = new List <TriangleVertexIndices>(); if (mesh.IndexBuffer.IndexElementSize == IndexElementSize.SixteenBits) { short[] tempshorts = new short[mesh.IndexBuffer.SizeInBytes / sizeof(short)]; mesh.IndexBuffer.GetData <short>(tempshorts); for (int i = 0; i < tempshorts.Length / 3; i++) { indexList.Add(new TriangleVertexIndices((int)tempshorts[i * 3 + 2], (int)tempshorts[i * 3 + 1], (int)tempshorts[i * 3 + 0])); } } if (mesh.IndexBuffer.IndexElementSize == IndexElementSize.ThirtyTwoBits) { //create index array of correct size int[] tempInts = new int[mesh.IndexBuffer.SizeInBytes / sizeof(int)]; mesh.IndexBuffer.GetData <int>(tempInts); for (int i = 0; i < tempInts.Length / 3; i++) { indexList.Add(new TriangleVertexIndices(tempInts[i * 3 + 2], tempInts[i * 3 + 1], tempInts[i * 3 + 0])); } } triangleMesh.CreateMesh(verts, indexList, 4, 1.0f); collision.AddPrimitive(triangleMesh, new MaterialProperties(0.8f, 0.7f, 0.6f)); }//do next mesh /* * List<object> tagData = ((ModelData)model.Model.Tag).VertexData as List<object>; * List<Vector3> vertices = (List<Vector3>)tagData[0]; * List<int> indices = (List<int>)tagData[1]; * * Vector3[] verts = new Vector3[vertices.Count]; * int[] inds = new int[indices.Count]; * * for (int i = 0; i < vertices.Count; i++) * verts[i] = vertices[i]; * * for (int i = 0; i < indices.Count; i++) * inds[i] = indices[i]; * * List<Vector3> vertexList = new List<Vector3>(); * List<TriangleVertexIndices> indexList = new List<TriangleVertexIndices>(); * * for (int i = 0; i < inds.Length / 3; i++) * { * indexList.Add(new TriangleVertexIndices(indices[i * 3 + 2], indices[i * 3 + 1], indices[i * 3 + 0])); * } * * for (int i = 0; i < verts.Length; i++) * { * vertexList.Add(vertices[i]); * } * * triangleMesh.CreateMesh(vertexList, indexList, 4, 1.0f); * * collision.AddPrimitive(triangleMesh, 1, new MaterialProperties(0.8f, 0.7f, 0.6f)); */ PhysicsSystem.CurrentPhysicsSystem.CollisionSystem.AddCollisionSkin(collision); body.CollisionSkin = collision; body.MoveTo(position, orientation); }
public XBoneMapObject(Vector3 position, ref XModel model) { //create a list to hold the collision primitives List <Primitive> prims = new List <Primitive>(); //parse for collision bones foreach (ModelBone bone in model.Model.Bones) { string[] keypairs = bone.Name.ToLower().Split('_'); //if checks for valid naming convention on collision bones if ((keypairs.Length == 2) && ((keypairs[0] == "sphere") || (keypairs[0] == "box") || (keypairs[0] == "capsule"))) { //determine object number int objectnum; if ((keypairs[1] != "") || (keypairs[1] != null)) { objectnum = int.Parse(keypairs[1]); } else { objectnum = 0; } //decompose bone transforms to components Vector3 pos; Vector3 scale; Quaternion qrot; bone.Transform.Decompose(out scale, out qrot, out pos); Matrix rot = Matrix.CreateFromQuaternion(qrot); //create collision primitive objects and add to list switch (keypairs[0]) { case ("sphere"): JigLibX.Geometry.Sphere sph = new JigLibX.Geometry.Sphere(pos, scale.X); prims.Add(sph); break; case ("box"): Box box = new Box(pos, rot, scale); prims.Add(box); break; case ("capsule"): break; } } } body = new Body(); collision = new CollisionSkin(body); if (prims.Count > 0) { foreach (Primitive prim in prims) { //TODO: Add ability to specify physics material type in art editor somehow collision.AddPrimitive(prim, (int)JigLibX.Collision.MaterialTable.MaterialID.NormalSmooth); } } else {//no collision prims detected from XSI so create a default one here using the mesh bounding spheres foreach (ModelMesh mesh in model.Model.Meshes) { //collision.AddPrimitive(new JigLibX.Geometry.Sphere(mesh.BoundingSphere.Center, mesh.BoundingSphere.Radius), new MaterialProperties(0.8f, 0.8f, 0.7f)); collision.AddPrimitive(new JigLibX.Geometry.Box(position, Matrix.Identity, new Vector3(mesh.BoundingSphere.Radius / 2f)), (int)JigLibX.Collision.MaterialTable.MaterialID.NormalRough); } } body.CollisionSkin = this.collision; Vector3 com = SetMass(1.0f); body.MoveTo(position, Matrix.Identity); collision.ApplyLocalTransform(new Transform(-com, Matrix.Identity)); body.EnableBody(); }
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 Planet(Vector3 center, Vector3 radius, double maxDeviation, double radianMeshSize, int subdivides, GraphicsDevice g, Texture2D texture) : base() { this.texture = texture; Body = new Body(); Skin = new CollisionSkin(null); for (double i = 0; i < MathHelper.TwoPi - JigLibX.Math.JiggleMath.Epsilon; i += radianMeshSize) { for (double j = 0; j < MathHelper.TwoPi - JigLibX.Math.JiggleMath.Epsilon; j += radianMeshSize) { //TriangleMesh tm = CreateSphericalMeshSegment(i, i + radianMeshSize, j, j + radianMeshSize, trianglesPerMesh, maxDeviation); TriangleMesh tm = new TriangleMesh(); List <Vector3> vl = new List <Vector3>(); // Polar -> Cartesion in 3-Spcae // X = R * Sin(Θ) * Cos(ϕ) // Y = R * Sin(Θ) * Sin(ϕ) // Z = R * Cos(Θ) vl.Add(SphericalToCartesian(i, j, radius)); vl.Add(SphericalToCartesian(i + radianMeshSize, j, radius)); vl.Add(SphericalToCartesian(i + radianMeshSize, j + radianMeshSize, radius)); vl.Add(SphericalToCartesian(i, j + radianMeshSize, radius)); List <TriangleVertexIndices> tvi = new List <TriangleVertexIndices>(); tvi.Add(new TriangleVertexIndices(0, 1, 2)); tvi.Add(new TriangleVertexIndices(1, 3, 2)); VertexPositionNormalTexture[] v = new VertexPositionNormalTexture[4]; Vector3 tmp = SphericalToCartesian(i, j, Vector3.One); v[0].Position = tmp * radius; v[0].Normal = Vector3.Zero; v[0].TextureCoordinate.X = tmp.X; v[0].TextureCoordinate.Y = tmp.Y; tmp = SphericalToCartesian(i + radianMeshSize, j, Vector3.One); v[1].Position = tmp * radius; v[1].Normal = Vector3.Zero; v[1].TextureCoordinate.X = tmp.X; v[1].TextureCoordinate.Y = tmp.Y; tmp = SphericalToCartesian(i + radianMeshSize, j + radianMeshSize, Vector3.One); v[2].Position = tmp * radius; v[2].Normal = Vector3.Zero; v[2].TextureCoordinate.X = tmp.X; v[2].TextureCoordinate.Y = tmp.Y; tmp = SphericalToCartesian(i, j + radianMeshSize, Vector3.One); v[3].Position = tmp * radius; v[3].Normal = Vector3.Zero; v[3].TextureCoordinate.X = tmp.X; v[3].TextureCoordinate.Y = tmp.Y; verts.Add(v); indices.Add(new int[] { 0, 1, 2, 1, 3, 2 }); SetNormalOfTriangleAtIndices(0, 1, 2, verts.Count - 1); SetNormalOfTriangleAtIndices(1, 3, 2, verts.Count - 1); tm.CreateMesh(vl, tvi, 1, 1f); // Last two parameters are listed as not used on the octree triangleMeshes.Add(tm); Skin.AddPrimitive(tm, (int)MaterialTable.MaterialID.NormalRough); } } List <Vector3> vertexList = new List <Vector3>(); List <TriangleVertexIndices> indexList = new List <TriangleVertexIndices>(); Effect = new BasicEffect(g); //ExtractData(vertexList, indexList, model); //triangleMesh.CreateMesh(vertexList,indexList, 4, 1.0f); //Skin.AddPrimitive(triangleMesh, new MaterialProperties(0.8f, 0.7f, 0.6f)); PhysicsSystem.CurrentPhysicsSystem.CollisionSystem.AddCollisionSkin(Skin); // Transform Skin.ApplyLocalTransform(new JigLibX.Math.Transform(center, Matrix.Identity)); // we also need to move this dummy, so the object is *rendered* at the correct positiob Body.MoveTo(center, Matrix.Identity); CommonInit(center, new Vector3(1, 1, 1), null, false, -1); }