Beispiel #1
0
        public void CapsuleSphereCollision()
        {
            // r=1.5 at (0,8,0)
            var sphere = SphereBody.Create(_simulation,
                                           new Vector3(0, 8, 0),
                                           1.5f);

            // pointing straight up, should hit sphere
            var capsule1 = CapsuleBody.Create(this._simulation,
                                              new Vector3(0, 0, 0),
                                              Quaternion.Identity,
                                              1f,
                                              6f);

            // rotated 45deg, should miss sphere
            var capsule2 = CapsuleBody.Create(this._simulation,
                                              new Vector3(0, 0, 0),
                                              Quaternion.CreateFromAxisAngle(Vector3.UnitX, (float)(0.125 * Math.Tau)),
                                              1f,
                                              6f);

            // floating, rotated 90deg, should hit sphere
            var capsule3 = CapsuleBody.Create(this._simulation,
                                              new Vector3(8, 8, 0),
                                              Quaternion.CreateFromAxisAngle(Vector3.UnitZ, (float)(0.25 * Math.Tau)),
                                              1f,
                                              6f);

            Assert.IsTrue(PhysicsSimulation.CapsuleSphereCollision(capsule1, sphere));
            Assert.IsFalse(PhysicsSimulation.CapsuleSphereCollision(capsule2, sphere));
            Assert.IsTrue(PhysicsSimulation.CapsuleSphereCollision(capsule3, sphere));
        }
Beispiel #2
0
        /// <summary>
        /// Adds a sphere physics body to the object.
        /// </summary>
        /// <param name="radius"></param>
        /// <param name="name"></param>
        public void SetProximityRadius(float radius, string name)
        {
            // Add an object and a sphere physics body.
            // One game object/script may require multiple physics bodies.
            var proximityObject = GameObject.Instantiate(GameObject.Zone);
            var physics         = proximityObject.AddComponent <PhysicsComponent>();
            var physicsObject   = SphereBody.Create(
                this.GameObject.Zone.Simulation,
                this.GameObject.Transform.Position,
                radius);

            physics.SetPhysics(physicsObject);

            // Listen for players entering and leaving.
            var playersInPhysicsObject = new List <Player>();

            Listen(physics.OnEnter, (component) =>
            {
                if (!(component.GameObject is Player player))
                {
                    return;
                }
                if (playersInPhysicsObject.Contains(player))
                {
                    return;
                }
                playersInPhysicsObject.Add(player);
                this.OnProximityUpdate(name, PhysicsCollisionStatus.Enter, player);
            });
Beispiel #3
0
        public void BoxSphereCollision()
        {
            // r=10 at (100,0,0)
            var sphereFar = SphereBody.Create(_simulation,
                                              new Vector3(100, 0, 0),
                                              10f);

            // r=4 at (7,0,0)
            var sphereClose = SphereBody.Create(_simulation,
                                                new Vector3(7, 0, 0),
                                                4f);

            // r=0.5 at (5,5,0)
            var sphere2 = SphereBody.Create(_simulation,
                                            new Vector3(5, 5, 0),
                                            0.5f);

            // r=0.5 at (6,0,0)
            var sphere3 = SphereBody.Create(_simulation,
                                            new Vector3(6, 0, 0),
                                            0.5f);

            // 10x10x10 at (0,0,0)
            var boxAtZero = BoxBody.Create(_simulation,
                                           new Vector3(0, 0, 0),
                                           Quaternion.Identity,
                                           Vector3.One * 10);

            // No rotation
            Assert.IsTrue(PhysicsSimulation.BoxSphereCollision(boxAtZero, sphereClose));
            Assert.IsFalse(PhysicsSimulation.BoxSphereCollision(boxAtZero, sphereFar));

            Assert.IsTrue(PhysicsSimulation.BoxSphereCollision(boxAtZero, sphere2));
            Assert.IsFalse(PhysicsSimulation.BoxSphereCollision(boxAtZero, sphere3));

            // Rotate box by 45deg around Z axis. Should now miss small sphere at (5,5,0) and hit small sphere at (6,0,0)
            var boxAtZeroRotated = BoxBody.Create(_simulation,
                                                  new Vector3(0, 0, 0),
                                                  Quaternion.CreateFromAxisAngle(Vector3.UnitZ, (float)Math.Tau / 8),
                                                  Vector3.One * 10);

            Assert.IsFalse(PhysicsSimulation.BoxSphereCollision(boxAtZeroRotated, sphere2));
            Assert.IsTrue(PhysicsSimulation.BoxSphereCollision(boxAtZeroRotated, sphere3));
        }
Beispiel #4
0
        public void SphereSphereCollision()
        {
            // r=10 at (0,0,0)
            var sphereAtZero = SphereBody.Create(_simulation, new Vector3(0, 0, 0),
                                                 10f);

            // r=10 at (100,0,0)
            var sphereFar = SphereBody.Create(_simulation,
                                              new Vector3(100, 0, 0),
                                              10f);

            // r=4 at (11,0,0)
            var sphereClose = SphereBody.Create(_simulation,
                                                new Vector3(11, 0, 0),
                                                4f);

            Assert.IsFalse(PhysicsSimulation.SphereSphereCollision(sphereAtZero, sphereFar));
            Assert.IsTrue(PhysicsSimulation.SphereSphereCollision(sphereAtZero, sphereClose));
        }