Esempio n. 1
0
        public void Destroying_parents_sanity_scheck()
        {
            var scene      = new Scene(null);
            var parent     = scene.AddActor("Peter Parent", new Vector2(80, 80));
            var child1     = scene.AddActor("Carrie Child", parent.transform.Position + new Vector2(50, 50));
            var child2     = scene.AddActor("Caleb Child", parent.transform.Position + new Vector2(-30, 200));
            var grandChild = scene.AddActor("Garry Grandchild", child1.transform.Position + new Vector2(-20, 40));

            child1.transform.SetParent(parent);
            child2.transform.SetParent(parent);
            grandChild.transform.SetParent(child1);
            scene.FlushBuffers();

            Assert.Equal(2, parent.transform.ChildCount);
            Assert.Equal(parent.transform.Position + new Vector2(50, 50),
                         child1.transform.Position);                             // Position is preserved after setting parent (child1)
            Assert.Equal(parent.transform.Position + new Vector2(-30, 200),
                         child2.transform.Position);                             // Position is preserved after setting parent (child2)
            Assert.Equal(child1.transform.Position + new Vector2(-20, 40),
                         grandChild.transform.Position);                         // Position is preserved after setting parent (grandChild)
            Assert.Equal(new Vector2(50, 50), child1.transform.LocalPosition);   // Local position sanity check (child1)
            Assert.Equal(new Vector2(-30, 200), child2.transform.LocalPosition); // Local position sanity check (child2)
            Assert.Equal(new Vector2(-20, 40),
                         grandChild.transform.LocalPosition);                    // Local position sanity check (grandchild)
            Assert.Equal(parent.transform.LocalPosition,
                         parent.transform
                         .Position); // If an actor has no parent, its local position is equivalent to its position
        }
        private void CreateFunnel(Scene scene)
        {
            // Create two boxes that angle inwards to create a funnel

            var material = scene.Physics.CreateMaterial(0.1f, 0.1f, 0.1f);

            var rigidActor = scene.Physics.CreateRigidDynamic();

            var boxGeom  = new BoxGeometry(10, 0.25f, 5);
            var boxShape = rigidActor.CreateShape(boxGeom, material);

            rigidActor.GlobalPose =
                Matrix.RotationX(0.2f) *
                Matrix.Translation(0, 7, -5.1f);
            rigidActor.SetMassAndUpdateInertia(10);
            rigidActor.Flags = RigidDynamicFlags.Kinematic;

            scene.AddActor(rigidActor);

            //

            var rigidActor2 = scene.Physics.CreateRigidDynamic();

            var boxGeom2  = new BoxGeometry(10, 0.25f, 5);
            var boxShape2 = rigidActor2.CreateShape(boxGeom2, material);

            rigidActor2.GlobalPose =
                Matrix.RotationX(-0.2f) *
                Matrix.Translation(0, 7, 5.1f);
            rigidActor2.SetMassAndUpdateInertia(10);
            rigidActor2.Flags = RigidDynamicFlags.Kinematic;

            scene.AddActor(rigidActor2);
        }
Esempio n. 3
0
		protected override void LoadPhysics(Scene scene)
		{
			var material = scene.Physics.CreateMaterial(0.7f, 0.7f, 0.1f);

			var boxA = scene.Physics.CreateRigidDynamic();
			boxA.Name = "Box A";
			boxA.GlobalPose = Matrix4x4.CreateTranslation(0, 50, 0);

			var shapeA = boxA.CreateShape(new BoxGeometry(2, 2, 2), material);

			scene.AddActor(boxA);

			//

			var boxB = scene.Physics.CreateRigidDynamic();
			boxB.Name = "Box B";
			boxB.GlobalPose = Matrix4x4.CreateTranslation(0, 4, 0);

			var shapeB = boxB.CreateShape(new BoxGeometry(2, 2, 2), material);

			scene.AddActor(boxB);

			//

			// Tell PhysX what to call when a contact/touch occurs
			var callback = new EventCallback(this);
			scene.SetSimulationEventCallback(callback, 0);
		}
Esempio n. 4
0
        protected override void LoadPhysics(Scene scene)
        {
            var material = scene.Physics.CreateMaterial(0.7f, 0.7f, 0.1f);

            var boxA = scene.Physics.CreateRigidDynamic();

            boxA.Name       = "Box A";
            boxA.GlobalPose = Matrix4x4.CreateTranslation(0, 50, 0);

            var shapeA = boxA.CreateShape(new BoxGeometry(2, 2, 2), material);

            scene.AddActor(boxA);

            //

            var boxB = scene.Physics.CreateRigidDynamic();

            boxB.Name       = "Box B";
            boxB.GlobalPose = Matrix4x4.CreateTranslation(0, 4, 0);

            var shapeB = boxB.CreateShape(new BoxGeometry(2, 2, 2), material);

            scene.AddActor(boxB);

            //

            // Tell PhysX what to call when a contact/touch occurs
            var callback = new EventCallback(this);

            scene.SetSimulationEventCallback(callback, 0);
        }
Esempio n. 5
0
        public void Assign_child_world_position()
        {
            var scene  = new Scene(null);
            var parent = scene.AddActor("Peter Parent", new Vector2(80, 80));
            var child  = scene.AddActor("Carrie Child", parent.transform.Position + new Vector2(100, 0));

            child.transform.SetParent(parent);
            scene.FlushBuffers();
            child.transform.Position = new Vector2(-50, -50);

            Assert.Equal(new Vector2(-50, -50), child.transform.Position); // Child gets set to assigned world position
        }
Esempio n. 6
0
        public void Assign_child_local_position()
        {
            var scene  = new Scene(null);
            var parent = scene.AddActor("Peter Parent", new Vector2(80, 80));
            var child  = scene.AddActor("Carrie Child", parent.transform.Position + new Vector2(100, 0));

            child.transform.SetParent(parent);
            scene.FlushBuffers();
            child.transform.LocalPosition = new Vector2(-20, -20);

            Assert.Equal(new Vector2(-20, -20), child.transform.LocalPosition); // Local position was set
            Assert.Equal(new Vector2(60, 60), child.transform.Position);        // Global position was set
        }
Esempio n. 7
0
		private void CreateFunnel(Scene scene)
		{
			// Create two boxes that angle inwards to create a funnel

			var material = scene.Physics.CreateMaterial(0.1f, 0.1f, 0.1f);

			var rigidActor = scene.Physics.CreateRigidDynamic();

			var boxGeom = new BoxGeometry(10, 0.25f, 5);
			var boxShape = rigidActor.CreateShape(boxGeom, material);

			rigidActor.GlobalPose =
				Matrix4x4.CreateRotationX(0.2f) *
				Matrix4x4.CreateTranslation(0, 7, -5.1f);
			rigidActor.SetMassAndUpdateInertia(10);
			rigidActor.Flags = RigidDynamicFlags.Kinematic;

			scene.AddActor(rigidActor);

			//

			var rigidActor2 = scene.Physics.CreateRigidDynamic();

			var boxGeom2 = new BoxGeometry(10, 0.25f, 5);
			var boxShape2 = rigidActor2.CreateShape(boxGeom2, material);

			rigidActor2.GlobalPose =
				Matrix4x4.CreateRotationX(-0.2f) *
				Matrix4x4.CreateTranslation(0, 7, 5.1f);
			rigidActor2.SetMassAndUpdateInertia(10);
			rigidActor2.Flags = RigidDynamicFlags.Kinematic;

			scene.AddActor(rigidActor2);
		}
Esempio n. 8
0
		protected override void LoadPhysics(Scene scene)
		{
			var material = scene.Physics.CreateMaterial(0.7f, 0.7f, 0.1f);

			// Spherical Joint
			{
				// Actor 0
				var rigidActor0 = scene.Physics.CreateRigidDynamic();
				rigidActor0.CreateShape(new BoxGeometry(2, 2, 2), material);

				rigidActor0.GlobalPose = Matrix.Translation(0, 10, 0);
				rigidActor0.SetMassAndUpdateInertia(10);



				// Actor 1
				//var rigidActor1 = scene.Physics.CreateRigidDynamic();
				//rigidActor1.CreateShape(new BoxGeometry(2, 2, 2), material);

				//rigidActor1.GlobalPose = Matrix.Translation(-20, 10, 0);
				//rigidActor1.SetMassAndUpdateInertia(10);

				//rigidActor1.Flags = RigidDynamicFlags.Kinematic; // Pin this actor in space

				//scene.AddActor(rigidActor1);

				//

				var sphericalJoint = scene.CreateJoint<SphericalJoint>(rigidActor0, Matrix.Translation(0, 4, 0), null, Matrix.Translation(0, 15, 0));

				sphericalJoint.ConstraintFlag = ConstraintFlag.Visualization;

				scene.AddActor(rigidActor0);
			}
		}
        private void CreateHeightField(Scene scene, Material material)
        {
            const int   rows = 25, columns = 25;
            const float scale = 3;

            var samples = CreateSampleGrid(rows, columns);

            var heightFieldDesc = new HeightFieldDesc()
            {
                NumberOfRows    = rows,
                NumberOfColumns = columns,
                Samples         = samples
            };

            HeightField heightField = scene.Physics.CreateHeightField(heightFieldDesc);

            //

            var rigidActor = scene.Physics.CreateRigidStatic();

            var heightFieldGeom = new HeightFieldGeometry(heightField, MeshGeometryFlag.DoubleSided, 1, scale, scale);

            rigidActor.CreateShape(heightFieldGeom, material);

            rigidActor.GlobalPose = Matrix.Translation(30, 30, -32.5f);

            scene.AddActor(rigidActor);
        }
Esempio n. 10
0
        private void CreateHeightField(Scene scene, Material material)
        {
            const int   rows = 25, columns = 25;
            const float scale = 3;

            var samples = CreateSampleGrid(rows, columns);

            var heightFieldDesc = new HeightFieldDesc()
            {
                NumberOfRows    = rows,
                NumberOfColumns = columns,
                Samples         = samples
            };

            var cooking = scene.Physics.CreateCooking();

            var  stream     = new MemoryStream();
            bool cookResult = cooking.CookHeightField(heightFieldDesc, stream);

            stream.Position = 0;

            HeightField heightField = scene.Physics.CreateHeightField(stream);

            //

            var rigidActor = scene.Physics.CreateRigidStatic();

            var heightFieldGeom = new HeightFieldGeometry(heightField, MeshGeometryFlag.DoubleSided, 1, scale, scale);

            rigidActor.CreateShape(heightFieldGeom, material);

            rigidActor.GlobalPose = Matrix4x4.CreateTranslation(30, 30, -32.5f);

            scene.AddActor(rigidActor);
        }
Esempio n. 11
0
        public void complex_horizontal_layout_using_uibuilder()
        {
            var scene            = new Scene(null);
            var uiBuilder        = new UIBuilder(UIStyle.Empty);
            var horizontalLayout = scene.AddActor("Layout");

            new BoundingRect(horizontalLayout, 256, 128);
            var uiGroup = new LayoutGroup(horizontalLayout, Orientation.Horizontal);

            uiGroup.SetPaddingBetweenElements(5);
            uiGroup.SetMarginSize(new Point(15, 15));
            Actor e1 = null;
            Actor e2 = null;
            Actor e3 = null;

            uiGroup.AddElement("e1", new Point(32, 32), act => e1 = act);
            uiGroup.AddElement("e2", new Point(64, 32), act => e2 = act).StretchVertically();
            uiGroup.AddElement("e3", new Point(32, 32), act => e3 = act).StretchHorizontally().StretchVertically();
            scene.FlushBuffers();
            uiGroup.ExecuteLayout();

            Assert.Equal(15, e1.transform.Position.X);
            Assert.Equal(52, e2.transform.Position.X);
            Assert.Equal(121, e3.transform.Position.X);

            Assert.Equal(32, e1.GetComponent <BoundingRect>().Width);
            Assert.Equal(64, e2.GetComponent <BoundingRect>().Width);
            Assert.Equal(120, e3.GetComponent <BoundingRect>().Width);

            Assert.Equal(32, e1.GetComponent <BoundingRect>().Height);
            Assert.Equal(98, e2.GetComponent <BoundingRect>().Height);
            Assert.Equal(98, e3.GetComponent <BoundingRect>().Height);
        }
        protected override void LoadPhysics(Scene scene)
        {
            var material = scene.Physics.CreateMaterial(0.7f, 0.7f, 0.1f);

            // Spherical Joint
            {
                // Actor 0
                var rigidActor0 = scene.Physics.CreateRigidDynamic();
                rigidActor0.CreateShape(new BoxGeometry(2, 2, 2), material);

                rigidActor0.GlobalPose = Matrix.Translation(0, 10, 0);
                rigidActor0.SetMassAndUpdateInertia(10);



                // Actor 1
                //var rigidActor1 = scene.Physics.CreateRigidDynamic();
                //rigidActor1.CreateShape(new BoxGeometry(2, 2, 2), material);

                //rigidActor1.GlobalPose = Matrix.Translation(-20, 10, 0);
                //rigidActor1.SetMassAndUpdateInertia(10);

                //rigidActor1.Flags = RigidDynamicFlags.Kinematic; // Pin this actor in space

                //scene.AddActor(rigidActor1);

                //

                var sphericalJoint = scene.CreateJoint <SphericalJoint>(rigidActor0, Matrix.Translation(0, 4, 0), null, Matrix.Translation(0, 15, 0));

                sphericalJoint.ConstraintFlag = ConstraintFlag.Visualization;

                scene.AddActor(rigidActor0);
            }
        }
Esempio n. 13
0
        private Hoverable BuildHoverable(Scene scene, Point startingPosition, string name, Depth depth)
        {
            var actor = scene.AddActor(name, startingPosition.ToVector2());

            actor.transform.Depth = depth;
            new BoundingRect(actor, new Point(20, 20)).SetOffsetToCenter();
            return(new Hoverable(actor));
        }
Esempio n. 14
0
        public void Child_becomes_orphan()
        {
            var scene  = new Scene(null);
            var parent = scene.AddActor("Peter Parent", new Vector2(80, 80));
            var child  = scene.AddActor("Carrie Child", parent.transform.Position + new Vector2(100, 0));

            child.transform.SetParent(parent);
            scene.FlushBuffers();

            child.transform.Position = new Vector2(300, 300);
            child.transform.SetParent(null);
            scene.FlushBuffers();

            Assert.Equal(new Vector2(300, 300), child.transform.Position); // Child is not moved by unsetting its parent
            Assert.Null(child.transform.Parent);                           // Child's parent is null
            Assert.Equal(2, scene.GetAllActors().Count);                   // Scene is aware of both actors
        }
Esempio n. 15
0
        public void Set_parent_to_self_is_null()
        {
            var scene  = new Scene(null);
            var parent = scene.AddActor("Peter Parent", new Vector2(80, 80));

            parent.transform.SetParent(parent);

            Assert.Null(parent.transform.Parent);
        }
Esempio n. 16
0
        public void Rotate_parent_and_then_gain_child()
        {
            var scene  = new Scene(null);
            var parent = scene.AddActor("Peter Parent", new Vector2(80, 80));
            var child  = scene.AddActor("Carrie Child", parent.transform.Position + new Vector2(100, 0));

            parent.transform.Angle = MathF.PI / 2;
            child.transform.SetParent(parent);
            scene.FlushBuffers();

            Assert.Equal(new Vector2(180, 80), child.transform.Position); // Child position is unaffected
            Assert.Equal(0, child.transform.Angle);                       // Child does not inherit angle

            Assert.True(
                new Vector2(0, -100).ApproximateEqual(child.transform
                                                      .LocalPosition)); // Child local position takes rotation into account
            Assert.Equal(-MathF.PI / 2, child.transform.LocalAngle);    // Child local rotation takes rotation into account
        }
Esempio n. 17
0
        private Cloth CreateCloth(Scene scene, RigidActor poleActor)
        {
            // Create a grid of triangles to be our cloth
            var clothGrid = VertexGrid.CreateGrid(40, 40, 0.4f);

            // Setup the grid for cooking
            var clothMeshDesc = new ClothMeshDesc()
            {
                Points    = clothGrid.Points,
                Triangles = ArrayUtil.ToByteArray(clothGrid.Indices)
            };

            // Cook
            var clothFabricStream = new MemoryStream();

            using (var cooking = scene.Physics.CreateCooking())
            {
                cooking.CookClothFabric(clothMeshDesc, new Vector3(0, -1, 0), clothFabricStream);
            }

            // Reset the seek position of the stream so we can read it form the beginning
            clothFabricStream.Position = 0;

            var clothFabric = scene.Physics.CreateClothFabric(clothFabricStream);

            var poleBoxGeom = poleActor.GetShape(0).GetBoxGeometry();
            var boxPosition = new Vector3(poleActor.GlobalPose.M41, poleActor.GlobalPose.M42, poleActor.GlobalPose.M43);

            var particles = from p in clothGrid.Points
                            select new ClothParticle()
            {
                Position = p,
                // Setting the inverse weight of a particle to 0 will pin it in place, any other value will be its weight
                InverseWeight = IsPointInBox(poleBoxGeom, boxPosition, p) ? 0 : 1
            };

            // Create the cloth mesh from the cooked stream
            var cloth = scene.Physics.CreateCloth
                        (
                Matrix4x4.CreateTranslation(0, 30, 0),
                clothFabric,
                particles.ToArray(),
                0
                        );

            // Enable collision with other scene geometry
            //cloth.Flags |= ClothFlag.SceneCollision;

            // GPU cloth if desired
            // The Engine class needs to create a CudaContextManager for this to work. Define 'GPU' on
            // the Engine project to enable it.
            cloth.Flags |= ClothFlag.GPU;

            scene.AddActor(cloth);

            return(cloth);
        }
Esempio n. 18
0
        public void Rotate_parent_with_child()
        {
            var scene  = new Scene(null);
            var parent = scene.AddActor("Peter Parent", new Vector2(80, 80));
            var child  = scene.AddActor("Carrie Child", parent.transform.Position + new Vector2(100, 0));

            child.transform.SetParent(parent);
            scene.FlushBuffers();
            parent.transform.Angle = MathF.PI / 2;

            Assert.True(
                new Vector2(80, 180).ApproximateEqual(child.transform
                                                      .Position)); // Child position has been changed relative to rotation
            Assert.Equal(MathF.PI / 2, child.transform.Angle);     // Child inherits angle

            Assert.Equal(new Vector2(100, 0),
                         child.transform.LocalPosition);  // Child local position is same relative position as the start
            Assert.Equal(0f, child.transform.LocalAngle); // Child local rotation is zero
        }
Esempio n. 19
0
        public LayoutActors(Scene scene, IBakedLayout layout, Point position = default)
        {
            var actorName = layout.OriginalRoot.Name.Text;

            this.rootActor = scene.AddActor(actorName);
            new BoundingRect(this.rootActor, layout.GetNode(actorName).Size);

            AddActorToTable(actorName, this.rootActor);
            SetupRootActor(this.rootActor, actorName, layout, position.ToVector2());
            CreateActorsForChildren(layout.OriginalRoot, layout);
        }
Esempio n. 20
0
        public void Count_children()
        {
            var scene   = new Scene(null);
            var parent  = scene.AddActor("Peter Parent", new Vector2(80, 80));
            var parent2 = scene.AddActor("Otto Other Parent", new Vector2(-80, -80));
            var child   = scene.AddActor("Carrie Child", parent.transform.Position + new Vector2(100, 0));

            child.transform.SetParent(parent);
            child.transform.SetParent(parent2);
            child.transform.SetParent(parent2);
            child.transform.SetParent(parent);
            child.transform.SetParent(parent2);
            scene.FlushBuffers();

            Assert.Equal(0, parent.transform.ChildCount);  // First parent has zero children during update
            Assert.Equal(1, parent2.transform.ChildCount); // Second parent has 1 child during update

            Assert.Equal(0, parent.transform.ChildCount);  // First parent has zero children after update
            Assert.Equal(1, parent2.transform.ChildCount); // Second parent has 1 child after update
        }
Esempio n. 21
0
        public void Destroying_actor_destroys_component()
        {
            var scene       = new Scene(null);
            var actor       = scene.AddActor("Terry Triangle");
            var deleteCount = 0;

            new FakeComponent(actor, () => { deleteCount++; });
            actor.Destroy();
            scene.FlushBuffers();

            Assert.Equal(1, deleteCount);
        }
Esempio n. 22
0
        public static void SpawnJellyfish(Scene gameScene, LevelTransition levelTransition)
        {
            var sign           = MachinaGame.Random.CleanRandom.NextDouble() < 0.5f ? 1 : 1;
            var x              = gameScene.camera.ViewportWidth * 1.5f * sign;
            var jellyFishActor = gameScene.AddActor("Jellyfish", new Vector2(x, levelTransition.transform.Position.Y));

            new BubbleSpawner(jellyFishActor, new Machina.Data.MinMax <int>(5, 7));
            new Fish(jellyFishActor, levelTransition.transform, FishStats.jellyfish);
            new Jellyfish(jellyFishActor, levelTransition);
            new JellyfishRenderer(jellyFishActor);
            new NewTargetOffsetWhenReachesCurrent(jellyFishActor);
        }
Esempio n. 23
0
        public static void SpawnSeaweed(Scene gameScene, Vector2 position, SeaweedInfo seaweedInfo, LevelTransition levelTransition)
        {
            var yOffset = gameScene.camera.ViewportHeight / 2;
            var x       = gameScene.camera.ViewportWidth * seaweedInfo.XPercent;

            var seaweedActor = gameScene.AddActor("Seaweed", new Vector2(x, position.Y + yOffset + seaweedInfo.Length * 2), -MathF.PI / 2 + seaweedInfo.Angle);

            new Seaweed(seaweedActor, seaweedInfo.Length, seaweedInfo.NodeCount, 5, levelTransition);
            new SeaweedRenderer(seaweedActor);
            var tween = new TweenChainComponent(seaweedActor);

            tween.AddLocalMoveTween(new Vector2(seaweedActor.transform.Position.X, position.Y + yOffset), 1, EaseFuncs.QuadraticEaseOut);
        }
Esempio n. 24
0
        public void Destroying_parent_with_starting_rotation()
        {
            var scene      = new Scene(null);
            var parent     = scene.AddActor("Peter Parent", new Vector2(80, 80), MathF.PI / 2);
            var child1     = scene.AddActor("Carrie Child", parent.transform.Position + new Vector2(50, 50), MathF.PI / 2);
            var child2     = scene.AddActor("Caleb Child", parent.transform.Position + new Vector2(-30, 200), 0.15f);
            var grandChild = scene.AddActor("Garry Grandchild", child1.transform.Position + new Vector2(0, 50), -0.1f);

            child1.transform.SetParent(parent);
            child2.transform.SetParent(parent);
            grandChild.transform.SetParent(child1);
            scene.FlushBuffers();

            Assert.Equal(new Vector2(50, -50),
                         child1.transform.LocalPosition); // Local position does not change with starting rotation (child1)
            Assert.True(
                new Vector2(200, 30).ApproximateEqual(child2.transform
                                                      .LocalPosition)); // Local position does not change with starting rotation (child2)
            Assert.True(
                new Vector2(50, 0).ApproximateEqual(grandChild.transform
                                                    .LocalPosition)); // Local position does not change with starting rotation (granchild)
        }
 public override void AddObject(IPhysicObject obj)
 {
     if (obj is PhysxPhysicObject)
     {
         PhysxPhysicObject PhysxPhysicObject = obj as PhysxPhysicObject;
         PhysxPhysicObject.RigidActor.UserData = obj;
         scene.AddActor(PhysxPhysicObject.RigidActor);
     }
     else if (obj is PhysxTriangleMesh)
     {
         PhysxTriangleMesh PhysxTriangleMesh = obj as PhysxTriangleMesh;
         PhysxTriangleMesh.StaticActor.UserData = obj;
         scene.AddActor(PhysxTriangleMesh.StaticActor);
     }
     else if (obj is PhysxStaticActor)
     {
         PhysxStaticActor PhysxTriangleMesh = obj as PhysxStaticActor;
         PhysxTriangleMesh.StaticActor.UserData = obj;
         scene.AddActor(PhysxTriangleMesh.StaticActor);
     }
     objs.Add(obj);
 }
Esempio n. 26
0
        public static void SpawnNewFish(Scene gameScene, Vector2 position, Player player, FishStats stats)
        {
            var fishActor = gameScene.AddActor("Fish", position);

            new BubbleSpawner(fishActor, new MinMax <int>(5, 10));
            new TimeAccumulator(fishActor);
            new Fish(fishActor, player.transform, stats);
            new ResetTargetOffsetPeriodically(fishActor);
            new SpawnBubblesRandomly(fishActor);
            new Eatable(fishActor);
            new FishRenderer(fishActor);
            new PlayerTarget(fishActor, player);
        }
Esempio n. 27
0
        private Cloth CreateCloth(Scene scene)
        {
            // Create a grid of triangles to be our cloth
            var clothGrid = new VertexGrid(25, 25);

            // Setup the grid for cooking
            var clothMeshDesc = new ClothMeshDesc()
            {
                Points    = clothGrid.Points,
                Triangles = clothGrid.Indices
            };

            // Cook
            var clothFabricStream = new MemoryStream();

            using (var cooking = scene.Physics.CreateCooking())
            {
                bool result = cooking.CookClothFabric(clothMeshDesc, new Vector3(0, -1, 0), clothFabricStream);

                if (!result)
                {
                    throw new Exception("Failed to cook deformable mesh");
                }
            }

            // Reset the seek position of the stream so we can read it form the beginning
            clothFabricStream.Position = 0;

            var clothFabric = scene.Physics.CreateClothFabric(clothFabricStream);

            var collisionData = new ClothCollisionData();

            var particles = from p in clothGrid.Points
                            select new ClothParticle()
            {
                Position      = p,
                InverseWeight = 0.1f
            };

            // Create the cloth mesh from the cooked stream
            var cloth = scene.Physics.CreateCloth(
                Matrix.Identity,
                clothFabric,
                particles.ToArray(),
                collisionData,
                0);

            scene.AddActor(cloth);

            return(cloth);
        }
Esempio n. 28
0
        public void Destroying_parent_with_late_rotation()
        {
            var scene      = new Scene(null);
            var parent     = scene.AddActor("Peter Parent", new Vector2(80, 80));
            var child1     = scene.AddActor("Carrie Child", parent.transform.Position + new Vector2(50, 50));
            var child2     = scene.AddActor("Caleb Child", parent.transform.Position + new Vector2(-30, 200));
            var grandChild = scene.AddActor("Garry Grandchild", child1.transform.Position + new Vector2(-20, 40));

            child1.transform.SetParent(parent);
            child2.transform.SetParent(parent);
            grandChild.transform.SetParent(child1);
            Assert.Equal(0, parent.transform.ChildCount); // ChildCount does not update until the next Update()
            scene.FlushBuffers();
            // Assign angle AFTER parent assignment and an update
            parent.transform.Angle = MathF.PI / 2;

            Assert.Equal(2, parent.transform.ChildCount); // ChildCount returns number of immediate children

            Assert.True(
                new Vector2(30, 130).ApproximateEqual(child1.transform.Position));  // Position after rotation (child1)
            Assert.True(
                new Vector2(-120, 50).ApproximateEqual(child2.transform.Position)); // Position after rotation (child2)
            Assert.True(
                new Vector2(-10, 110).ApproximateEqual(grandChild.transform
                                                       .Position)); // Position after rotation (grandChild)

            Assert.Equal(new Vector2(50, 50),
                         child1.transform.LocalPosition); // Local position does not change after rotation (child1)
            Assert.True(
                new Vector2(-30, 200).ApproximateEqual(child2.transform
                                                       .LocalPosition)); // Local position does not change after rotation (child2)
            Assert.True(
                new Vector2(-20, 40).ApproximateEqual(grandChild.transform
                                                      .LocalPosition)); // Local position does not change after rotation (grandchild)
            Assert.Equal(parent.transform.LocalPosition,
                         parent.transform
                         .Position); // If an actor has no parent, its local position is equivalent to its position
        }
Esempio n. 29
0
        public void moving_parent_with_children()
        {
            var scene      = new Scene(null);
            var parent     = scene.AddActor("Peter Parent", new Vector2(80, 80));
            var child      = parent.transform.AddActorAsChild("Carrie Child", new Vector2(100, 0));
            var grandChild = child.transform.AddActorAsChild("Garry Grandchild", new Vector2(100, 0));

            scene.FlushBuffers();

            parent.transform.Position = new Vector2(0, 0);

            child.transform.Position.Should().Be(new Vector2(100, 0));
            grandChild.transform.Position.Should().Be(new Vector2(200, 0));
        }
        private RigidDynamic CreateBox(Vector3 position)
        {
            var actor = _scene.Physics.CreateRigidDynamic();

            var boxGeom  = new BoxGeometry(2, 2, 2);
            var boxShape = actor.CreateShape(boxGeom, _material);

            actor.GlobalPose = Matrix4x4.CreateTranslation(position);
            actor.SetMassAndUpdateInertia(10);

            _scene.AddActor(actor);

            return(actor);
        }
Esempio n. 31
0
		protected RigidDynamic CreateBoxActor(Scene scene, Vector3 size, Vector3 position)
		{
			var material = scene.Physics.CreateMaterial(0.5f, 0.5f, 0.1f);

			var rigid = scene.Physics.CreateRigidDynamic();

			var shape = rigid.CreateShape(new BoxGeometry(size / 2), material);

			rigid.GlobalPose = Matrix.Translation(position);

			scene.AddActor(rigid);

			return rigid;
		}
Esempio n. 32
0
        protected RigidDynamic CreateBoxActor(Scene scene, Vector3 size, Vector3 position)
        {
            var material = scene.Physics.CreateMaterial(0.5f, 0.5f, 0.1f);

            var rigid = scene.Physics.CreateRigidDynamic();

            var shape = rigid.CreateShape(new BoxGeometry(size / 2), material);

            rigid.GlobalPose = Matrix4x4.CreateTranslation(position);

            scene.AddActor(rigid);

            return(rigid);
        }
Esempio n. 33
0
		private Cloth CreateCloth(Scene scene)
		{
			// Create a grid of triangles to be our cloth
			var clothGrid = new VertexGrid(25, 25);

			// Setup the grid for cooking
			var clothMeshDesc = new ClothMeshDesc()
			{
				Points = clothGrid.Points,
				Triangles = clothGrid.Indices
			};

			// Cook
			var clothFabricStream = new MemoryStream();

			using (var cooking = scene.Physics.CreateCooking())
			{
				bool result = cooking.CookClothFabric(clothMeshDesc, new Vector3(0, -1, 0), clothFabricStream);

				if (!result)
					throw new Exception("Failed to cook deformable mesh");
			}

			// Reset the seek position of the stream so we can read it form the beginning
			clothFabricStream.Position = 0;

			var clothFabric = scene.Physics.CreateClothFabric(clothFabricStream);

			var collisionData = new ClothCollisionData();

			var particles = from p in clothGrid.Points
							select new ClothParticle()
							{
								Position = p,
								InverseWeight = 0.1f
							};

			// Create the cloth mesh from the cooked stream
			var cloth = scene.Physics.CreateCloth(
				Matrix.Identity,
				clothFabric,
				particles.ToArray(),
				collisionData,
				0);

			scene.AddActor(cloth);

			return cloth;
		}
Esempio n. 34
0
        private static void CreateCapsules(Scene scene, Material material)
        {
            for (int i = 0; i < 10; i++)
            {
                var rigidActor = scene.Physics.CreateRigidDynamic();

                var capsuleGeom = new CapsuleGeometry(radius: 2, halfHeight: 2);
                var boxShape    = rigidActor.CreateShape(capsuleGeom, material);

                rigidActor.GlobalPose = Matrix4x4.CreateTranslation(0, 30 + i * (capsuleGeom.HalfHeight + capsuleGeom.Radius + 0.5f), 0);
                rigidActor.SetMassAndUpdateInertia(10);

                scene.AddActor(rigidActor);
            }
        }
Esempio n. 35
0
        private static void CreateSpheres(Scene scene, Material material)
        {
            for (int i = 0; i < 10; i++)
            {
                var rigidActor = scene.Physics.CreateRigidDynamic();

                var sphereGeom = new SphereGeometry(radius: 2);
                var boxShape   = rigidActor.CreateShape(sphereGeom, material);

                rigidActor.GlobalPose = Matrix4x4.CreateTranslation(-10, 30 + i * (sphereGeom.Radius * 2 + 0.5f), 0);
                rigidActor.SetMassAndUpdateInertia(10);

                scene.AddActor(rigidActor);
            }
        }
Esempio n. 36
0
		private static void CreateBoxes(Scene scene, Material material)
		{
			for (int i = 0; i < 10; i++)
			{
				var rigidActor = scene.Physics.CreateRigidDynamic();

				var boxGeom = new BoxGeometry(2, 2, 2);
				var boxShape = rigidActor.CreateShape(boxGeom, material);

				rigidActor.GlobalPose = Matrix.Translation(-20, 10 + i * (boxGeom.Size.Y + 0.5f), 0);
				rigidActor.SetMassAndUpdateInertia(10);

				scene.AddActor(rigidActor);
			}
		}
Esempio n. 37
0
		private static void CreateSpheres(Scene scene, Material material)
		{
			for (int i = 0; i < 10; i++)
			{
				var rigidActor = scene.Physics.CreateRigidDynamic();

				var sphereGeom = new SphereGeometry(radius: 2);
				var boxShape = rigidActor.CreateShape(sphereGeom, material);

				rigidActor.GlobalPose = Matrix.Translation(-10, 30 + i * (sphereGeom.Radius * 2 + 0.5f), 0);
				rigidActor.SetMassAndUpdateInertia(10);

				scene.AddActor(rigidActor);
			}
		}
Esempio n. 38
0
		public static void StackOfBoxes(Scene scene)
		{
			for (int i = 0; i < 10; i++)
			{
				var rigidActor = scene.Physics.CreateRigidDynamic();

				var material = scene.Physics.CreateMaterial(0.7f, 0.7f, 0.1f);

				var boxGeom = new BoxGeometry(2, 2, 2);
				var boxShape = rigidActor.CreateShape(boxGeom, material);

				rigidActor.GlobalPose = Matrix.Translation(0, 10 + i * boxGeom.Size.Y, 0);
				rigidActor.SetMassAndUpdateInertia(10);

				scene.AddActor(rigidActor);
			}
		}
Esempio n. 39
0
		private static void CreateBoxes(Scene scene, Material material)
		{
			for (int i = 0; i < 10; i++)
			{
				var rigidActor = scene.Physics.CreateRigidDynamic();

				var boxGeom = new BoxGeometry(2, 2, 2);
				var boxShape = rigidActor.CreateShape(boxGeom, material);

				rigidActor.GlobalPose = Matrix.Translation(-20, 10 + i * (boxGeom.Size.Y + 0.5f), 0);
				rigidActor.SetMassAndUpdateInertia(10);

				scene.AddActor(rigidActor);

				// Spin the cube
				rigidActor.AddTorque(new Vector3(0, 50000, 0), ForceMode.Acceleration, true);
			}
		}
Esempio n. 40
0
		private static void CreateFluidBlob(Scene scene)
		{
			// The number of particles we want along one of the three dimensions
			const int d = 15;

			// Create the particle fluid
			var particleFluid = scene.Physics.CreateParticleFluid(d * d * d);

			particleFluid.RestParticleDistance *= 10.5f;

			//

			// Create an array to hold the positions of the particles
			var particlePositions = new Vector3[particleFluid.MaximumParticles];

			var blobOffset = -new Vector3(d) * 0.5f;

			int i = 0;
			for (int x = 0; x < d; x++)
			{
				for (int y = 0; y < d; y++)
				{
					for (int z = 0; z < d; z++)
					{
						// Compute the location in a grid of particles + move the grid back so its center is at 0, 0, 0
						particlePositions[i++] = new Vector3(x, 20 + y, z) + blobOffset;
					}
				}
			}

			// Add the particles
			var particleCreateDesc = new ParticleCreationData()
			{
				NumberOfParticles = particleFluid.MaximumParticles,
				PositionBuffer = particlePositions,
				IndexBuffer = Enumerable.Range(0, particlePositions.Length).ToArray()
			};

			int numCreated = particleFluid.CreateParticles(particleCreateDesc);

			scene.AddActor(particleFluid);
		}
Esempio n. 41
0
        public override void OnAdd(Scene scene)
        {
            base.OnAdd(scene);

            scene.AddActor(this);

            PhysicsSystem world = scene.GetPhysicsEngine();

            Vector3 pos = Vector3.Up * 256 + 15*(new Vector3((float)RandomHelper.RandomGen.NextDouble(), (float)RandomHelper.RandomGen.NextDouble(), (float)RandomHelper.RandomGen.NextDouble())*2-Vector3.One);
            //pos.X += (scene.MainTerrain as TerrainHeightmap).GetWidth()*0.5f;
            //pos.Z += (scene.MainTerrain as TerrainHeightmap).GetDepth() * 0.5f;
            Vector3 normal = Vector3.Up;
            //scene.MainTerrain.GenerateRandomTransform(RandomHelper.RandomGen, out pos, out normal);
            //pos = pos + Vector3.Up * 5;

            body = new CharacterBody();
            collision = new CollisionSkin(body);

            standCapsule = new Capsule(Vector3.Zero, Matrix.CreateRotationX(MathHelper.PiOver2), 1.0f, 1.778f);
            crouchCapsule = new Capsule(Vector3.Zero, Matrix.CreateRotationX(MathHelper.PiOver2), 1.0f, 1.0f);
            SetupPosture(false);
            collision.AddPrimitive(standCapsule, (int)MaterialTable.MaterialID.NormalRough);
            body.CollisionSkin = collision;
            Vector3 com = PhysicsHelper.SetMass(75.0f, body, collision);

            body.MoveTo(pos + com, Matrix.Identity);
            collision.ApplyLocalTransform(new JigLibX.Math.Transform(-com, Matrix.Identity));

            body.SetBodyInvInertia(0.0f, 0.0f, 0.0f);

            body.AllowFreezing = false;
            body.EnableBody();
            Transformation = new Transform(body);

            ResetState();
        }
Esempio n. 42
0
		private void CreateHeightField(Scene scene, Material material)
		{
			const int rows = 25, columns = 25;
			const float scale = 3;

			var samples = CreateSampleGrid(rows, columns);

			var heightFieldDesc = new HeightFieldDesc()
			{
				NumberOfRows = rows,
				NumberOfColumns = columns,
				Samples = samples
			};

			HeightField heightField = scene.Physics.CreateHeightField(heightFieldDesc);

			//

			var rigidActor = scene.Physics.CreateRigidStatic();

			var heightFieldGeom = new HeightFieldGeometry(heightField, MeshGeometryFlag.DoubleSided, 1, scale, scale);

			rigidActor.CreateShape(heightFieldGeom, material);

			rigidActor.GlobalPose = Matrix.Translation(30, 30, -32.5f);

			scene.AddActor(rigidActor);
		}
Esempio n. 43
0
		private static void CreateCapsules(Scene scene, Material material)
		{
			for (int i = 0; i < 10; i++)
			{
				var rigidActor = scene.Physics.CreateRigidDynamic();

				var capsuleGeom = new CapsuleGeometry(radius: 2, halfHeight: 2);
				var boxShape = rigidActor.CreateShape(capsuleGeom, material);

				rigidActor.GlobalPose = Matrix.Translation(0, 30 + i * (capsuleGeom.HalfHeight + capsuleGeom.Radius + 0.5f), 0);
				rigidActor.SetMassAndUpdateInertia(10);

				scene.AddActor(rigidActor);
			}
		}
Esempio n. 44
0
		private void CreateConvexMesh(Scene scene, Material material)
		{
			var colladaLoader = new ColladaLoader();
			var bunny = colladaLoader.Load(@"Teapot.DAE", this.Engine.GraphicsDevice);

			var convexMeshDesc = new ConvexMeshDesc()
			{
				Flags = ConvexFlag.ComputeConvex
			};
			convexMeshDesc.SetPositions(bunny.VertexPositions);
			convexMeshDesc.SetTriangles(bunny.Indices);

			var cooking = scene.Physics.CreateCooking();

			var stream = new MemoryStream();
			var cookResult = cooking.CookConvexMesh(convexMeshDesc, stream);

			stream.Position = 0;

			var convexMesh = scene.Physics.CreateConvexMesh(stream);

			var convexMeshGeom = new ConvexMeshGeometry(convexMesh)
			{
				Scale = new MeshScale(new Vector3(0.3f, 0.3f, 0.3f), Quaternion.Identity)
			};

			var rigidActor = scene.Physics.CreateRigidDynamic();

			// TODO: The Shape created here is now also an owner of the ConvexMesh object,
			// this needs to be incorp into the ObjectTable ownership logic
			rigidActor.CreateShape(convexMeshGeom, material);

			rigidActor.GlobalPose =
				Matrix.RotationX(-(float)System.Math.PI / 2) *
				Matrix.Translation(0, 80, 0);

			scene.AddActor(rigidActor);
		}
Esempio n. 45
0
		protected override void LoadPhysics(Scene scene)
		{
			var material = scene.Physics.CreateMaterial(0.7f, 0.7f, 0.1f);

			// Spherical Joint
			{
				var box = scene.Physics.CreateRigidDynamic();
				box.CreateShape(new BoxGeometry(2, 2, 2), material);
				box.GlobalPose = Matrix4x4.CreateTranslation(0, 10, 0);
				box.SetMassAndUpdateInertia(10);

				//

				var sphericalJoint = scene.CreateJoint<SphericalJoint>(box, Matrix4x4.CreateTranslation(0, 4, 0), null, Matrix4x4.CreateTranslation(0, 15, 0));
				sphericalJoint.ConstraintFlag = ConstraintFlag.Visualization;

				scene.AddActor(box);
			}

			// Revolute Joint
			{
				var box = scene.Physics.CreateRigidDynamic();
				box.CreateShape(new BoxGeometry(2, 2, 2), material);
				box.GlobalPose = Matrix4x4.CreateTranslation(-10, 10, 0);
				box.SetMassAndUpdateInertia(10);

				//

				var revoluteJoint = scene.CreateJoint<RevoluteJoint>(box, Matrix4x4.CreateTranslation(0, 4, 0), null, Matrix4x4.CreateTranslation(-10, 15, 0));
				revoluteJoint.ConstraintFlag = ConstraintFlag.Visualization;

				scene.AddActor(box);
			}

			// Breakable Revolute Joint
			{
				var box = scene.Physics.CreateRigidDynamic();
				box.CreateShape(new BoxGeometry(2, 2, 2), material);
				box.GlobalPose = Matrix4x4.CreateTranslation(10, 10, 0);
				box.SetMassAndUpdateInertia(10);

				//

				var revoluteJoint = scene.CreateJoint<RevoluteJoint>(box, Matrix4x4.CreateTranslation(0, 4, 0), null, Matrix4x4.CreateTranslation(10, 15, 0));
				revoluteJoint.ConstraintFlag = ConstraintFlag.Visualization;
				revoluteJoint.BreakForce = 0.001f;

				scene.AddActor(box);
			}

			// Distance Joint
			{
				var box = scene.Physics.CreateRigidDynamic();
				box.CreateShape(new BoxGeometry(2, 2, 2), material);
				box.GlobalPose = Matrix4x4.CreateTranslation(-20, 10, 0);
				box.SetMassAndUpdateInertia(10);

				//

				var distanceJoint = scene.CreateJoint<DistanceJoint>(box, Matrix4x4.CreateTranslation(0, 4, 0), null, Matrix4x4.CreateTranslation(-20, 15, 0));
				distanceJoint.ConstraintFlag = ConstraintFlag.Visualization;
				distanceJoint.MinimumDistance = 1;
				distanceJoint.MaximumDistance = 3;

				scene.AddActor(box);
			}

			// Fixed Joint
			{
				var box = scene.Physics.CreateRigidDynamic();
				box.CreateShape(new BoxGeometry(2, 2, 2), material);
				box.GlobalPose = Matrix4x4.CreateTranslation(-30, 15, 0);
				box.SetMassAndUpdateInertia(10);

				//

				var fixedJoint = scene.CreateJoint<FixedJoint>(box, Matrix4x4.Identity, null, Matrix4x4.CreateTranslation(-30, 15, 0));
				fixedJoint.ConstraintFlag = ConstraintFlag.Visualization;

				scene.AddActor(box);
			}

			// Prismatic Joint
			{
				var box = scene.Physics.CreateRigidDynamic();
				box.CreateShape(new BoxGeometry(2, 2, 2), material);
				box.GlobalPose = Matrix4x4.CreateTranslation(-40, 15, 0);
				box.SetMassAndUpdateInertia(10);

				//

				var prismaticJoint = scene.CreateJoint<PrismaticJoint>(box, Matrix4x4.Identity, null, Matrix4x4.CreateTranslation(-40, 15, 0));
				prismaticJoint.ConstraintFlag = ConstraintFlag.Visualization;

				scene.AddActor(box);
			}

			// D6 Joint
			{
				var box = scene.Physics.CreateRigidDynamic();
				box.CreateShape(new BoxGeometry(2, 2, 2), material);
				box.GlobalPose = Matrix4x4.CreateTranslation(20, 15, 0);
				box.SetMassAndUpdateInertia(10);

				//

				var d6Joint = scene.CreateJoint<D6Joint>(box, Matrix4x4.Identity, null, Matrix4x4.CreateTranslation(20, 15, 0));
				d6Joint.ConstraintFlag = ConstraintFlag.Visualization;
				d6Joint.SetMotion(D6Axis.Twist, D6Motion.Free); // Spin on the X axis

				scene.AddActor(box);
			}
		}
Esempio n. 46
0
		private Cloth CreateCloth(Scene scene, RigidActor poleActor)
		{
			// Create a grid of triangles to be our cloth
			var clothGrid = VertexGrid.CreateGrid(40, 40, 0.4f);

			// Setup the grid for cooking
			var clothMeshDesc = new ClothMeshDesc()
			{
				Points = clothGrid.Points,
				Triangles = ArrayUtil.ToByteArray(clothGrid.Indices)
			};

			// Cook
			var clothFabricStream = new MemoryStream();

			using (var cooking = scene.Physics.CreateCooking())
			{
				cooking.CookClothFabric(clothMeshDesc, new Vector3(0, -1, 0), clothFabricStream);
			}

			// Reset the seek position of the stream so we can read it form the beginning
			clothFabricStream.Position = 0;

			var clothFabric = scene.Physics.CreateClothFabric(clothFabricStream);

			var poleBoxGeom = poleActor.GetShape(0).GetBoxGeometry();
			var boxPosition = new Vector3(poleActor.GlobalPose.M41, poleActor.GlobalPose.M42, poleActor.GlobalPose.M43);

			var particles = from p in clothGrid.Points
							select new ClothParticle()
							{
								Position = p,
								// Setting the inverse weight of a particle to 0 will pin it in place, any other value will be its weight
								InverseWeight = IsPointInBox(poleBoxGeom, boxPosition, p) ? 0 : 1
							};

			// Create the cloth mesh from the cooked stream
			var cloth = scene.Physics.CreateCloth
			(
				Matrix4x4.CreateTranslation(0, 30, 0),
				clothFabric,
				particles.ToArray(),
				0
			);

			// Enable collision with other scene geometry
			//cloth.Flags |= ClothFlag.SceneCollision;

			// GPU cloth if desired
			// The Engine class needs to create a CudaContextManager for this to work. Define 'GPU' on
			// the Engine project to enable it.
			cloth.Flags |= ClothFlag.GPU;

			scene.AddActor(cloth);

			return cloth;
		}