Exemplo n.º 1
0
        public void DisposeOfFluid()
        {
            using (CreateCoreAndScene())
            {
                const int numberOfParticles = 100;

                FluidDescription fluidDescription = new FluidDescription()
                {
                    Flags = FluidFlag.Enabled | FluidFlag.Visualization
                };

                Random random = new Random();

                fluidDescription.InitialParticleData.AllocatePositionBuffer <Vector3>(numberOfParticles);
                for (int i = 0; i < numberOfParticles; i++)
                {
                    float x = (float)random.NextDouble();
                    float y = (float)random.NextDouble();
                    float z = (float)random.NextDouble();

                    fluidDescription.InitialParticleData.PositionBuffer.Write(new Vector3(x, y, z) * 50);
                }

                fluidDescription.InitialParticleData.NumberOfParticles = numberOfParticles;

                fluidDescription.IsValid();
                Fluid fluid = this.Scene.CreateFluid(fluidDescription);

                fluid.Dispose();
            }
        }
Exemplo n.º 2
0
		public void DisposeOfFluid()
		{
			using (CreateCoreAndScene())
			{
				const int numberOfParticles = 100;

				FluidDescription fluidDescription = new FluidDescription()
				{
					Flags = FluidFlag.Enabled | FluidFlag.Visualization
				};

				Random random = new Random();

				fluidDescription.InitialParticleData.AllocatePositionBuffer<Vector3>(numberOfParticles);
				for (int i = 0; i < numberOfParticles; i++)
				{
					float x = (float)random.NextDouble();
					float y = (float)random.NextDouble();
					float z = (float)random.NextDouble();

					fluidDescription.InitialParticleData.PositionBuffer.Write(new Vector3(x, y, z) * 50);
				}

				fluidDescription.InitialParticleData.NumberOfParticles = numberOfParticles;

				fluidDescription.IsValid();
				Fluid fluid = this.Scene.CreateFluid(fluidDescription);

				fluid.Dispose();
			}
		}
Exemplo n.º 3
0
        public static PhysX.Fluid FluidWithEmitterAndDrain(Scene scene)
        {
            const int maximumParticles = 1000;

            var fluidEmitterDesc = new FluidEmitterDescription()
            {
                DimensionX   = 0.5f,
                DimensionY   = 0.5f,
                Rate         = 15,
                RelativePose =
                    Matrix.RotationAxis(new Vector3(0, 1, 0), (float)Math.PI) *
                    Matrix.Translation(-40, 10, -50),
                Shape       = EmitterShape.Rectangular,
                Type        = EmitterType.ConstantFlowRate,
                RandomAngle = 0.5f
            };

            fluidEmitterDesc.Flags |= (FluidEmitterFlag.Enabled | FluidEmitterFlag.Visualization);

            var fluidDesc = new FluidDescription()
            {
                Emitters         = { fluidEmitterDesc },
                Flags            = FluidFlag.Enabled | FluidFlag.Visualization,
                MaximumParticles = maximumParticles
            };

            fluidDesc.ParticleWriteData.AllocatePositionBuffer <Vector3>(maximumParticles);
            fluidDesc.ParticleWriteData.NumberOfParticles = maximumParticles;

            var fluid = scene.CreateFluid(fluidDesc);

            // Ledge
            {
                var boxShapeDesc = new BoxShapeDescription(5, 0.1f, 5);

                var drainActorDesc = new ActorDescription()
                {
                    GlobalPose = Matrix.RotationX(-0.5f) * Matrix.Translation(-40, 5, -52),
                    Shapes     = { boxShapeDesc }
                };

                var drianActor = scene.CreateActor(drainActorDesc);
            }

            // Drain
            {
                var boxShapeDesc = new BoxShapeDescription(5, 0.1f, 5);
                boxShapeDesc.Flags |= ShapeFlag.FluidDrain;

                var drainActorDesc = new ActorDescription()
                {
                    GlobalPose = Matrix.Translation(-40, 0, -55),
                    Shapes     = { boxShapeDesc }
                };

                var drianActor = scene.CreateActor(drainActorDesc);
            }

            return(fluid);
        }
Exemplo n.º 4
0
		public static PhysX.Fluid FluidWithEmitterAndDrain(Scene scene)
		{
			const int maximumParticles = 1000;

			var fluidEmitterDesc = new FluidEmitterDescription()
			{
				DimensionX = 0.5f,
				DimensionY = 0.5f,
				Rate = 15,
				RelativePose = 
					Matrix.RotationAxis(new Vector3(0, 1, 0), (float)Math.PI) *
					Matrix.Translation(-40, 10, -50),
				Shape = EmitterShape.Rectangular,
				Type = EmitterType.ConstantFlowRate,
				RandomAngle = 0.5f
			};
			fluidEmitterDesc.Flags |= (FluidEmitterFlag.Enabled | FluidEmitterFlag.Visualization);

			var fluidDesc = new FluidDescription()
			{
				Emitters = { fluidEmitterDesc },
				Flags = FluidFlag.Enabled | FluidFlag.Visualization,
				MaximumParticles = maximumParticles
			};
			fluidDesc.ParticleWriteData.AllocatePositionBuffer<Vector3>(maximumParticles);
			fluidDesc.ParticleWriteData.NumberOfParticles = maximumParticles;

			var fluid = scene.CreateFluid(fluidDesc);

			// Ledge
			{
				var boxShapeDesc = new BoxShapeDescription(5, 0.1f, 5);

				var drainActorDesc = new ActorDescription()
				{
					GlobalPose = Matrix.RotationX(-0.5f) * Matrix.Translation(-40, 5, -52),
					Shapes = { boxShapeDesc }
				};

				var drianActor = scene.CreateActor(drainActorDesc);
			}

			// Drain
			{
				var boxShapeDesc = new BoxShapeDescription(5, 0.1f, 5);
				boxShapeDesc.Flags |= ShapeFlag.FluidDrain;

				var drainActorDesc = new ActorDescription()
				{
					GlobalPose = Matrix.Translation(-40, 0, -55),
					Shapes = { boxShapeDesc }
				};

				var drianActor = scene.CreateActor(drainActorDesc);
			}

			return fluid;
		}
Exemplo n.º 5
0
		public void ParticleWriteData()
		{
			using (CreateCoreAndScene())
			{
				var fluidDesc = new FluidDescription()
				{
					Flags = FluidFlag.Enabled
				};

				int numParticles = 1000;

				fluidDesc.ParticleWriteData.AllocatePositionBuffer<Vector3>(numParticles);
				fluidDesc.ParticleWriteData.NumberOfParticles = numParticles;

				var emitterDesc = new FluidEmitterDescription()
				{
					DimensionX = 0.01f,
					DimensionY = 0.01f,
					MaximumParticles = numParticles,
					Shape = EmitterShape.Ellipse,
					FluidVelocityMagnitude = 0f,
					RandomPosition = new Vector3(1f / 100f, 1f / 100f, 1f / 100f),
				};

				var fluid = this.Scene.CreateFluid(fluidDesc);
				fluid.CreateFluidEmitter(emitterDesc);

				// Advance the scene forward so the emitter has time to emit some particles
				for (int i = 0; i < 100; i++)
				{
					this.Scene.Simulate(1f / 60f);
					this.Scene.FlushStream();
					this.Scene.FetchResults(SimulationStatus.RigidBodyFinished, true);
				}

				// Read the particles
				Vector3[] positions = fluid.ParticleWriteData.PositionBuffer.GetData<Vector3>();

				// There should be a handful particles with positions other than 0, 0, 0
				Assert.AreNotEqual(Vector3.Zero, positions[0]);
				Assert.AreNotEqual(Vector3.Zero, positions[1]);
				Assert.AreNotEqual(Vector3.Zero, positions[2]);
				Assert.AreNotEqual(Vector3.Zero, positions[3]);
				Assert.AreNotEqual(Vector3.Zero, positions[4]);
			}
		}
Exemplo n.º 6
0
        public void ParticleWriteData()
        {
            using (CreateCoreAndScene())
            {
                var fluidDesc = new FluidDescription()
                {
                    Flags = FluidFlag.Enabled
                };

                int numParticles = 1000;

                fluidDesc.ParticleWriteData.AllocatePositionBuffer <Vector3>(numParticles);
                fluidDesc.ParticleWriteData.NumberOfParticles = numParticles;

                var emitterDesc = new FluidEmitterDescription()
                {
                    DimensionX             = 0.01f,
                    DimensionY             = 0.01f,
                    MaximumParticles       = numParticles,
                    Shape                  = EmitterShape.Ellipse,
                    FluidVelocityMagnitude = 0f,
                    RandomPosition         = new Vector3(1f / 100f, 1f / 100f, 1f / 100f),
                };

                var fluid = this.Scene.CreateFluid(fluidDesc);
                fluid.CreateFluidEmitter(emitterDesc);

                // Advance the scene forward so the emitter has time to emit some particles
                for (int i = 0; i < 100; i++)
                {
                    this.Scene.Simulate(1f / 60f);
                    this.Scene.FlushStream();
                    this.Scene.FetchResults(SimulationStatus.RigidBodyFinished, true);
                }

                // Read the particles
                Vector3[] positions = fluid.ParticleWriteData.PositionBuffer.GetData <Vector3>();

                // There should be a handful particles with positions other than 0, 0, 0
                Assert.AreNotEqual(Vector3.Zero, positions[0]);
                Assert.AreNotEqual(Vector3.Zero, positions[1]);
                Assert.AreNotEqual(Vector3.Zero, positions[2]);
                Assert.AreNotEqual(Vector3.Zero, positions[3]);
                Assert.AreNotEqual(Vector3.Zero, positions[4]);
            }
        }
Exemplo n.º 7
0
        protected override void LoadPhysics(Scene scene)
        {
            // Create a simple fluid description with fluids and visualization enabled
            FluidDescription fluidDesc = new FluidDescription()
            {
                Flags = FluidFlag.Enabled | FluidFlag.Visualization,
            };

            // Store our particle positions somewhere (as our particle generation function below generates and unknown number of particles at runtime we need a list instead of an array)
            var particlePositions = new List <Vector3>();

            // Move all the particles by this offset
            Vector3 position = new Vector3(0, 20, 0);

            // Number of particles in the x, y and z directions
            int   sideNum  = 10;
            float distance = 1f;

            float radius = sideNum * distance * 0.5f;

            for (int i = 0; i < sideNum; i++)
            {
                for (int j = 0; j < sideNum; j++)
                {
                    for (int k = 0; k < sideNum; k++)
                    {
                        Vector3 p = new Vector3(i * distance, j * distance, k * distance);

                        if ((p - new Vector3(radius, radius, radius)).Length() < radius)
                        {
                            p += position - new Vector3(radius, radius, radius);

                            particlePositions.Add(p);
                        }
                    }
                }
            }

            // Allocate memory for the initial particle positions to be stored in
            // And then set the position buffer
            fluidDesc.InitialParticleData.AllocatePositionBuffer <Vector3>(particlePositions.Count);
            fluidDesc.InitialParticleData.NumberOfParticles = particlePositions.Count;
            fluidDesc.InitialParticleData.PositionBuffer.SetData(particlePositions.ToArray());

            // Allocate memory for PhysX to store the position of each particle
            fluidDesc.ParticleWriteData.AllocatePositionBuffer <Vector3>(particlePositions.Count);
            fluidDesc.ParticleWriteData.NumberOfParticles = particlePositions.Count;

            Fluid fluid = scene.CreateFluid(fluidDesc);

            //

            // Make a basic box (ramp) for the particle to slide down
            ActorDescription rampDesc = new ActorDescription()
            {
                Shapes     = { new BoxShapeDescription(10, 0.2f, 10) },
                GlobalPose =
                    Matrix.RotationZ(0.3f) *
                    Matrix.Translation(0, 5, 0)
            };

            Actor ramp = scene.CreateActor(rampDesc);
        }
Exemplo n.º 8
0
		public void LoadPhysics()
		{
			Core _core = this.Core;
			Scene _scene = this.Scene;

			#region Some Boxes
			for( int x = 0; x < 5; x++ )
			{
				BoxShapeDescription boxShapeDesc = new BoxShapeDescription( 2, 3, 8 );
				
				ActorDescription actorDesc = new ActorDescription()
				{
					Name = String.Format( "Box {0}", x ),
					BodyDescription = new BodyDescription( 10.0f ),
					GlobalPose = Matrix.Translation( 100, 15 + 3 * x, 20 ),
					Shapes = { boxShapeDesc }
				};

				Actor actor = _scene.CreateActor( actorDesc );
			}
			#endregion

			//#region Cloth (Flag)
			//{
			//    // Create a Grid of Points
			//    VertexGrid grid = VertexGrid.CreateGrid( 10, 10 );

			//    ClothMeshDescription clothMeshDesc = new ClothMeshDescription();
			//        clothMeshDesc.AllocateVertices<Vector3>( grid.Points.Length );
			//        clothMeshDesc.AllocateTriangles<int>( grid.Indices.Length / 3 );

			//        clothMeshDesc.VertexCount = grid.Points.Length;
			//        clothMeshDesc.TriangleCount = grid.Indices.Length / 3;

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

			//        // We are using 32 bit integers, so make sure the 16 bit flag is removed.
			//        // 32 bits are the default, so this isn't technically needed
			//        clothMeshDesc.Flags &= ~MeshFlag.Indices16Bit;

			//    // Write the cooked data to memory
			//    MemoryStream 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 clothMesh = _core.CreateClothMesh( memoryStream );

			//    //

			//    ClothDescription clothDesc = new ClothDescription()
			//    {
			//        ClothMesh = clothMesh,
			//        Flags = ClothFlag.Gravity | ClothFlag.Bending | ClothFlag.CollisionTwoway | ClothFlag.Visualization,
			//        GlobalPose =
			//            Matrix.CreateFromYawPitchRoll( 0, (float)Math.PI / 2.0f, (float)Math.PI / 2.0f ) *
			//            Matrix.CreateTranslation( 0, 20, 0 )
			//    };
			//    clothDesc.MeshData.AllocatePositions<Vector3>( grid.Points.Length );
			//    clothDesc.MeshData.AllocateIndices<int>( grid.Indices.Length );

			//    clothDesc.MeshData.MaximumVertices = grid.Points.Length;
			//    clothDesc.MeshData.MaximumIndices = grid.Indices.Length;

			//    clothDesc.MeshData.NumberOfVertices = grid.Points.Length;
			//    clothDesc.MeshData.NumberOfIndices = grid.Indices.Length;

			//    _flag = _scene.CreateCloth( clothDesc );

			//    // Flag Pole
			//    ActorDescription flagPoleActorDesc = new ActorDescription()
			//    {
			//        GlobalPose = Matrix.CreateTranslation( 0, 10, 0 ),
			//        Shapes = { new BoxShapeDescription( 1.0f, 20.0f, 1.0f ) }
			//    };

			//    Actor flagPoleActor = _scene.CreateActor( flagPoleActorDesc );

			//    _flag.AttachToShape( flagPoleActor.Shapes[ 0 ], 0 );
			//    _flag.WindAcceleration = new Vector3( 10, 10, 10 );
			//    _flag.BendingStiffness = 0.1f;
			//}
			//#endregion
			
			#region Revolute Joint
			{
				BoxShapeDescription boxShapeDescA = new BoxShapeDescription( 3, 3, 3 );
				BoxShapeDescription boxShapeDescB = new BoxShapeDescription( 3, 3, 3 );

				ActorDescription actorDescA = new ActorDescription()
				{
					BodyDescription = new BodyDescription( 10.0f ),
					GlobalPose = Matrix.Translation( 75, 1.5f, 55 ),
					Shapes = { boxShapeDescA }
				};
				Actor actorA = _scene.CreateActor( actorDescA );

				ActorDescription actorDescB = new ActorDescription()
				{
					BodyDescription = new BodyDescription( 10.0f ),
					GlobalPose = Matrix.Translation( 70, 1.5f, 55 ),
					Shapes = { boxShapeDescB }
				};
				Actor actorB = _scene.CreateActor( actorDescB );

				//

				RevoluteJointDescription revoluteJointDesc = new RevoluteJointDescription()
				{
					Actor1 = actorA,
					Actor2 = actorB,
					Motor = new MotorDescription( 20, 20.1f, true )
				};
				revoluteJointDesc.Flags |= RevoluteJointFlag.MotorEnabled;
				revoluteJointDesc.SetGlobalAnchor( new Vector3( 73.5f, 1.5f, 55 ) );
				revoluteJointDesc.SetGlobalAxis( new Vector3( 1, 0, 0 ) );

				RevoluteJoint revoluteJoint = _scene.CreateJoint( revoluteJointDesc ) as RevoluteJoint;
			}
			#endregion

			#region Prismatic Joint with Limit
			{
				Actor actorA, actorB;
				{
					BoxShapeDescription boxShapeDesc = new BoxShapeDescription( 3, 3, 3 );

					BodyDescription bodyDesc = new BodyDescription( 10.0f );
						bodyDesc.BodyFlags |= BodyFlag.Kinematic;

					ActorDescription actorDesc = new ActorDescription()
					{
						BodyDescription = bodyDesc,
						GlobalPose = Matrix.Translation( 70, 25, 65 ),
						Shapes = { boxShapeDesc }
					};
					actorA = _scene.CreateActor( actorDesc );
				}
				{
					BoxShapeDescription boxShapeDesc = new BoxShapeDescription( 3, 3, 3 );

					ActorDescription actorDesc = new ActorDescription()
					{
						BodyDescription = new BodyDescription( 10.0f ),
						GlobalPose = Matrix.Translation( 70, 15, 65 ),
						Shapes = { boxShapeDesc }
					};
					actorB = _scene.CreateActor( actorDesc );
				}

				PrismaticJointDescription prismaticJointDesc = new PrismaticJointDescription()
				{
					Actor1 = actorA,
					Actor2 = actorB,
				};
				prismaticJointDesc.SetGlobalAnchor( new Vector3( 70, 20, 65 ) );
				prismaticJointDesc.SetGlobalAxis( new Vector3( 0, 1, 0 ) );

				PrismaticJoint prismaticJoint = _scene.CreateJoint( prismaticJointDesc ) as PrismaticJoint;

				LimitPlane limitPlane = new LimitPlane( new Vector3( 0, 1, 0 ), new Vector3( -30, 8, -30 ), 0 );
					prismaticJoint.AddLimitPlane( limitPlane );
			}
			#endregion
			
			#region Fluid
			{
				const int maximumParticles = 1000;

				FluidEmitterDescription fluidEmitterDesc = new FluidEmitterDescription()
				{
					DimensionX = 0.5f,
					DimensionY = 0.5f,
					Rate = 15,
					RelativePose = Matrix.Translation( -40, 10, 50 ),
					Shape = EmitterShape.Rectangular,
					Type = EmitterType.ConstantFlowRate,
					RandomAngle = 0.5f
				};
				fluidEmitterDesc.Flags |= ( FluidEmitterFlag.Enabled | FluidEmitterFlag.Visualization );

				FluidDescription fluidDesc = new FluidDescription()
				{
					Emitters = { fluidEmitterDesc },
					Flags = FluidFlag.Enabled | FluidFlag.Visualization,
					MaximumParticles = maximumParticles
				};
				fluidDesc.ParticleWriteData.AllocatePositionBuffer<Vector3>( maximumParticles );
				fluidDesc.ParticleWriteData.NumberOfParticles = maximumParticles;

				Fluid fluid = _scene.CreateFluid( fluidDesc );

				// Ledge
				{
					BoxShapeDescription boxShapeDesc = new BoxShapeDescription( 5, 0.1f, 5 );
					
					ActorDescription drainActorDesc = new ActorDescription()
					{
						GlobalPose = Matrix.RotationX( 0.5f ) * Matrix.Translation( -40, 5, 52 ),
						Shapes =  { boxShapeDesc }
					};

					Actor drianActor = _scene.CreateActor( drainActorDesc );
				}

				// Drain
				{
					BoxShapeDescription boxShapeDesc = new BoxShapeDescription( 5, 0.1f, 5 );
						boxShapeDesc.Flags |= ShapeFlag.FluidDrain;

					ActorDescription drainActorDesc = new ActorDescription()
					{
						GlobalPose = Matrix.Translation( -40, 0, 55 ),
						Shapes = { boxShapeDesc }
					};

					Actor drianActor = _scene.CreateActor( drainActorDesc );
				}
			}
			#endregion
			
			#region Force Field
			{
				BoxForceFieldShapeDescription boxForceFieldShapeDesc = new BoxForceFieldShapeDescription()
				{
					Size = new Vector3( 10, 10, 10 )
				};

				ForceFieldLinearKernelDescription kernelDesc = new ForceFieldLinearKernelDescription()
				{
					Constant = new Vector3( 0, 100.0f, 0 )
				};

				ForceFieldLinearKernel kernel = _scene.CreateForceFieldLinearKernel( kernelDesc );

				ForceFieldShapeGroupDescription shapeGroupDesc = new ForceFieldShapeGroupDescription()
				{
					Shapes = { boxForceFieldShapeDesc }
				};

				ForceFieldShapeGroup shapeGroup = _scene.CreateForceFieldShapeGroup( shapeGroupDesc );

				BoxForceFieldShape boxForceFieldShape = shapeGroup.CreateShape( boxForceFieldShapeDesc ) as BoxForceFieldShape;
				boxForceFieldShape.Pose = Matrix.Translation( 30, 5, 0 );

				ForceFieldDescription forceFieldDesc = new ForceFieldDescription()
				{
					Kernel = kernel,
					ShapeGroups = { shapeGroup }
				};
				ForceField forceField = _scene.CreateForceField( forceFieldDesc );
			}
			#endregion
			
			#region Heightfield
			{
				int rows = 25;
				int columns = 25;

				HeightFieldSample[] samples = new HeightFieldSample[ rows * columns ];
				for( int r = 0; r < rows; r++ )
				{
					for( int c = 0; c < columns; c++ )
					{
						// Put a z and x curve together
						double h = Math.Sin( c ) * Math.Cos( r ) * short.MaxValue;

						HeightFieldSample sample = new HeightFieldSample()
						{
							Height = (short)h,
							MaterialIndex0 = 0,
							MaterialIndex1 = 1,
							TessellationFlag = 0
						};

						samples[ r * columns + c ] = sample;
					}
				}

				HeightFieldDescription heightFieldDesc = new HeightFieldDescription()
				{
					NumberOfRows = rows,
					NumberOfColumns = columns
				};
				heightFieldDesc.SetSamples( samples );

				HeightField heightField = _core.CreateHeightField( heightFieldDesc );
				
				//

				HeightFieldShapeDescription heightFieldShapeDesc = new HeightFieldShapeDescription()
				{
					HeightField = heightField,
					HoleMaterial = 2,
					// The max height of our samples is short.MaxValue and we want it to be 1
					HeightScale = 1.0f / (float)short.MaxValue,
					RowScale = 3,
					ColumnScale = 3
				};
				heightFieldShapeDesc.LocalPosition = new Vector3( -0.5f * rows * 1 * heightFieldShapeDesc.RowScale, 0, -0.5f * columns * 1 * heightFieldShapeDesc.ColumnScale );

				ActorDescription actorDesc = new ActorDescription()
				{
					GlobalPose = Matrix.Translation( 100, 0, 0 ),
					Shapes = { heightFieldShapeDesc }
				};
				Actor actor = _scene.CreateActor( actorDesc );
			}
			#endregion
			
			//#region Convex Mesh
			//{
			//    ModelMesh mesh = _torusModel.Meshes.First();

			//    Matrix[] transforms = new Matrix[ _torusModel.Bones.Count ];
			//    _torusModel.CopyAbsoluteBoneTransformsTo( transforms );

			//    // Gets the vertices from the mesh
			//    VertexPositionNormalTexture[] vertices = new VertexPositionNormalTexture[ mesh.MeshParts[ 0 ].NumVertices ];
			//    mesh.VertexBuffer.GetData<VertexPositionNormalTexture>( vertices );

			//    //

			//    // Allocate memory for the points and triangles
			//    var convexMeshDesc = new ConvexMeshDescription()
			//    {
			//        PointCount = vertices.Length
			//    };
			//    convexMeshDesc.Flags |= ConvexFlag.ComputeConvex;
			//    convexMeshDesc.AllocatePoints<Vector3>( vertices.Length );

			//    // Write in the points and triangles
			//    // We only want the Position component of the vertex. Also scale down the mesh
			//    foreach( VertexPositionNormalTexture vertex in vertices )
			//    {
			//        Vector3 position = Vector3.Transform( vertex.Position, Matrix.CreateScale( 0.1f, 0.1f, 0.1f ) * transforms[ 0 ] );

			//        convexMeshDesc.PointsStream.Write( position );
			//    }

			//    //

			//    // Cook to memory or to a file
			//    MemoryStream stream = new MemoryStream();
			//    //FileStream stream = new FileStream( @"Convex Mesh.cooked", FileMode.CreateNew );

			//    Cooking.InitializeCooking( new ConsoleOutputStream() );
			//    Cooking.CookConvexMesh( convexMeshDesc, stream );
			//    Cooking.CloseCooking();

			//    stream.Position = 0;

			//    ConvexMesh convexMesh = _core.CreateConvexMesh( stream );

			//    ConvexShapeDescription convexShapeDesc = new ConvexShapeDescription( convexMesh );

			//    ActorDescription actorDesc = new ActorDescription()
			//    {
			//        BodyDescription = new BodyDescription( 10.0f ),
			//        GlobalPose = Matrix.CreateTranslation( 30, 30, 0 )
			//    };
			//    actorDesc.Shapes.Add( convexShapeDesc );

			//    _torusActor = _scene.CreateActor( actorDesc );
			//}
			//#endregion
			
			//#region SoftBody
			//if( false ) // Enable to view soft bodies, they run slowly
			//{
			//    XmlDocument doc = new XmlDocument();
			//        doc.Load( "Teapot.xml" );
				
			//    // Not how NxuStream are meant to used but what ever :S
			//    Vector3[] vertices = ReadVertices( doc.SelectSingleNode( "/NXUSTREAM2/NxuPhysicsCollection/NxSoftBodyMeshDesc/vertices" ) );
			//    int[] tetrahedraSingles = ReadTetrahedra( doc.SelectSingleNode( "/NXUSTREAM2/NxuPhysicsCollection/NxSoftBodyMeshDesc/tetrahedra" ) );

			//    var softBodyMeshDesc = new SoftBodyMeshDescription()
			//    {
			//        VertexCount = vertices.Length,
			//        TetrahedraCount = tetrahedraSingles.Length / 4 // Tetrahedras come in quadruples of ints
			//    };

			//    softBodyMeshDesc.AllocateVertices<Vector3>( softBodyMeshDesc.VertexCount );
			//    softBodyMeshDesc.AllocateTetrahedra<int>( softBodyMeshDesc.TetrahedraCount ); // Note: T is an int. T is the type of each point

			//    softBodyMeshDesc.VertexStream.SetData( vertices );
			//    softBodyMeshDesc.TetrahedraStream.SetData( tetrahedraSingles );

			//    MemoryStream memoryStream = new MemoryStream();

			//    Cooking.InitializeCooking();
			//    Cooking.CookSoftBodyMesh( softBodyMeshDesc, memoryStream );
			//    Cooking.CloseCooking();

			//    memoryStream.Position = 0;

			//    SoftBodyMesh softBodyMesh = _core.CreateSoftBodyMesh( memoryStream );

			//    SoftBodyDescription desc = new SoftBodyDescription()
			//    {
			//        GlobalPose = Matrix.CreateTranslation( -30, 20, -30 ),
			//        SoftBodyMesh = softBodyMesh
			//    };
			//    desc.Flags |= SoftBodyFlag.Visualization;

			//    desc.MeshData.AllocatePositions<Vector3>( vertices.Length );
			//    desc.MeshData.AllocateIndices<int>( tetrahedraSingles.Length );

			//    SoftBody softBody = _scene.CreateSoftBody( desc );
			//}
			//#endregion

			//#region Reports
			//// Contact report
			//// When the capsule actor hits the ground make it bounce by using the conact report
			//{
			//    CapsuleShapeDescription capsuleShapeDesc = new CapsuleShapeDescription( 1, 5 );
				
			//    ActorDescription actorDesc = new ActorDescription()
			//    {
			//        GlobalPose = Matrix.CreateTranslation( -30, 20, 0 ),
			//        BodyDescription = new BodyDescription( 10.0f ),
			//        Name = "Report Capsule",
			//        Shapes = { capsuleShapeDesc }
			//    };

			//    _contactReportActor = _scene.CreateActor( actorDesc );

			//    _scene.SetActorPairFlags( _contactReportActor, _groundActor, ContactPairFlag.All );

			//    _scene.UserContactReport = new ContactReport( this );
			//}

			//// Trigger Reports
			//{
			//    BoxShapeDescription boxShapeDesc = new BoxShapeDescription( 15, 8, 15 );
			//        boxShapeDesc.Flags |= ( ShapeFlag.TriggerOnEnter | ShapeFlag.TriggerOnLeave );

			//    ActorDescription actorDesc = new ActorDescription()
			//    {
			//        GlobalPose = Matrix.CreateTranslation( -30, 4, 0 ),
			//        Shapes = { boxShapeDesc }
			//    };
			//    _scene.CreateActor( actorDesc );

			//    _scene.UserTriggerReport = new TriggerReport( this );
			//}

			//_scene.UserNotify = new Notify( this );
			//#endregion

			//#region Wheel
			//{
			//    _basicVehicle = new Vehicle( this );
			//}
			//#endregion

			#region Controller
			{
				ControllerManager manager = _scene.CreateControllerManager();

				CapsuleControllerDescription capsuleControllerDesc = new CapsuleControllerDescription( 4, 3 );

				CapsuleController capsuleController = manager.CreateController<CapsuleController>( capsuleControllerDesc );
					capsuleController.Position = new Vector3( 0, 1.5f + 2, -15 );
					capsuleController.Actor.Name = "BoxController";
					capsuleController.SetCollisionEnabled( true );
			}
			#endregion
		}
Exemplo n.º 9
0
        protected override void LoadContent(GraphicInfo GraphicInfo, GraphicFactory factory, IContentManager contentManager)
        {
            PhysxPhysicWorld PhysxPhysicWorld = World.PhysicWorld as PhysxPhysicWorld;

            base.LoadContent(GraphicInfo, factory, contentManager);

            // Create a simple fluid description with fluids and visualization enabled
            FluidDescription fluidDesc = new FluidDescription()
            {
                Flags = FluidFlag.Enabled | FluidFlag.Visualization,
            };

            // Store our particle positions somewhere (as our particle generation function below generates and unknown number of particles at runtime we need a list instead of an array)
            var particlePositions = new List <Phyx.Vector3>();

            // Move all the particles by this offset
            Phyx.Vector3 position = new Phyx.Vector3(0, 20, 0);

            // Number of particles in the x, y and z directions
            int   sideNum  = 10;
            float distance = 1f;

            float radius = sideNum * distance * 0.5f;

            for (int i = 0; i < sideNum; i++)
            {
                for (int j = 0; j < sideNum; j++)
                {
                    for (int k = 0; k < sideNum; k++)
                    {
                        Phyx.Vector3 p = new Phyx.Vector3(i * distance, j * distance, k * distance);

                        if ((p - new Phyx.Vector3(radius, radius, radius)).Length() < radius)
                        {
                            p += position - new Phyx.Vector3(radius, radius, radius);

                            particlePositions.Add(p);
                        }
                    }
                }
            }

            // Allocate memory for the initial particle positions to be stored in
            // And then set the position buffer
            fluidDesc.InitialParticleData.AllocatePositionBuffer <Phyx.Vector3>(particlePositions.Count);
            fluidDesc.InitialParticleData.NumberOfParticles = particlePositions.Count;
            fluidDesc.InitialParticleData.PositionBuffer.SetData(particlePositions.ToArray());

            // Allocate memory for PhysX to store the position of each particle
            fluidDesc.ParticleWriteData.AllocatePositionBuffer <Phyx.Vector3>(particlePositions.Count);
            fluidDesc.ParticleWriteData.NumberOfParticles = particlePositions.Count;

            InstancedBilboardModel          InstancedBilboardModel          = new InstancedBilboardModel(factory, "teste", "Textures/Smoke", new BilboardInstance[] { new BilboardInstance() }, particlePositions.Count);
            PhysxFluidObject                PhysxFluidObject                = new PloobsEngine.Physics.PhysxFluidObject(fluidDesc);
            DeferredInstancedBilboardShader DeferredInstancedBilboardShader = new PloobsEngine.Material.DeferredInstancedBilboardShader(BilboardType.Spherical);

            DeferredInstancedBilboardShader.AlphaTestLimit = 0.2f;
            FluidMaterial DeferredMaterial = new FluidMaterial(DeferredInstancedBilboardShader, particlePositions.Count, new Vector2(0.2f));
            IObject       IObject          = new IObject(DeferredMaterial, InstancedBilboardModel, PhysxFluidObject);

            this.World.AddObject(IObject);


            // Ledge
            {
                var         boxShapeDesc = new BoxShapeDescription(10, 0.2f, 10);
                SimpleModel SimpleModel  = new PloobsEngine.Modelo.SimpleModel(factory, "Model/block");
                SimpleModel.SetTexture(factory.CreateTexture2DColor(1, 1, Color.Red), TextureType.DIFFUSE);

                PhysxPhysicObject PhysxPhysicObject = new PloobsEngine.Physics.PhysxPhysicObject(boxShapeDesc,
                                                                                                 (Phyx.Matrix.RotationZ(0.3f) *
                                                                                                  Phyx.Matrix.Translation(0, 5, 0)).AsXNA(), new Vector3(10, 0.2f, 10));
                DeferredNormalShader shader    = new DeferredNormalShader();
                DeferredMaterial     fmaterial = new DeferredMaterial(shader);
                IObject obj = new IObject(fmaterial, SimpleModel, PhysxPhysicObject);
                this.World.AddObject(obj);
            }

            #region NormalLight
            DirectionalLightPE ld1 = new DirectionalLightPE(Vector3.Left, Color.White);
            DirectionalLightPE ld2 = new DirectionalLightPE(Vector3.Right, Color.White);
            DirectionalLightPE ld3 = new DirectionalLightPE(Vector3.Backward, Color.White);
            DirectionalLightPE ld4 = new DirectionalLightPE(Vector3.Forward, Color.White);
            DirectionalLightPE ld5 = new DirectionalLightPE(Vector3.Down, Color.White);
            float li = 0.5f;
            ld1.LightIntensity = li;
            ld2.LightIntensity = li;
            ld3.LightIntensity = li;
            ld4.LightIntensity = li;
            ld5.LightIntensity = li;
            this.World.AddLight(ld1);
            this.World.AddLight(ld2);
            this.World.AddLight(ld3);
            this.World.AddLight(ld4);
            this.World.AddLight(ld5);
            #endregion


            BallThrowPhysx28 BallThrowBullet = new BallThrowPhysx28(this.World, GraphicFactory, false);
            this.AttachCleanUpAble(BallThrowBullet);

            this.World.CameraManager.AddCamera(new CameraFirstPerson(GraphicInfo));
        }
        protected override void LoadContent(GraphicInfo GraphicInfo, GraphicFactory factory ,IContentManager contentManager)
        {
            PhysxPhysicWorld PhysxPhysicWorld = World.PhysicWorld as PhysxPhysicWorld;
            
            base.LoadContent(GraphicInfo, factory, contentManager);

            const int maximumParticles = 3000;

            ///Remember
            ///There are 2 math apis (XNA AND PHYSX)
            ///Sometimes we need to convert between then
            ///Use the extension methods AsPhysx() in XNA API and AsXNA in Physx APi

            ///emitter
            var fluidEmitterDesc = new FluidEmitterDescription()
            {
                DimensionX = 1.5f,
                DimensionY = 1.5f,
                Rate = 65,
                RelativePose =
                    Phyx.Matrix.RotationAxis(new Phyx.Vector3(0, 1, 0), (float)Math.PI) *
                    Phyx.Matrix.Translation(-40, 10, -50),
                Shape = EmitterShape.Rectangular,
                Type = EmitterType.ConstantFlowRate,
                RandomAngle = 0.5f,
                

            };
            fluidEmitterDesc.Flags |= (FluidEmitterFlag.Enabled | FluidEmitterFlag.Visualization);

            ///fluid
            var fluidDesc = new FluidDescription()
            {
                Emitters = { fluidEmitterDesc },
                Flags = FluidFlag.Enabled | FluidFlag.Visualization,
                MaximumParticles = maximumParticles,
                
                
            };
            
            fluidDesc.ParticleWriteData.AllocatePositionBuffer<Vector3>(maximumParticles);
            fluidDesc.ParticleWriteData.NumberOfParticles = maximumParticles;            

            
            ///create and add the fluid to the world
            fluid = PhysxPhysicWorld.Scene.CreateFluid(fluidDesc);

            
            ///Use Billboards to render the fuild particles (dummy way)
            Texture2D tex = factory.GetTexture2D("Textures/Smoke");
            for (int i = 0; i < maximumParticles; i++)
            {
                Billboard3D Billboard3D = new Billboard3D(tex,Vector3.Zero,new Vector2(0.001f));
                Billboard3D.Enabled = false;
                CPUSphericalBillboardComponent.Billboards.Add(Billboard3D);
            }


            // Ledge
            {
                var boxShapeDesc = new BoxShapeDescription(5, 0.1f, 5);
                SimpleModel SimpleModel = new PloobsEngine.Modelo.SimpleModel(factory, "Model/block");
                SimpleModel.SetTexture(factory.CreateTexture2DColor(1, 1, Color.Red), TextureType.DIFFUSE);
                PhysxPhysicObject PhysxPhysicObject = new PloobsEngine.Physics.PhysxPhysicObject(boxShapeDesc,
                    (Phyx.Matrix.RotationX(-0.5f) * Phyx.Matrix.Translation(-40, 5, -52)).AsXNA(),new Vector3(5,0.1f,5));
                ForwardXNABasicShader shader = new ForwardXNABasicShader(ForwardXNABasicShaderDescription.Default());
                ForwardMaterial fmaterial = new ForwardMaterial(shader);
                IObject obj = new IObject(fmaterial, SimpleModel, PhysxPhysicObject);
                this.World.AddObject(obj);
            }

            // Drain
            {
                var boxShapeDesc = new BoxShapeDescription(5, 0.1f, 5);
                boxShapeDesc.Flags |= ShapeFlag.FluidDrain;

                var drainActorDesc = new ActorDescription()
                {
                    GlobalPose = Phyx.Matrix.Translation(-40, -20, -55),
                    Shapes = { boxShapeDesc }
                };

                var drianActor = PhysxPhysicWorld.Scene.CreateActor(drainActorDesc);
            }            

            BallThrowPhysx28 BallThrowBullet = new BallThrowPhysx28(this.World, GraphicFactory);
            BallThrowBullet.ballSize = 1f;
            BallThrowBullet.Speed = 25;
            this.AttachCleanUpAble(BallThrowBullet);

            CameraFirstPerson CameraFirstPerson = new CameraFirstPerson(GraphicInfo);
            CameraFirstPerson.Position = new Vector3(-35, 8, -52);
            CameraFirstPerson.LeftRightRot = MathHelper.ToRadians(125);
            this.World.CameraManager.AddCamera(CameraFirstPerson);
        }
        protected override void LoadContent(GraphicInfo GraphicInfo, GraphicFactory factory, IContentManager contentManager)
        {
            PhysxPhysicWorld PhysxPhysicWorld = World.PhysicWorld as PhysxPhysicWorld;

            base.LoadContent(GraphicInfo, factory, contentManager);

            const int maximumParticles = 1000;
            var       fluidEmitterDesc = new FluidEmitterDescription()
            {
                DimensionX   = 0.5f,
                DimensionY   = 0.5f,
                Rate         = 15,
                RelativePose =
                    Phyx.Matrix.RotationAxis(new Phyx.Vector3(0, 1, 0), (float)Math.PI) *
                    Phyx.Matrix.Translation(-40, 10, -50),
                Shape       = EmitterShape.Rectangular,
                Type        = EmitterType.ConstantFlowRate,
                RandomAngle = 0.5f
            };

            fluidEmitterDesc.Flags |= (FluidEmitterFlag.Enabled | FluidEmitterFlag.Visualization);

            var fluidDesc = new FluidDescription()
            {
                Emitters         = { fluidEmitterDesc },
                Flags            = FluidFlag.Enabled | FluidFlag.Visualization | FluidFlag.Enabled,
                MaximumParticles = maximumParticles,
            };

            fluidDesc.ParticleWriteData.AllocatePositionBuffer <Vector3>(maximumParticles);
            fluidDesc.ParticleWriteData.NumberOfParticles = maximumParticles;

            InstancedBilboardModel          InstancedBilboardModel          = new InstancedBilboardModel(factory, "teste", "Textures/Smoke", new BilboardInstance[] { new BilboardInstance() }, maximumParticles);
            PhysxFluidObject                PhysxFluidObject                = new PloobsEngine.Physics.PhysxFluidObject(fluidDesc);
            DeferredInstancedBilboardShader DeferredInstancedBilboardShader = new PloobsEngine.Material.DeferredInstancedBilboardShader(BilboardType.Spherical);

            DeferredInstancedBilboardShader.AlphaTestLimit = 0.2f;
            FluidMaterial DeferredMaterial = new FluidMaterial(DeferredInstancedBilboardShader, maximumParticles, new Vector2(0.2f));
            IObject       IObject          = new IObject(DeferredMaterial, InstancedBilboardModel, PhysxFluidObject);

            this.World.AddObject(IObject);


            // Ledge
            {
                var         boxShapeDesc = new BoxShapeDescription(5, 0.1f, 5);
                SimpleModel SimpleModel  = new PloobsEngine.Modelo.SimpleModel(factory, "Model/block");
                SimpleModel.SetTexture(factory.CreateTexture2DColor(1, 1, Color.Red), TextureType.DIFFUSE);

                PhysxPhysicObject PhysxPhysicObject = new PloobsEngine.Physics.PhysxPhysicObject(boxShapeDesc,
                                                                                                 (Phyx.Matrix.RotationX(-0.5f) * Phyx.Matrix.Translation(-40, 5, -52)).AsXNA(), new Vector3(5, 0.1f, 5));
                DeferredNormalShader shader    = new DeferredNormalShader();
                DeferredMaterial     fmaterial = new DeferredMaterial(shader);
                IObject obj = new IObject(fmaterial, SimpleModel, PhysxPhysicObject);
                this.World.AddObject(obj);
            }

            // Drain
            {
                var boxShapeDesc = new BoxShapeDescription(5, 0.1f, 5);
                boxShapeDesc.Flags |= ShapeFlag.FluidDrain;

                var drainActorDesc = new ActorDescription()
                {
                    GlobalPose = Phyx.Matrix.Translation(-40, 0, -55),
                    Shapes     = { boxShapeDesc }
                };

                var drianActor = PhysxPhysicWorld.Scene.CreateActor(drainActorDesc);
            }

            #region NormalLight
            DirectionalLightPE ld1 = new DirectionalLightPE(Vector3.Left, Color.White);
            DirectionalLightPE ld2 = new DirectionalLightPE(Vector3.Right, Color.White);
            DirectionalLightPE ld3 = new DirectionalLightPE(Vector3.Backward, Color.White);
            DirectionalLightPE ld4 = new DirectionalLightPE(Vector3.Forward, Color.White);
            DirectionalLightPE ld5 = new DirectionalLightPE(Vector3.Down, Color.White);
            float li = 0.5f;
            ld1.LightIntensity = li;
            ld2.LightIntensity = li;
            ld3.LightIntensity = li;
            ld4.LightIntensity = li;
            ld5.LightIntensity = li;
            this.World.AddLight(ld1);
            this.World.AddLight(ld2);
            this.World.AddLight(ld3);
            this.World.AddLight(ld4);
            this.World.AddLight(ld5);
            #endregion


            BallThrowPhysx28 BallThrowBullet = new BallThrowPhysx28(this.World, GraphicFactory, false);
            this.AttachCleanUpAble(BallThrowBullet);

            this.World.CameraManager.AddCamera(new CameraFirstPerson(GraphicInfo));
        }
 public PhysxFluidObject(FluidDescription FluidDescription)
 {
     this.FluidDesc = FluidDescription;
 }
        protected override void LoadContent(GraphicInfo GraphicInfo, GraphicFactory factory, IContentManager contentManager)
        {
            PhysxPhysicWorld PhysxPhysicWorld = World.PhysicWorld as PhysxPhysicWorld;

            base.LoadContent(GraphicInfo, factory, contentManager);

            const int maximumParticles = 1000;
            var       fluidEmitterDesc = new FluidEmitterDescription()
            {
                DimensionX   = 0.5f,
                DimensionY   = 0.5f,
                Rate         = 15,
                RelativePose =
                    Phyx.Matrix.RotationAxis(new Phyx.Vector3(0, 1, 0), (float)Math.PI) *
                    Phyx.Matrix.Translation(-40, 10, -50),
                Shape       = EmitterShape.Rectangular,
                Type        = EmitterType.ConstantFlowRate,
                RandomAngle = 0.5f
            };

            fluidEmitterDesc.Flags |= (FluidEmitterFlag.Enabled | FluidEmitterFlag.Visualization);

            var fluidDesc = new FluidDescription()
            {
                Emitters         = { fluidEmitterDesc },
                Flags            = FluidFlag.Enabled | FluidFlag.Visualization | FluidFlag.Enabled,
                MaximumParticles = maximumParticles,
            };

            fluidDesc.ParticleWriteData.AllocatePositionBuffer <Vector3>(maximumParticles);
            fluidDesc.ParticleWriteData.NumberOfParticles = maximumParticles;

            fluid = PhysxPhysicWorld.Scene.CreateFluid(fluidDesc);


            Texture2D tex = factory.GetTexture2D("Textures/Smoke");

            for (int i = 0; i < maximumParticles; i++)
            {
                Billboard3D Billboard3D = new Billboard3D(tex, Vector3.Zero, new Vector2(0.002f));
                Billboard3D.Enabled = false;
                CPUSphericalBillboardComponent.Billboards.Add(Billboard3D);
            }


            // Ledge
            {
                var         boxShapeDesc = new BoxShapeDescription(5, 0.1f, 5);
                SimpleModel SimpleModel  = new PloobsEngine.Modelo.SimpleModel(factory, "Model/block");
                SimpleModel.SetTexture(factory.CreateTexture2DColor(1, 1, Color.Red), TextureType.DIFFUSE);
                PhysxPhysicObject PhysxPhysicObject = new PloobsEngine.Physics.PhysxPhysicObject(boxShapeDesc,
                                                                                                 (Phyx.Matrix.RotationX(-0.5f) * Phyx.Matrix.Translation(-40, 5, -52)).AsXNA(), new Vector3(5, 0.1f, 5));
                ForwardXNABasicShader shader    = new ForwardXNABasicShader(ForwardXNABasicShaderDescription.Default());
                ForwardMaterial       fmaterial = new ForwardMaterial(shader);
                IObject obj = new IObject(fmaterial, SimpleModel, PhysxPhysicObject);
                this.World.AddObject(obj);
            }

            // Drain
            {
                var boxShapeDesc = new BoxShapeDescription(5, 0.1f, 5);
                boxShapeDesc.Flags |= ShapeFlag.FluidDrain;

                var drainActorDesc = new ActorDescription()
                {
                    GlobalPose = Phyx.Matrix.Translation(-40, 0, -55),
                    Shapes     = { boxShapeDesc }
                };

                var drianActor = PhysxPhysicWorld.Scene.CreateActor(drainActorDesc);
            }

            BallThrowPhysx28 BallThrowBullet = new BallThrowPhysx28(this.World, GraphicFactory);

            this.AttachCleanUpAble(BallThrowBullet);

            this.World.CameraManager.AddCamera(new CameraFirstPerson(GraphicInfo));
        }
Exemplo n.º 14
0
        public void LoadPhysics()
        {
            Core  _core  = this.Core;
            Scene _scene = this.Scene;

            #region Some Boxes
            for (int x = 0; x < 5; x++)
            {
                BoxShapeDescription boxShapeDesc = new BoxShapeDescription(2, 3, 8);

                ActorDescription actorDesc = new ActorDescription()
                {
                    Name            = String.Format("Box {0}", x),
                    BodyDescription = new BodyDescription(10.0f),
                    GlobalPose      = Matrix.Translation(100, 15 + 3 * x, 20),
                    Shapes          = { boxShapeDesc }
                };

                Actor actor = _scene.CreateActor(actorDesc);
            }
            #endregion

            //#region Cloth (Flag)
            //{
            //    // Create a Grid of Points
            //    VertexGrid grid = VertexGrid.CreateGrid( 10, 10 );

            //    ClothMeshDescription clothMeshDesc = new ClothMeshDescription();
            //        clothMeshDesc.AllocateVertices<Vector3>( grid.Points.Length );
            //        clothMeshDesc.AllocateTriangles<int>( grid.Indices.Length / 3 );

            //        clothMeshDesc.VertexCount = grid.Points.Length;
            //        clothMeshDesc.TriangleCount = grid.Indices.Length / 3;

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

            //        // We are using 32 bit integers, so make sure the 16 bit flag is removed.
            //        // 32 bits are the default, so this isn't technically needed
            //        clothMeshDesc.Flags &= ~MeshFlag.Indices16Bit;

            //    // Write the cooked data to memory
            //    MemoryStream 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 clothMesh = _core.CreateClothMesh( memoryStream );

            //    //

            //    ClothDescription clothDesc = new ClothDescription()
            //    {
            //        ClothMesh = clothMesh,
            //        Flags = ClothFlag.Gravity | ClothFlag.Bending | ClothFlag.CollisionTwoway | ClothFlag.Visualization,
            //        GlobalPose =
            //            Matrix.CreateFromYawPitchRoll( 0, (float)Math.PI / 2.0f, (float)Math.PI / 2.0f ) *
            //            Matrix.CreateTranslation( 0, 20, 0 )
            //    };
            //    clothDesc.MeshData.AllocatePositions<Vector3>( grid.Points.Length );
            //    clothDesc.MeshData.AllocateIndices<int>( grid.Indices.Length );

            //    clothDesc.MeshData.MaximumVertices = grid.Points.Length;
            //    clothDesc.MeshData.MaximumIndices = grid.Indices.Length;

            //    clothDesc.MeshData.NumberOfVertices = grid.Points.Length;
            //    clothDesc.MeshData.NumberOfIndices = grid.Indices.Length;

            //    _flag = _scene.CreateCloth( clothDesc );

            //    // Flag Pole
            //    ActorDescription flagPoleActorDesc = new ActorDescription()
            //    {
            //        GlobalPose = Matrix.CreateTranslation( 0, 10, 0 ),
            //        Shapes = { new BoxShapeDescription( 1.0f, 20.0f, 1.0f ) }
            //    };

            //    Actor flagPoleActor = _scene.CreateActor( flagPoleActorDesc );

            //    _flag.AttachToShape( flagPoleActor.Shapes[ 0 ], 0 );
            //    _flag.WindAcceleration = new Vector3( 10, 10, 10 );
            //    _flag.BendingStiffness = 0.1f;
            //}
            //#endregion

            #region Revolute Joint
            {
                BoxShapeDescription boxShapeDescA = new BoxShapeDescription(3, 3, 3);
                BoxShapeDescription boxShapeDescB = new BoxShapeDescription(3, 3, 3);

                ActorDescription actorDescA = new ActorDescription()
                {
                    BodyDescription = new BodyDescription(10.0f),
                    GlobalPose      = Matrix.Translation(75, 1.5f, 55),
                    Shapes          = { boxShapeDescA }
                };
                Actor actorA = _scene.CreateActor(actorDescA);

                ActorDescription actorDescB = new ActorDescription()
                {
                    BodyDescription = new BodyDescription(10.0f),
                    GlobalPose      = Matrix.Translation(70, 1.5f, 55),
                    Shapes          = { boxShapeDescB }
                };
                Actor actorB = _scene.CreateActor(actorDescB);

                //

                RevoluteJointDescription revoluteJointDesc = new RevoluteJointDescription()
                {
                    Actor1 = actorA,
                    Actor2 = actorB,
                    Motor  = new MotorDescription(20, 20.1f, true)
                };
                revoluteJointDesc.Flags |= RevoluteJointFlag.MotorEnabled;
                revoluteJointDesc.SetGlobalAnchor(new Vector3(73.5f, 1.5f, 55));
                revoluteJointDesc.SetGlobalAxis(new Vector3(1, 0, 0));

                RevoluteJoint revoluteJoint = _scene.CreateJoint(revoluteJointDesc) as RevoluteJoint;
            }
            #endregion

            #region Prismatic Joint with Limit
            {
                Actor actorA, actorB;
                {
                    BoxShapeDescription boxShapeDesc = new BoxShapeDescription(3, 3, 3);

                    BodyDescription bodyDesc = new BodyDescription(10.0f);
                    bodyDesc.BodyFlags |= BodyFlag.Kinematic;

                    ActorDescription actorDesc = new ActorDescription()
                    {
                        BodyDescription = bodyDesc,
                        GlobalPose      = Matrix.Translation(70, 25, 65),
                        Shapes          = { boxShapeDesc }
                    };
                    actorA = _scene.CreateActor(actorDesc);
                }
                {
                    BoxShapeDescription boxShapeDesc = new BoxShapeDescription(3, 3, 3);

                    ActorDescription actorDesc = new ActorDescription()
                    {
                        BodyDescription = new BodyDescription(10.0f),
                        GlobalPose      = Matrix.Translation(70, 15, 65),
                        Shapes          = { boxShapeDesc }
                    };
                    actorB = _scene.CreateActor(actorDesc);
                }

                PrismaticJointDescription prismaticJointDesc = new PrismaticJointDescription()
                {
                    Actor1 = actorA,
                    Actor2 = actorB,
                };
                prismaticJointDesc.SetGlobalAnchor(new Vector3(70, 20, 65));
                prismaticJointDesc.SetGlobalAxis(new Vector3(0, 1, 0));

                PrismaticJoint prismaticJoint = _scene.CreateJoint(prismaticJointDesc) as PrismaticJoint;

                LimitPlane limitPlane = new LimitPlane(new Vector3(0, 1, 0), new Vector3(-30, 8, -30), 0);
                prismaticJoint.AddLimitPlane(limitPlane);
            }
            #endregion

            #region Fluid
            {
                const int maximumParticles = 1000;

                FluidEmitterDescription fluidEmitterDesc = new FluidEmitterDescription()
                {
                    DimensionX   = 0.5f,
                    DimensionY   = 0.5f,
                    Rate         = 15,
                    RelativePose = Matrix.Translation(-40, 10, 50),
                    Shape        = EmitterShape.Rectangular,
                    Type         = EmitterType.ConstantFlowRate,
                    RandomAngle  = 0.5f
                };
                fluidEmitterDesc.Flags |= (FluidEmitterFlag.Enabled | FluidEmitterFlag.Visualization);

                FluidDescription fluidDesc = new FluidDescription()
                {
                    Emitters         = { fluidEmitterDesc },
                    Flags            = FluidFlag.Enabled | FluidFlag.Visualization,
                    MaximumParticles = maximumParticles
                };
                fluidDesc.ParticleWriteData.AllocatePositionBuffer <Vector3>(maximumParticles);
                fluidDesc.ParticleWriteData.NumberOfParticles = maximumParticles;

                Fluid fluid = _scene.CreateFluid(fluidDesc);

                // Ledge
                {
                    BoxShapeDescription boxShapeDesc = new BoxShapeDescription(5, 0.1f, 5);

                    ActorDescription drainActorDesc = new ActorDescription()
                    {
                        GlobalPose = Matrix.RotationX(0.5f) * Matrix.Translation(-40, 5, 52),
                        Shapes     = { boxShapeDesc }
                    };

                    Actor drianActor = _scene.CreateActor(drainActorDesc);
                }

                // Drain
                {
                    BoxShapeDescription boxShapeDesc = new BoxShapeDescription(5, 0.1f, 5);
                    boxShapeDesc.Flags |= ShapeFlag.FluidDrain;

                    ActorDescription drainActorDesc = new ActorDescription()
                    {
                        GlobalPose = Matrix.Translation(-40, 0, 55),
                        Shapes     = { boxShapeDesc }
                    };

                    Actor drianActor = _scene.CreateActor(drainActorDesc);
                }
            }
            #endregion

            #region Force Field
            {
                BoxForceFieldShapeDescription boxForceFieldShapeDesc = new BoxForceFieldShapeDescription()
                {
                    Size = new Vector3(10, 10, 10)
                };

                ForceFieldLinearKernelDescription kernelDesc = new ForceFieldLinearKernelDescription()
                {
                    Constant = new Vector3(0, 100.0f, 0)
                };

                ForceFieldLinearKernel kernel = _scene.CreateForceFieldLinearKernel(kernelDesc);

                ForceFieldShapeGroupDescription shapeGroupDesc = new ForceFieldShapeGroupDescription()
                {
                    Shapes = { boxForceFieldShapeDesc }
                };

                ForceFieldShapeGroup shapeGroup = _scene.CreateForceFieldShapeGroup(shapeGroupDesc);

                BoxForceFieldShape boxForceFieldShape = shapeGroup.CreateShape(boxForceFieldShapeDesc) as BoxForceFieldShape;
                boxForceFieldShape.Pose = Matrix.Translation(30, 5, 0);

                ForceFieldDescription forceFieldDesc = new ForceFieldDescription()
                {
                    Kernel      = kernel,
                    ShapeGroups = { shapeGroup }
                };
                ForceField forceField = _scene.CreateForceField(forceFieldDesc);
            }
            #endregion

            #region Heightfield
            {
                int rows    = 25;
                int columns = 25;

                HeightFieldSample[] samples = new HeightFieldSample[rows * columns];
                for (int r = 0; r < rows; r++)
                {
                    for (int c = 0; c < columns; c++)
                    {
                        // Put a z and x curve together
                        double h = Math.Sin(c) * Math.Cos(r) * short.MaxValue;

                        HeightFieldSample sample = new HeightFieldSample()
                        {
                            Height           = (short)h,
                            MaterialIndex0   = 0,
                            MaterialIndex1   = 1,
                            TessellationFlag = 0
                        };

                        samples[r * columns + c] = sample;
                    }
                }

                HeightFieldDescription heightFieldDesc = new HeightFieldDescription()
                {
                    NumberOfRows    = rows,
                    NumberOfColumns = columns
                };
                heightFieldDesc.SetSamples(samples);

                HeightField heightField = _core.CreateHeightField(heightFieldDesc);

                //

                HeightFieldShapeDescription heightFieldShapeDesc = new HeightFieldShapeDescription()
                {
                    HeightField  = heightField,
                    HoleMaterial = 2,
                    // The max height of our samples is short.MaxValue and we want it to be 1
                    HeightScale = 1.0f / (float)short.MaxValue,
                    RowScale    = 3,
                    ColumnScale = 3
                };
                heightFieldShapeDesc.LocalPosition = new Vector3(-0.5f * rows * 1 * heightFieldShapeDesc.RowScale, 0, -0.5f * columns * 1 * heightFieldShapeDesc.ColumnScale);

                ActorDescription actorDesc = new ActorDescription()
                {
                    GlobalPose = Matrix.Translation(100, 0, 0),
                    Shapes     = { heightFieldShapeDesc }
                };
                Actor actor = _scene.CreateActor(actorDesc);
            }
            #endregion

            //#region Convex Mesh
            //{
            //    ModelMesh mesh = _torusModel.Meshes.First();

            //    Matrix[] transforms = new Matrix[ _torusModel.Bones.Count ];
            //    _torusModel.CopyAbsoluteBoneTransformsTo( transforms );

            //    // Gets the vertices from the mesh
            //    VertexPositionNormalTexture[] vertices = new VertexPositionNormalTexture[ mesh.MeshParts[ 0 ].NumVertices ];
            //    mesh.VertexBuffer.GetData<VertexPositionNormalTexture>( vertices );

            //    //

            //    // Allocate memory for the points and triangles
            //    var convexMeshDesc = new ConvexMeshDescription()
            //    {
            //        PointCount = vertices.Length
            //    };
            //    convexMeshDesc.Flags |= ConvexFlag.ComputeConvex;
            //    convexMeshDesc.AllocatePoints<Vector3>( vertices.Length );

            //    // Write in the points and triangles
            //    // We only want the Position component of the vertex. Also scale down the mesh
            //    foreach( VertexPositionNormalTexture vertex in vertices )
            //    {
            //        Vector3 position = Vector3.Transform( vertex.Position, Matrix.CreateScale( 0.1f, 0.1f, 0.1f ) * transforms[ 0 ] );

            //        convexMeshDesc.PointsStream.Write( position );
            //    }

            //    //

            //    // Cook to memory or to a file
            //    MemoryStream stream = new MemoryStream();
            //    //FileStream stream = new FileStream( @"Convex Mesh.cooked", FileMode.CreateNew );

            //    Cooking.InitializeCooking( new ConsoleOutputStream() );
            //    Cooking.CookConvexMesh( convexMeshDesc, stream );
            //    Cooking.CloseCooking();

            //    stream.Position = 0;

            //    ConvexMesh convexMesh = _core.CreateConvexMesh( stream );

            //    ConvexShapeDescription convexShapeDesc = new ConvexShapeDescription( convexMesh );

            //    ActorDescription actorDesc = new ActorDescription()
            //    {
            //        BodyDescription = new BodyDescription( 10.0f ),
            //        GlobalPose = Matrix.CreateTranslation( 30, 30, 0 )
            //    };
            //    actorDesc.Shapes.Add( convexShapeDesc );

            //    _torusActor = _scene.CreateActor( actorDesc );
            //}
            //#endregion

            //#region SoftBody
            //if( false ) // Enable to view soft bodies, they run slowly
            //{
            //    XmlDocument doc = new XmlDocument();
            //        doc.Load( "Teapot.xml" );

            //    // Not how NxuStream are meant to used but what ever :S
            //    Vector3[] vertices = ReadVertices( doc.SelectSingleNode( "/NXUSTREAM2/NxuPhysicsCollection/NxSoftBodyMeshDesc/vertices" ) );
            //    int[] tetrahedraSingles = ReadTetrahedra( doc.SelectSingleNode( "/NXUSTREAM2/NxuPhysicsCollection/NxSoftBodyMeshDesc/tetrahedra" ) );

            //    var softBodyMeshDesc = new SoftBodyMeshDescription()
            //    {
            //        VertexCount = vertices.Length,
            //        TetrahedraCount = tetrahedraSingles.Length / 4 // Tetrahedras come in quadruples of ints
            //    };

            //    softBodyMeshDesc.AllocateVertices<Vector3>( softBodyMeshDesc.VertexCount );
            //    softBodyMeshDesc.AllocateTetrahedra<int>( softBodyMeshDesc.TetrahedraCount ); // Note: T is an int. T is the type of each point

            //    softBodyMeshDesc.VertexStream.SetData( vertices );
            //    softBodyMeshDesc.TetrahedraStream.SetData( tetrahedraSingles );

            //    MemoryStream memoryStream = new MemoryStream();

            //    Cooking.InitializeCooking();
            //    Cooking.CookSoftBodyMesh( softBodyMeshDesc, memoryStream );
            //    Cooking.CloseCooking();

            //    memoryStream.Position = 0;

            //    SoftBodyMesh softBodyMesh = _core.CreateSoftBodyMesh( memoryStream );

            //    SoftBodyDescription desc = new SoftBodyDescription()
            //    {
            //        GlobalPose = Matrix.CreateTranslation( -30, 20, -30 ),
            //        SoftBodyMesh = softBodyMesh
            //    };
            //    desc.Flags |= SoftBodyFlag.Visualization;

            //    desc.MeshData.AllocatePositions<Vector3>( vertices.Length );
            //    desc.MeshData.AllocateIndices<int>( tetrahedraSingles.Length );

            //    SoftBody softBody = _scene.CreateSoftBody( desc );
            //}
            //#endregion

            //#region Reports
            //// Contact report
            //// When the capsule actor hits the ground make it bounce by using the conact report
            //{
            //    CapsuleShapeDescription capsuleShapeDesc = new CapsuleShapeDescription( 1, 5 );

            //    ActorDescription actorDesc = new ActorDescription()
            //    {
            //        GlobalPose = Matrix.CreateTranslation( -30, 20, 0 ),
            //        BodyDescription = new BodyDescription( 10.0f ),
            //        Name = "Report Capsule",
            //        Shapes = { capsuleShapeDesc }
            //    };

            //    _contactReportActor = _scene.CreateActor( actorDesc );

            //    _scene.SetActorPairFlags( _contactReportActor, _groundActor, ContactPairFlag.All );

            //    _scene.UserContactReport = new ContactReport( this );
            //}

            //// Trigger Reports
            //{
            //    BoxShapeDescription boxShapeDesc = new BoxShapeDescription( 15, 8, 15 );
            //        boxShapeDesc.Flags |= ( ShapeFlag.TriggerOnEnter | ShapeFlag.TriggerOnLeave );

            //    ActorDescription actorDesc = new ActorDescription()
            //    {
            //        GlobalPose = Matrix.CreateTranslation( -30, 4, 0 ),
            //        Shapes = { boxShapeDesc }
            //    };
            //    _scene.CreateActor( actorDesc );

            //    _scene.UserTriggerReport = new TriggerReport( this );
            //}

            //_scene.UserNotify = new Notify( this );
            //#endregion

            //#region Wheel
            //{
            //    _basicVehicle = new Vehicle( this );
            //}
            //#endregion

            #region Controller
            {
                ControllerManager manager = _scene.CreateControllerManager();

                CapsuleControllerDescription capsuleControllerDesc = new CapsuleControllerDescription(4, 3);

                CapsuleController capsuleController = manager.CreateController <CapsuleController>(capsuleControllerDesc);
                capsuleController.Position   = new Vector3(0, 1.5f + 2, -15);
                capsuleController.Actor.Name = "BoxController";
                capsuleController.SetCollisionEnabled(true);
            }
            #endregion
        }
        protected override void LoadContent(GraphicInfo GraphicInfo, GraphicFactory factory ,IContentManager contentManager)
        {
            PhysxPhysicWorld PhysxPhysicWorld = World.PhysicWorld as PhysxPhysicWorld;
            
            base.LoadContent(GraphicInfo, factory, contentManager);

            const int maximumParticles = 1000;
            var fluidEmitterDesc = new FluidEmitterDescription()
            {
                DimensionX = 0.5f,
                DimensionY = 0.5f,
                Rate = 15,
                RelativePose =
                    Phyx.Matrix.RotationAxis(new Phyx.Vector3(0, 1, 0), (float)Math.PI) *
                    Phyx.Matrix.Translation(-40, 10, -50),
                Shape = EmitterShape.Rectangular,
                Type = EmitterType.ConstantFlowRate,
                RandomAngle = 0.5f
            };
            fluidEmitterDesc.Flags |= (FluidEmitterFlag.Enabled | FluidEmitterFlag.Visualization);

            var fluidDesc = new FluidDescription()
            {
                Emitters = { fluidEmitterDesc },
                Flags = FluidFlag.Enabled | FluidFlag.Visualization | FluidFlag.Enabled,
                MaximumParticles = maximumParticles,
                
            };
            fluidDesc.ParticleWriteData.AllocatePositionBuffer<Vector3>(maximumParticles);
            fluidDesc.ParticleWriteData.NumberOfParticles = maximumParticles;
            
            fluid = PhysxPhysicWorld.Scene.CreateFluid(fluidDesc);

                       
            Texture2D tex = factory.GetTexture2D("Textures/Smoke");
            for (int i = 0; i < maximumParticles; i++)
            {
                Billboard3D Billboard3D = new Billboard3D(tex,Vector3.Zero,new Vector2(0.002f));
                Billboard3D.Enabled = false;
                CPUSphericalBillboardComponent.Billboards.Add(Billboard3D);
            }


            // Ledge
            {
                var boxShapeDesc = new BoxShapeDescription(5, 0.1f, 5);
                SimpleModel SimpleModel = new PloobsEngine.Modelo.SimpleModel(factory, "Model/block");
                SimpleModel.SetTexture(factory.CreateTexture2DColor(1, 1, Color.Red), TextureType.DIFFUSE);
                PhysxPhysicObject PhysxPhysicObject = new PloobsEngine.Physics.PhysxPhysicObject(boxShapeDesc,
                    (Phyx.Matrix.RotationX(-0.5f) * Phyx.Matrix.Translation(-40, 5, -52)).AsXNA(),new Vector3(5,0.1f,5));
                ForwardXNABasicShader shader = new ForwardXNABasicShader(ForwardXNABasicShaderDescription.Default());
                ForwardMaterial fmaterial = new ForwardMaterial(shader);
                IObject obj = new IObject(fmaterial, SimpleModel, PhysxPhysicObject);
                this.World.AddObject(obj);
            }

            // Drain
            {
                var boxShapeDesc = new BoxShapeDescription(5, 0.1f, 5);
                boxShapeDesc.Flags |= ShapeFlag.FluidDrain;

                var drainActorDesc = new ActorDescription()
                {
                    GlobalPose = Phyx.Matrix.Translation(-40, 0, -55),
                    Shapes = { boxShapeDesc }
                };

                var drianActor = PhysxPhysicWorld.Scene.CreateActor(drainActorDesc);
            }            

            BallThrowPhysx28 BallThrowBullet = new BallThrowPhysx28(this.World, GraphicFactory);
            this.AttachCleanUpAble(BallThrowBullet);

            this.World.CameraManager.AddCamera(new CameraFirstPerson(GraphicInfo));
        }
Exemplo n.º 16
0
        protected override void LoadContent(GraphicInfo GraphicInfo, GraphicFactory factory, IContentManager contentManager)
        {
            PhysxPhysicWorld PhysxPhysicWorld = World.PhysicWorld as PhysxPhysicWorld;

            base.LoadContent(GraphicInfo, factory, contentManager);

            const int maximumParticles = 3000;

            ///Remember
            ///There are 2 math apis (XNA AND PHYSX)
            ///Sometimes we need to convert between then
            ///Use the extension methods AsPhysx() in XNA API and AsXNA in Physx APi

            ///emitter
            var fluidEmitterDesc = new FluidEmitterDescription()
            {
                DimensionX   = 1.5f,
                DimensionY   = 1.5f,
                Rate         = 65,
                RelativePose =
                    Phyx.Matrix.RotationAxis(new Phyx.Vector3(0, 1, 0), (float)Math.PI) *
                    Phyx.Matrix.Translation(-40, 10, -50),
                Shape       = EmitterShape.Rectangular,
                Type        = EmitterType.ConstantFlowRate,
                RandomAngle = 0.5f,
            };

            fluidEmitterDesc.Flags |= (FluidEmitterFlag.Enabled | FluidEmitterFlag.Visualization);

            ///fluid
            var fluidDesc = new FluidDescription()
            {
                Emitters         = { fluidEmitterDesc },
                Flags            = FluidFlag.Enabled | FluidFlag.Visualization,
                MaximumParticles = maximumParticles,
            };

            fluidDesc.ParticleWriteData.AllocatePositionBuffer <Vector3>(maximumParticles);
            fluidDesc.ParticleWriteData.NumberOfParticles = maximumParticles;


            ///create and add the fluid to the world
            fluid = PhysxPhysicWorld.Scene.CreateFluid(fluidDesc);


            ///Use Billboards to render the fuild particles (dummy way)
            Texture2D tex = factory.GetTexture2D("Textures/Smoke");

            for (int i = 0; i < maximumParticles; i++)
            {
                Billboard3D Billboard3D = new Billboard3D(tex, Vector3.Zero, new Vector2(0.001f));
                Billboard3D.Enabled = false;
                CPUSphericalBillboardComponent.Billboards.Add(Billboard3D);
            }


            // Ledge
            {
                var         boxShapeDesc = new BoxShapeDescription(5, 0.1f, 5);
                SimpleModel SimpleModel  = new PloobsEngine.Modelo.SimpleModel(factory, "Model/block");
                SimpleModel.SetTexture(factory.CreateTexture2DColor(1, 1, Color.Red), TextureType.DIFFUSE);
                PhysxPhysicObject PhysxPhysicObject = new PloobsEngine.Physics.PhysxPhysicObject(boxShapeDesc,
                                                                                                 (Phyx.Matrix.RotationX(-0.5f) * Phyx.Matrix.Translation(-40, 5, -52)).AsXNA(), new Vector3(5, 0.1f, 5));
                ForwardXNABasicShader shader    = new ForwardXNABasicShader(ForwardXNABasicShaderDescription.Default());
                ForwardMaterial       fmaterial = new ForwardMaterial(shader);
                IObject obj = new IObject(fmaterial, SimpleModel, PhysxPhysicObject);
                this.World.AddObject(obj);
            }

            // Drain
            {
                var boxShapeDesc = new BoxShapeDescription(5, 0.1f, 5);
                boxShapeDesc.Flags |= ShapeFlag.FluidDrain;

                var drainActorDesc = new ActorDescription()
                {
                    GlobalPose = Phyx.Matrix.Translation(-40, -20, -55),
                    Shapes     = { boxShapeDesc }
                };

                var drianActor = PhysxPhysicWorld.Scene.CreateActor(drainActorDesc);
            }

            BallThrowPhysx28 BallThrowBullet = new BallThrowPhysx28(this.World, GraphicFactory);

            BallThrowBullet.ballSize = 1f;
            BallThrowBullet.Speed    = 25;
            this.AttachCleanUpAble(BallThrowBullet);

            CameraFirstPerson CameraFirstPerson = new CameraFirstPerson(GraphicInfo);

            CameraFirstPerson.Position     = new Vector3(-35, 8, -52);
            CameraFirstPerson.LeftRightRot = MathHelper.ToRadians(125);
            this.World.CameraManager.AddCamera(CameraFirstPerson);
        }
        protected override void LoadContent(GraphicInfo GraphicInfo, GraphicFactory factory, IContentManager contentManager)
        {
            PhysxPhysicWorld PhysxPhysicWorld = World.PhysicWorld as PhysxPhysicWorld;
            base.LoadContent(GraphicInfo, factory, contentManager);

            {
                SimpleModel simpleModel = new SimpleModel(factory, "Model//cenario");

                PhysxTriangleMesh tmesh = new PhysxTriangleMesh(PhysxPhysicWorld, simpleModel,
                    Matrix.Identity, Vector3.One);

                DeferredNormalShader shader = new DeferredNormalShader();
                DeferredMaterial fmaterial = new DeferredMaterial(shader);
                IObject obj = new IObject(fmaterial, simpleModel, tmesh);
                this.World.AddObject(obj);
            }

            PhysxPhysicObject frame;
            {
                var boxShapeDesc = new BoxShapeDescription(3, 10, 3);
                SimpleModel SimpleModel = new PloobsEngine.Modelo.SimpleModel(factory, "Model/block");
                SimpleModel.SetTexture(factory.CreateTexture2DColor(1, 1, Color.Red), TextureType.DIFFUSE);

                frame = new PloobsEngine.Physics.PhysxPhysicObject(boxShapeDesc, Matrix.CreateTranslation(50, 5, 50), new Vector3(3, 10, 3));
                DeferredNormalShader shader = new DeferredNormalShader();
                DeferredMaterial fmaterial = new DeferredMaterial(shader);
                IObject obj = new IObject(fmaterial, SimpleModel, frame);
                this.World.AddObject(obj);
            }

            //Create an attached emitter
            FluidEmitterDescription emitterDesc = new FluidEmitterDescription();
            emitterDesc.MaximumParticles = 0;
            emitterDesc.DimensionX = 8f;
            emitterDesc.DimensionY = 8f;
            emitterDesc.Type = EmitterType.ConstantFlowRate;
            emitterDesc.Rate = 150.0f;
            emitterDesc.FluidVelocityMagnitude = 60.0f;
            emitterDesc.ParticleLifetime = 8.0f;
            emitterDesc.Shape = EmitterShape.Rectangular;

            //attach to actor
            emitterDesc.Flags = FluidEmitterFlag.AddBodyVelocity | FluidEmitterFlag.Enabled | FluidEmitterFlag.Visualization;
            emitterDesc.RepulsionCoefficient = 0.02f;

            //emitterDesc.RelativePose = Phyx.Matrix.RotationX((float)Math.PI / 2);
            emitterDesc.RelativePose *= Phyx.Matrix.Translation(0, 20f, 0);

            emitterDesc.FrameShape = frame.Actor.Shapes[0];

            FluidDescription fluidDesc = new FluidDescription();
            fluidDesc.MaximumParticles = 2500;
            fluidDesc.KernelRadiusMultiplier = 5.0f;
            fluidDesc.RestParticlesPerMeter = 5.0f;
            fluidDesc.MotionLimitMultiplier = 0.9f;
            fluidDesc.PacketSizeMultiplier = 16;
            fluidDesc.CollisionDistanceMultiplier = 0.12f;
            fluidDesc.Stiffness = 50.0f;
            fluidDesc.Viscosity = 40.0f;
            fluidDesc.RestDensity = 1000.0f;
            fluidDesc.Damping = 0.0f;
            fluidDesc.RestitutionForStaticShapes = 0.2f;
            fluidDesc.DynamicFrictionForStaticShapes = 0.05f;
            fluidDesc.Flags = FluidFlag.Enabled | FluidFlag.Visualization;
            fluidDesc.SimulationMethod = FluidSimulationMethod.SmoothedParticleHydrodynamics;

            fluidDesc.ParticleWriteData.AllocatePositionBuffer<Phyx.Vector3>(fluidDesc.MaximumParticles);
            fluidDesc.ParticleWriteData.NumberOfParticles = fluidDesc.MaximumParticles;

            FluidMOdel FluidMOdel = new PloobsEngine.Modelo.FluidMOdel(factory, "teste", null, fluidDesc.MaximumParticles);
            PhysxFluidObject PhysxFluidObject = new PloobsEngine.Physics.PhysxFluidObject(fluidDesc);
            FluidShader FluidShader = new FluidShader();
            DeferredMaterial ForwardMaterial = new DeferredMaterial(FluidShader);
            IObject IObject = new IObject(ForwardMaterial, FluidMOdel, PhysxFluidObject);
            this.World.AddObject(IObject);

            PhysxFluidObject.Fluid.CreateFluidEmitter(emitterDesc);

            #region NormalLight
            DirectionalLightPE ld1 = new DirectionalLightPE(Vector3.Left, Color.White);
            DirectionalLightPE ld2 = new DirectionalLightPE(Vector3.Right, Color.White);
            DirectionalLightPE ld3 = new DirectionalLightPE(Vector3.Backward, Color.White);
            DirectionalLightPE ld4 = new DirectionalLightPE(Vector3.Forward, Color.White);
            DirectionalLightPE ld5 = new DirectionalLightPE(Vector3.Down, Color.White);
            float li = 0.5f;
            ld1.LightIntensity = li;
            ld2.LightIntensity = li;
            ld3.LightIntensity = li;
            ld4.LightIntensity = li;
            ld5.LightIntensity = li;
            this.World.AddLight(ld1);
            this.World.AddLight(ld2);
            this.World.AddLight(ld3);
            this.World.AddLight(ld4);
            this.World.AddLight(ld5);
            #endregion


            BallThrowPhysx28 BallThrowBullet = new BallThrowPhysx28(this.World, GraphicFactory, false);
            this.AttachCleanUpAble(BallThrowBullet);

            this.World.CameraManager.AddCamera(new CameraFirstPerson(GraphicInfo));
        }
 public PhysxFluidObject(FluidDescription FluidDescription)
 {
     this.FluidDesc = FluidDescription;                    
 }
Exemplo n.º 19
0
        protected override void LoadContent(GraphicInfo GraphicInfo, GraphicFactory factory, IContentManager contentManager)
        {
            PhysxPhysicWorld PhysxPhysicWorld = World.PhysicWorld as PhysxPhysicWorld;

            base.LoadContent(GraphicInfo, factory, contentManager);

            {
                SimpleModel simpleModel = new SimpleModel(factory, "Model//cenario");

                PhysxTriangleMesh tmesh = new PhysxTriangleMesh(PhysxPhysicWorld, simpleModel,
                                                                Matrix.Identity, Vector3.One);

                DeferredNormalShader shader    = new DeferredNormalShader();
                DeferredMaterial     fmaterial = new DeferredMaterial(shader);
                IObject obj = new IObject(fmaterial, simpleModel, tmesh);
                this.World.AddObject(obj);
            }

            PhysxPhysicObject frame;
            {
                var         boxShapeDesc = new BoxShapeDescription(3, 10, 3);
                SimpleModel SimpleModel  = new PloobsEngine.Modelo.SimpleModel(factory, "Model/block");
                SimpleModel.SetTexture(factory.CreateTexture2DColor(1, 1, Color.Red), TextureType.DIFFUSE);

                frame = new PloobsEngine.Physics.PhysxPhysicObject(boxShapeDesc, Matrix.CreateTranslation(50, 5, 50), new Vector3(3, 10, 3));
                DeferredNormalShader shader    = new DeferredNormalShader();
                DeferredMaterial     fmaterial = new DeferredMaterial(shader);
                IObject obj = new IObject(fmaterial, SimpleModel, frame);
                this.World.AddObject(obj);
            }

            //Create an attached emitter
            FluidEmitterDescription emitterDesc = new FluidEmitterDescription();

            emitterDesc.MaximumParticles       = 0;
            emitterDesc.DimensionX             = 8f;
            emitterDesc.DimensionY             = 8f;
            emitterDesc.Type                   = EmitterType.ConstantFlowRate;
            emitterDesc.Rate                   = 150.0f;
            emitterDesc.FluidVelocityMagnitude = 60.0f;
            emitterDesc.ParticleLifetime       = 8.0f;
            emitterDesc.Shape                  = EmitterShape.Rectangular;

            //attach to actor
            emitterDesc.Flags = FluidEmitterFlag.AddBodyVelocity | FluidEmitterFlag.Enabled | FluidEmitterFlag.Visualization;
            emitterDesc.RepulsionCoefficient = 0.02f;

            //emitterDesc.RelativePose = Phyx.Matrix.RotationX((float)Math.PI / 2);
            emitterDesc.RelativePose *= Phyx.Matrix.Translation(0, 20f, 0);

            emitterDesc.FrameShape = frame.Actor.Shapes[0];

            FluidDescription fluidDesc = new FluidDescription();

            fluidDesc.MaximumParticles            = 2500;
            fluidDesc.KernelRadiusMultiplier      = 5.0f;
            fluidDesc.RestParticlesPerMeter       = 5.0f;
            fluidDesc.MotionLimitMultiplier       = 0.9f;
            fluidDesc.PacketSizeMultiplier        = 16;
            fluidDesc.CollisionDistanceMultiplier = 0.12f;
            fluidDesc.Stiffness   = 50.0f;
            fluidDesc.Viscosity   = 40.0f;
            fluidDesc.RestDensity = 1000.0f;
            fluidDesc.Damping     = 0.0f;
            fluidDesc.RestitutionForStaticShapes     = 0.2f;
            fluidDesc.DynamicFrictionForStaticShapes = 0.05f;
            fluidDesc.Flags            = FluidFlag.Enabled | FluidFlag.Visualization;
            fluidDesc.SimulationMethod = FluidSimulationMethod.SmoothedParticleHydrodynamics;

            fluidDesc.ParticleWriteData.AllocatePositionBuffer <Phyx.Vector3>(fluidDesc.MaximumParticles);
            fluidDesc.ParticleWriteData.NumberOfParticles = fluidDesc.MaximumParticles;

            FluidMOdel       FluidMOdel       = new PloobsEngine.Modelo.FluidMOdel(factory, "teste", null, fluidDesc.MaximumParticles);
            PhysxFluidObject PhysxFluidObject = new PloobsEngine.Physics.PhysxFluidObject(fluidDesc);
            FluidShader      FluidShader      = new FluidShader();
            DeferredMaterial ForwardMaterial  = new DeferredMaterial(FluidShader);
            IObject          IObject          = new IObject(ForwardMaterial, FluidMOdel, PhysxFluidObject);

            this.World.AddObject(IObject);

            PhysxFluidObject.Fluid.CreateFluidEmitter(emitterDesc);

            #region NormalLight
            DirectionalLightPE ld1 = new DirectionalLightPE(Vector3.Left, Color.White);
            DirectionalLightPE ld2 = new DirectionalLightPE(Vector3.Right, Color.White);
            DirectionalLightPE ld3 = new DirectionalLightPE(Vector3.Backward, Color.White);
            DirectionalLightPE ld4 = new DirectionalLightPE(Vector3.Forward, Color.White);
            DirectionalLightPE ld5 = new DirectionalLightPE(Vector3.Down, Color.White);
            float li = 0.5f;
            ld1.LightIntensity = li;
            ld2.LightIntensity = li;
            ld3.LightIntensity = li;
            ld4.LightIntensity = li;
            ld5.LightIntensity = li;
            this.World.AddLight(ld1);
            this.World.AddLight(ld2);
            this.World.AddLight(ld3);
            this.World.AddLight(ld4);
            this.World.AddLight(ld5);
            #endregion


            BallThrowPhysx28 BallThrowBullet = new BallThrowPhysx28(this.World, GraphicFactory, false);
            this.AttachCleanUpAble(BallThrowBullet);

            this.World.CameraManager.AddCamera(new CameraFirstPerson(GraphicInfo));
        }
Exemplo n.º 20
0
		protected override void LoadPhysics(Scene scene)
		{
			// Create a simple fluid description with fluids and visualization enabled
			FluidDescription fluidDesc = new FluidDescription()
			{
				Flags = FluidFlag.Enabled | FluidFlag.Visualization,
			};

			// Store our particle positions somewhere (as our particle generation function below generates and unknown number of particles at runtime we need a list instead of an array)
			var particlePositions = new List<Vector3>();

			// Move all the particles by this offset
			Vector3 position = new Vector3( 0, 20, 0 );

			// Number of particles in the x, y and z directions
			int sideNum = 10;
			float distance = 1f;

			float radius = sideNum * distance * 0.5f;

			for( int i = 0; i < sideNum; i++ )
			{
				for( int j = 0; j < sideNum; j++ )
				{
					for( int k = 0; k < sideNum; k++ )
					{
						Vector3 p = new Vector3( i * distance, j * distance, k * distance );

						if( ( p - new Vector3( radius, radius, radius ) ).Length() < radius )
						{
							p += position - new Vector3( radius, radius, radius );

							particlePositions.Add( p );
						}
					}
				}
			}

			// Allocate memory for the initial particle positions to be stored in
			// And then set the position buffer
			fluidDesc.InitialParticleData.AllocatePositionBuffer<Vector3>( particlePositions.Count );
			fluidDesc.InitialParticleData.NumberOfParticles = particlePositions.Count;
			fluidDesc.InitialParticleData.PositionBuffer.SetData( particlePositions.ToArray() );

			// Allocate memory for PhysX to store the position of each particle
			fluidDesc.ParticleWriteData.AllocatePositionBuffer<Vector3>( particlePositions.Count );
			fluidDesc.ParticleWriteData.NumberOfParticles = particlePositions.Count;

			Fluid fluid = scene.CreateFluid(fluidDesc);

			//

			// Make a basic box (ramp) for the particle to slide down
			ActorDescription rampDesc = new ActorDescription()
			{
				Shapes = { new BoxShapeDescription( 10, 0.2f, 10 ) },
				GlobalPose =
					Matrix.RotationZ( 0.3f ) *
					Matrix.Translation( 0, 5, 0 )
			};

			Actor ramp = scene.CreateActor(rampDesc);
		}
        protected override void LoadContent(GraphicInfo GraphicInfo, GraphicFactory factory ,IContentManager contentManager)
        {
            PhysxPhysicWorld PhysxPhysicWorld = World.PhysicWorld as PhysxPhysicWorld;
            
            base.LoadContent(GraphicInfo, factory, contentManager);

            const int maximumParticles = 1000;
            var fluidEmitterDesc = new FluidEmitterDescription()
            {
                DimensionX = 0.5f,
                DimensionY = 0.5f,
                Rate = 15,
                RelativePose =
                    Phyx.Matrix.RotationAxis(new Phyx.Vector3(0, 1, 0), (float)Math.PI) *
                    Phyx.Matrix.Translation(-40, 10, -50),
                Shape = EmitterShape.Rectangular,
                Type = EmitterType.ConstantFlowRate,
                RandomAngle = 0.5f
            };
            fluidEmitterDesc.Flags |= (FluidEmitterFlag.Enabled | FluidEmitterFlag.Visualization);

            var fluidDesc = new FluidDescription()
            {
                Emitters = { fluidEmitterDesc },
                Flags = FluidFlag.Enabled | FluidFlag.Visualization | FluidFlag.Enabled,
                MaximumParticles = maximumParticles,
                
            };
            fluidDesc.ParticleWriteData.AllocatePositionBuffer<Vector3>(maximumParticles);
            fluidDesc.ParticleWriteData.NumberOfParticles = maximumParticles;

            InstancedBilboardModel InstancedBilboardModel = new InstancedBilboardModel(factory, "teste", "Textures/Smoke", new BilboardInstance[] {new BilboardInstance() }, maximumParticles);
            PhysxFluidObject PhysxFluidObject = new PloobsEngine.Physics.PhysxFluidObject(fluidDesc);
            DeferredInstancedBilboardShader DeferredInstancedBilboardShader = new PloobsEngine.Material.DeferredInstancedBilboardShader(BilboardType.Spherical);
            DeferredInstancedBilboardShader.AlphaTestLimit = 0.2f;
            FluidMaterial DeferredMaterial = new FluidMaterial(DeferredInstancedBilboardShader,maximumParticles,new Vector2(0.2f));
            IObject IObject = new IObject(DeferredMaterial, InstancedBilboardModel, PhysxFluidObject);
            this.World.AddObject(IObject);


            // Ledge
            {
                var boxShapeDesc = new BoxShapeDescription(5, 0.1f, 5);
                SimpleModel SimpleModel = new PloobsEngine.Modelo.SimpleModel(factory, "Model/block");
                SimpleModel.SetTexture(factory.CreateTexture2DColor(1, 1, Color.Red), TextureType.DIFFUSE);

                PhysxPhysicObject PhysxPhysicObject = new PloobsEngine.Physics.PhysxPhysicObject(boxShapeDesc,
                    (Phyx.Matrix.RotationX(-0.5f) * Phyx.Matrix.Translation(-40, 5, -52)).AsXNA(),new Vector3(5,0.1f,5));
                DeferredNormalShader shader = new DeferredNormalShader();
                DeferredMaterial fmaterial = new DeferredMaterial(shader);
                IObject obj = new IObject(fmaterial, SimpleModel, PhysxPhysicObject);
                this.World.AddObject(obj);
            }

            // Drain
            {
                var boxShapeDesc = new BoxShapeDescription(5, 0.1f, 5);
                boxShapeDesc.Flags |= ShapeFlag.FluidDrain;

                var drainActorDesc = new ActorDescription()
                {
                    GlobalPose = Phyx.Matrix.Translation(-40, 0, -55),
                    Shapes = { boxShapeDesc }
                };

                var drianActor = PhysxPhysicWorld.Scene.CreateActor(drainActorDesc);
            }

            #region NormalLight
            DirectionalLightPE ld1 = new DirectionalLightPE(Vector3.Left, Color.White);
            DirectionalLightPE ld2 = new DirectionalLightPE(Vector3.Right, Color.White);
            DirectionalLightPE ld3 = new DirectionalLightPE(Vector3.Backward, Color.White);
            DirectionalLightPE ld4 = new DirectionalLightPE(Vector3.Forward, Color.White);
            DirectionalLightPE ld5 = new DirectionalLightPE(Vector3.Down, Color.White);
            float li = 0.5f;
            ld1.LightIntensity = li;
            ld2.LightIntensity = li;
            ld3.LightIntensity = li;
            ld4.LightIntensity = li;
            ld5.LightIntensity = li;
            this.World.AddLight(ld1);
            this.World.AddLight(ld2);
            this.World.AddLight(ld3);
            this.World.AddLight(ld4);
            this.World.AddLight(ld5);
            #endregion


            BallThrowPhysx28 BallThrowBullet = new BallThrowPhysx28(this.World, GraphicFactory,false);
            this.AttachCleanUpAble(BallThrowBullet);

            this.World.CameraManager.AddCamera(new CameraFirstPerson(GraphicInfo));
        }
        protected override void LoadContent(GraphicInfo GraphicInfo, GraphicFactory factory ,IContentManager contentManager)
        {
            PhysxPhysicWorld PhysxPhysicWorld = World.PhysicWorld as PhysxPhysicWorld;            
            base.LoadContent(GraphicInfo, factory, contentManager);

            // Create a simple fluid description with fluids and visualization enabled
            FluidDescription fluidDesc = new FluidDescription()
            {
                Flags = FluidFlag.Enabled | FluidFlag.Visualization,
            };

            // Store our particle positions somewhere (as our particle generation function below generates and unknown number of particles at runtime we need a list instead of an array)
            var particlePositions = new List<Phyx.Vector3>();

            // Move all the particles by this offset
            Phyx.Vector3 position = new Phyx.Vector3(0, 20, 0);

            // Number of particles in the x, y and z directions
            int sideNum = 10;
            float distance = 1f;

            float radius = sideNum * distance * 0.5f;

            for (int i = 0; i < sideNum; i++)
            {
                for (int j = 0; j < sideNum; j++)
                {
                    for (int k = 0; k < sideNum; k++)
                    {
                        Phyx.Vector3 p = new Phyx.Vector3(i * distance, j * distance, k * distance);

                        if ((p - new Phyx.Vector3(radius, radius, radius)).Length() < radius)
                        {
                            p += position - new Phyx.Vector3(radius, radius, radius);

                            particlePositions.Add(p);
                        }
                    }
                }
            }

            // Allocate memory for the initial particle positions to be stored in
            // And then set the position buffer
            fluidDesc.InitialParticleData.AllocatePositionBuffer<Phyx.Vector3>(particlePositions.Count);
            fluidDesc.InitialParticleData.NumberOfParticles = particlePositions.Count;
            fluidDesc.InitialParticleData.PositionBuffer.SetData(particlePositions.ToArray());

            // Allocate memory for PhysX to store the position of each particle
            fluidDesc.ParticleWriteData.AllocatePositionBuffer<Phyx.Vector3>(particlePositions.Count);
            fluidDesc.ParticleWriteData.NumberOfParticles = particlePositions.Count;

            InstancedBilboardModel InstancedBilboardModel = new InstancedBilboardModel(factory, "teste", "Textures/Smoke", new BilboardInstance[] { new BilboardInstance() }, particlePositions.Count);
            PhysxFluidObject PhysxFluidObject = new PloobsEngine.Physics.PhysxFluidObject(fluidDesc);
            DeferredInstancedBilboardShader DeferredInstancedBilboardShader = new PloobsEngine.Material.DeferredInstancedBilboardShader(BilboardType.Spherical);
            DeferredInstancedBilboardShader.AlphaTestLimit = 0.2f;
            FluidMaterial DeferredMaterial = new FluidMaterial(DeferredInstancedBilboardShader, particlePositions.Count, new Vector2(0.2f));
            IObject IObject = new IObject(DeferredMaterial, InstancedBilboardModel, PhysxFluidObject);
            this.World.AddObject(IObject);


            // Ledge
            {
                var boxShapeDesc = new BoxShapeDescription(10, 0.2f, 10);
                SimpleModel SimpleModel = new PloobsEngine.Modelo.SimpleModel(factory, "Model/block");
                SimpleModel.SetTexture(factory.CreateTexture2DColor(1, 1, Color.Red), TextureType.DIFFUSE);

                PhysxPhysicObject PhysxPhysicObject = new PloobsEngine.Physics.PhysxPhysicObject(boxShapeDesc,
                    (Phyx.Matrix.RotationZ(0.3f) *
                        Phyx.Matrix.Translation(0, 5, 0)).AsXNA(), new Vector3(10, 0.2f, 10));
                DeferredNormalShader shader = new DeferredNormalShader();
                DeferredMaterial fmaterial = new DeferredMaterial(shader);
                IObject obj = new IObject(fmaterial, SimpleModel, PhysxPhysicObject);
                this.World.AddObject(obj);                        
            }

            #region NormalLight
            DirectionalLightPE ld1 = new DirectionalLightPE(Vector3.Left, Color.White);
            DirectionalLightPE ld2 = new DirectionalLightPE(Vector3.Right, Color.White);
            DirectionalLightPE ld3 = new DirectionalLightPE(Vector3.Backward, Color.White);
            DirectionalLightPE ld4 = new DirectionalLightPE(Vector3.Forward, Color.White);
            DirectionalLightPE ld5 = new DirectionalLightPE(Vector3.Down, Color.White);
            float li = 0.5f;
            ld1.LightIntensity = li;
            ld2.LightIntensity = li;
            ld3.LightIntensity = li;
            ld4.LightIntensity = li;
            ld5.LightIntensity = li;
            this.World.AddLight(ld1);
            this.World.AddLight(ld2);
            this.World.AddLight(ld3);
            this.World.AddLight(ld4);
            this.World.AddLight(ld5);
            #endregion


            BallThrowPhysx28 BallThrowBullet = new BallThrowPhysx28(this.World, GraphicFactory,false);
            this.AttachCleanUpAble(BallThrowBullet);

            this.World.CameraManager.AddCamera(new CameraFirstPerson(GraphicInfo));
        }