Ejemplo n.º 1
0
		protected override void LoadPhysics(Scene scene)
		{
			int w = 25;
			int h = 25;

			float hw = w / 2.0f;
			float hh = h / 2.0f;

			Vector3 p = new Vector3(0, 20, 0);

			// Create a Grid of Points
			int vertices, indices;

			ClothMesh clothMesh;
			{
				var grid = VertexGrid.CreateGrid(w, h);

				vertices = grid.Points.Length;
				indices = grid.Indices.Length;

				ClothMeshDescription clothMeshDesc = new ClothMeshDescription();
				clothMeshDesc.AllocateVertices<Vector3>(vertices);
				clothMeshDesc.AllocateTriangles<int>(indices / 3);

				clothMeshDesc.VertexCount = vertices;
				clothMeshDesc.TriangleCount = indices / 3;

				clothMeshDesc.VerticesStream.SetData(grid.Points);
				clothMeshDesc.TriangleStream.SetData(grid.Indices);

				// We are using 32 bit integers for our indices, so make sure the 16 bit flag is removed.
				// 32 bits are the default, so this isn't technically needed, but it's good to show in a sample
				clothMeshDesc.Flags &= ~MeshFlag.Indices16Bit;
				clothMeshDesc.Flags |= (MeshFlag)((int)clothMeshDesc.Flags | (int)ClothMeshFlag.Tearable);

				// Write the cooked data to memory
				using (var memoryStream = new MemoryStream())
				{
					Cooking.InitializeCooking();
					Cooking.CookClothMesh(clothMeshDesc, memoryStream);
					Cooking.CloseCooking();

					// Need to reset the position of the stream to the beginning
					memoryStream.Position = 0;

					clothMesh = Engine.Core.CreateClothMesh(memoryStream);
				}
			}

			//

			int j = vertices * 2;
			int k = indices * 3;

			var clothDesc = new ClothDescription()
			{
				ClothMesh = clothMesh,
				GlobalPose =
					Matrix.Translation(-hw, 0, -hh) *
					Matrix.Translation(p),
				Flags = ClothFlag.Gravity | ClothFlag.Bending | ClothFlag.CollisionTwoway | ClothFlag.Visualization | ClothFlag.Tearable,
				BendingStiffness = 0.1f,
				TearFactor = 1.5f
			};
			clothDesc.MeshData.AllocatePositions<Vector3>(j);
			clothDesc.MeshData.AllocateIndices<int>(k);
			clothDesc.MeshData.AllocateNormals<Vector3>(j);

			clothDesc.MeshData.MaximumVertices = j;
			clothDesc.MeshData.MaximumIndices = k;

			clothDesc.MeshData.NumberOfVertices = vertices;
			clothDesc.MeshData.NumberOfIndices = indices;

			_cloth = scene.CreateCloth(clothDesc);

			//

			// Four corner boxes to hold it in place
			var positions = new[]
			{
				new Vector3(0, 0, -hh), // Back
				new Vector3(0, 0, hh), // Front
				new Vector3(-hw, 0, 0), // Left
				new Vector3(hw, 0, 0), // Right
			};

			var sizes = new[]
			{
				new Vector3(w, 1, 1), // Back
				new Vector3(w, 1, 1), // Front
				new Vector3(1, 1, h), // Left
				new Vector3(1, 1, h), //Right
			};

			for (int i = 0; i < 4; i++)
			{
				var actorDesc = new ActorDescription()
				{
					GlobalPose = Matrix.Translation(positions[i] + p),
					Shapes = { new BoxShapeDescription(sizes[i]) }
				};

				var actor = scene.CreateActor(actorDesc);

				_cloth.AttachToShape(actor.Shapes.First(), (ClothAttachmentFlag)0);
			}

			//

			// Something to drop on it
			{
				var actorDesc = new ActorDescription()
				{
					GlobalPose = Matrix.Translation(0, 100, 0),
					Shapes = { new SphereShapeDescription(2) },
					BodyDescription = new BodyDescription(50)
				};

				var actor = scene.CreateActor(actorDesc);
			}
		}
Ejemplo n.º 2
0
        protected override void LoadPhysics(Scene scene)
        {
            int w = 25;
            int h = 25;

            float hw = w / 2.0f;
            float hh = h / 2.0f;

            Vector3 p = new Vector3(0, 20, 0);

            // Create a Grid of Points
            int vertices, indices;

            ClothMesh clothMesh;
            {
                var grid = VertexGrid.CreateGrid(w, h);

                vertices = grid.Points.Length;
                indices  = grid.Indices.Length;

                ClothMeshDescription clothMeshDesc = new ClothMeshDescription();
                clothMeshDesc.AllocateVertices <Vector3>(vertices);
                clothMeshDesc.AllocateTriangles <int>(indices / 3);

                clothMeshDesc.VertexCount   = vertices;
                clothMeshDesc.TriangleCount = indices / 3;

                clothMeshDesc.VerticesStream.SetData(grid.Points);
                clothMeshDesc.TriangleStream.SetData(grid.Indices);

                // We are using 32 bit integers for our indices, so make sure the 16 bit flag is removed.
                // 32 bits are the default, so this isn't technically needed, but it's good to show in a sample
                clothMeshDesc.Flags &= ~MeshFlag.Indices16Bit;
                clothMeshDesc.Flags |= (MeshFlag)((int)clothMeshDesc.Flags | (int)ClothMeshFlag.Tearable);

                // Write the cooked data to memory
                using (var memoryStream = new MemoryStream())
                {
                    Cooking.InitializeCooking();
                    Cooking.CookClothMesh(clothMeshDesc, memoryStream);
                    Cooking.CloseCooking();

                    // Need to reset the position of the stream to the beginning
                    memoryStream.Position = 0;

                    clothMesh = Engine.Core.CreateClothMesh(memoryStream);
                }
            }

            //

            int j = vertices * 2;
            int k = indices * 3;

            var clothDesc = new ClothDescription()
            {
                ClothMesh  = clothMesh,
                GlobalPose =
                    Matrix.Translation(-hw, 0, -hh) *
                    Matrix.Translation(p),
                Flags            = ClothFlag.Gravity | ClothFlag.Bending | ClothFlag.CollisionTwoway | ClothFlag.Visualization | ClothFlag.Tearable,
                BendingStiffness = 0.1f,
                TearFactor       = 1.5f
            };

            clothDesc.MeshData.AllocatePositions <Vector3>(j);
            clothDesc.MeshData.AllocateIndices <int>(k);
            clothDesc.MeshData.AllocateNormals <Vector3>(j);

            clothDesc.MeshData.MaximumVertices = j;
            clothDesc.MeshData.MaximumIndices  = k;

            clothDesc.MeshData.NumberOfVertices = vertices;
            clothDesc.MeshData.NumberOfIndices  = indices;

            _cloth = scene.CreateCloth(clothDesc);

            //

            // Four corner boxes to hold it in place
            var positions = new[]
            {
                new Vector3(0, 0, -hh),                // Back
                new Vector3(0, 0, hh),                 // Front
                new Vector3(-hw, 0, 0),                // Left
                new Vector3(hw, 0, 0),                 // Right
            };

            var sizes = new[]
            {
                new Vector3(w, 1, 1),                 // Back
                new Vector3(w, 1, 1),                 // Front
                new Vector3(1, 1, h),                 // Left
                new Vector3(1, 1, h),                 //Right
            };

            for (int i = 0; i < 4; i++)
            {
                var actorDesc = new ActorDescription()
                {
                    GlobalPose = Matrix.Translation(positions[i] + p),
                    Shapes     = { new BoxShapeDescription(sizes[i]) }
                };

                var actor = scene.CreateActor(actorDesc);

                _cloth.AttachToShape(actor.Shapes.First(), (ClothAttachmentFlag)0);
            }

            //

            // Something to drop on it
            {
                var actorDesc = new ActorDescription()
                {
                    GlobalPose      = Matrix.Translation(0, 100, 0),
                    Shapes          = { new SphereShapeDescription(2) },
                    BodyDescription = new BodyDescription(50)
                };

                var actor = scene.CreateActor(actorDesc);
            }
        }
Ejemplo n.º 3
0
        protected override void LoadPhysics(Scene scene)
        {
            int w = 26;
            int h = 26;

            float hw = w / 2.0f;
            float hh = h / 2.0f;

            Vector3 p = new Vector3(0, h + 1, 0);

            // Create a Grid of Points
            int vertices, indices;

            ClothMesh clothMesh;
            {
                var grid = VertexGrid.CreateGrid(w, h);

                vertices = grid.Points.Length;
                indices = grid.Indices.Length;

                ClothMeshDescription clothMeshDesc = new ClothMeshDescription();
                clothMeshDesc.AllocateVertices<Vector3>(vertices);
                clothMeshDesc.AllocateTriangles<int>(indices / 3);

                clothMeshDesc.VertexCount = vertices;
                clothMeshDesc.TriangleCount = indices / 3;

                clothMeshDesc.VerticesStream.SetData(grid.Points);
                clothMeshDesc.TriangleStream.SetData(grid.Indices);

                // We are using 32 bit integers for our indices, so make sure the 16 bit flag is removed.
                // 32 bits are the default, so this isn't technically needed, but it's good to show in a sample
                clothMeshDesc.Flags &= ~MeshFlag.Indices16Bit;
                clothMeshDesc.Flags |= (MeshFlag)((int)clothMeshDesc.Flags | (int)ClothMeshFlag.Tearable);

                //var elements = new[]
                //{
                //new SlimDX.Direct3D10.InputElement("Position", 0, SlimDX.DXGI.Format.R32G32B32A32_Float, 0, 0),
                //new SlimDX.Direct3D10.InputElement("Color", 0, SlimDX.DXGI.Format.R32G32B32A32_Float, 16, 0)
                //};
                //mesh = new SlimDX.Direct3D10.Mesh(Engine.GraphicsDevice, elements, "Position", vertices, indices / 3, SlimDX.Direct3D10.MeshFlags.Has32BitIndices);

                // Write the cooked data to memory
                using (var memoryStream = new MemoryStream())
                {
                    Cooking.InitializeCooking();
                    Cooking.CookClothMesh(clothMeshDesc, memoryStream);
                    Cooking.CloseCooking();

                    // Need to reset the position of the stream to the beginning
                    memoryStream.Position = 0;

                    clothMesh = Engine.Core.CreateClothMesh(memoryStream);
                }
            }

            //

            int j = vertices * 2;
            int k = indices * 3;

            var clothDesc = new ClothDescription()
            {
                ClothMesh = clothMesh,
                GlobalPose =
                    Matrix.RotationX((float)Math.PI / 2.0F) *
                    Matrix.Translation(-w - 1, 0, 0) *
                    Matrix.Translation(p),
                Flags = ClothFlag.Gravity | ClothFlag.Bending | ClothFlag.CollisionTwoway | ClothFlag.Visualization,
                BendingStiffness = 0.1f,
                TearFactor = 1.5f,
                WindAcceleration = new Vector3(windX, windY, windZ)
            };
            clothDesc.MeshData.AllocatePositions<Vector3>(j);
            clothDesc.MeshData.AllocateIndices<int>(k);
            clothDesc.MeshData.AllocateNormals<Vector3>(j);

            clothDesc.MeshData.MaximumVertices = j;
            clothDesc.MeshData.MaximumIndices = k;

            clothDesc.MeshData.NumberOfVertices = vertices;
            clothDesc.MeshData.NumberOfIndices = indices;

            _clothL = scene.CreateCloth(clothDesc);

            var clothDesc2 = new ClothDescription()
            {
                ClothMesh = clothMesh,
                GlobalPose =
                    Matrix.RotationX((float)Math.PI / 2.0F) *
                    Matrix.Translation(1, 0, 0) *
                    Matrix.Translation(p),
                Flags = ClothFlag.Gravity | ClothFlag.Bending | ClothFlag.CollisionTwoway | ClothFlag.Visualization,
                BendingStiffness = 0.1f,
                TearFactor = 1.5f,
                WindAcceleration = new Vector3(windX, windY, windZ)
            };
            clothDesc2.MeshData.AllocatePositions<Vector3>(j);
            clothDesc2.MeshData.AllocateIndices<int>(k);
            clothDesc2.MeshData.AllocateNormals<Vector3>(j);

            clothDesc2.MeshData.MaximumVertices = j;
            clothDesc2.MeshData.MaximumIndices = k;

            clothDesc2.MeshData.NumberOfVertices = vertices;
            clothDesc2.MeshData.NumberOfIndices = indices;

            _clothR = scene.CreateCloth(clothDesc2);
            //

            for (int i = 0; i <= w; i += 2)
            {
                var actorDesc = new ActorDescription()
                {
                    GlobalPose = Matrix.Translation(new Vector3(i - w - 1, 0, 0) + p),
                    Shapes = { new SphereShapeDescription(0.3F) },
                    BodyDescription = new BodyDescription(3)
                };
                var actor = scene.CreateActor(actorDesc);
                AnchorActorsL.Add(actor);
                _clothL.AttachToShape(actor.Shapes.First(), (ClothAttachmentFlag)0);
            }
            for (int i = 0; i <= w; i += 2)
            {
                var actorDesc = new ActorDescription()
                {
                    GlobalPose = Matrix.Translation(new Vector3(-i + w + 1, 0, 0) + p),
                    Shapes = { new SphereShapeDescription(0.3F) },
                    BodyDescription = new BodyDescription(3)
                };
                var actor = scene.CreateActor(actorDesc);
                AnchorActorsR.Add(actor);
                _clothR.AttachToShape(actor.Shapes.First(), (ClothAttachmentFlag)0);
            }
            for (int i = 0; i <= 1; i++)
            {
                var actorDesc = new ActorDescription()
                {
                    GlobalPose = Matrix.Translation(new Vector3(0, -1,  0.2F * (float)Math.Pow(-1, i)) + p),
                    Shapes = { new BoxShapeDescription(new Vector3(2 * w + 4, 0.001F, 0.1F)) }
                };
                scene.CreateActor(actorDesc);
            }
            for (int i = 0; i <= 1; i++)
            {
                var actorDesc = new ActorDescription()
                {
                    GlobalPose = Matrix.Translation(new Vector3(0, -1, 0.4F * (float)Math.Pow(-1, i)) + p),
                    Shapes = { new BoxShapeDescription(new Vector3(2 * w + 4, 1, 0.1F)) }
                };
                scene.CreateActor(actorDesc);
            }
            for (int i = 0; i <= 1; i++)
            {
                var actorDesc = new ActorDescription()
                {
                    GlobalPose = Matrix.Translation(new Vector3((w + 2.1F) * (float)Math.Pow(-1, i), -1, 0) + p),
                    Shapes = { new BoxShapeDescription(new Vector3(0.1F, 1, 1)) }
                };
                scene.CreateActor(actorDesc);
            }
        }