void detectorVolume_EntityStoppedTouching(DetectorVolume volume, BEPUphysics.Entities.Entity toucher)
 {
     actor.EndCollision((Actor)toucher.CollisionInformation.OwningActor);
 }
        // position and rotation come from world parm file, which we dont have access to here
        /*public PhysicsObject(PhysicsType physicsType, Model model, Vector3 position, float mass)
        {
            this.physicsType = physicsType;
            int id = idCounter++;

            if (DisableMass)
                mass = -1.0f;

            switch (physicsType)
            {
                case PhysicsType.Box:
                    spaceObject = PhysicsHelpers.ModelToPhysicsBox(model, position, mass);
                    // boxes contain a broadphase entry which is what shows up in ray casts, so we need to make sure its tag is set to the right id
                    // this will need to be done fore anything that is an ibroadphaseentryowner
                    ((IBroadPhaseEntryOwner)spaceObject).Entry.Tag = id;
                    break;
                case PhysicsType.StaticMesh:
                    spaceObject = PhysicsHelpers.ModelToStaticMesh(model, new BEPUphysics.MathExtensions.AffineTransform(position));
                    //staticmesh's are not ibroadphaseentryowners...they will turn directly on the raycast, so nothing else to set the tag on
                    break;
                case PhysicsType.None:
                    spaceObject = null;
                    this.position = position;
                    return;
            }

            spaceObject.Tag = id;
            Stage.ActiveStage.GetQB<PhysicsQB>().Space.Add(spaceObject);
        }*/
        /// <summary>
        /// Create a physics object
        /// </summary>
        /// <param name="actor">Actor that we are attached to</param>
        /// <param name="Parm">Actor's parm file</param>
        /// <param name="model">Actors model (so we can size the collider)</param>
        /// <param name="position">Position (from world parm)</param>
        /// <param name="rotation">Rotation (from world parm)</param>
        public PhysicsObject(Actor actor, ParameterSet Parm, Model model, Vector3 position, Vector3 rotation, Stage stage)
        {
            // **rotation comes in from file as ( pitch, yaw, roll )**

            this.actor = actor;
            this.physicsType = GameLib.PhysicsObject.PhysicsTypeFromString(Parm.GetString("PhysicsType"));
            this.position = position;
            this.rotation = Matrix.CreateFromYawPitchRoll(rotation.Y, rotation.X, rotation.Z);

            if (ForceStaticMesh)
                this.physicsType = PhysicsType.StaticMesh;

            switch (this.physicsType)
            {
                case PhysicsType.Box:
                    {
                        float mass;
                        if (DisableMass)
                            mass = -1.0f;
                        else
                            mass = Parm.GetFloat("Mass");
                        BEPUphysics.Entities.Entity entity = PhysicsHelpers.ModelToPhysicsBox(model, position, mass, rotation.X, rotation.Y, rotation.Z);
                        entity.CollisionInformation.OwningActor = actor;
                        spaceObject = entity;
                        break;
                    }
                case PhysicsType.StaticMesh:
                    BEPUphysics.Collidables.StaticMesh mesh = PhysicsHelpers.ModelToStaticMesh(model, new BEPUphysics.MathExtensions.AffineTransform(Quaternion.CreateFromRotationMatrix( this.rotation ), position));
                    mesh.OwningActor = actor;
                    spaceObject = mesh;
                    break;
                case PhysicsType.Character:
                    {
                        float mass = Parm.GetFloat("Mass");
                        float scaleRadius = 1.0f;
                        if (Parm.HasParm("ScaleRadius"))
                            scaleRadius = Parm.GetFloat("ScaleRadius");
                        characterController = PhysicsHelpers.ModelToCharacterController(model, position, mass, scaleRadius);
                        spaceObject = characterController.Body;
                        characterController.Body.CollisionInformation.OwningActor = actor;
                        stage.GetQB<PhysicsQB>().AddToSpace(CharacterController);
                        return;
                    }
                case PhysicsType.CylinderCharacter:
                    {
                        float mass = Parm.GetFloat("Mass");
                        float scaleRadius = 1.0f;
                        if (Parm.HasParm("ScaleRadius"))
                            scaleRadius = Parm.GetFloat("ScaleRadius");
                        if (Parm.HasParm("ColliderDims"))
                        {
                            Vector2 v = Parm.GetVector2("ColliderDims");
                            cylinderCharController = new CylinderCharacterController(position, v.Y, v.X, mass);
                        }
                        else
                            cylinderCharController = PhysicsHelpers.ModelToCylinderCharacterController(model, position, mass, scaleRadius);
                        cylinderCharController.Body.Orientation = Quaternion.CreateFromYawPitchRoll(rotation.Y, rotation.X, rotation.Z);
                        spaceObject = cylinderCharController.Body;
                        cylinderCharController.Body.CollisionInformation.OwningActor = actor;
                        stage.GetQB<PhysicsQB>().AddToSpace(cylinderCharController);
                        return;
                    }
                case PhysicsType.TriggerVolume:
                    {
                        DetectorVolume detectorVolume = new DetectorVolume(PhysicsHelpers.ModelToTriangleMesh(model, position));
                        detectorVolume.OwningActor = actor;
                        spaceObject = detectorVolume;
                        detectorVolume.EntityBeganTouching += new EntityBeginsTouchingVolumeEventHandler(detectorVolume_EntityBeganTouching);
                        detectorVolume.EntityStoppedTouching += new EntityStopsTouchingVolumeEventHandler(detectorVolume_EntityStoppedTouching);
                        break;
                    }
                case PhysicsType.None:
                    spaceObject = null;
                    this.position = position;
                    this.rotation = Matrix.CreateFromYawPitchRoll(rotation.Y, rotation.X, rotation.Z);
                    return;
            }

            stage.GetQB<PhysicsQB>().AddToSpace(spaceObject);
        }
示例#3
0
        public override void UpdateCollision(float dt)
        {
            WasContaining = Containing;
            WasTouching   = Touching;

            mobileTriangle.collisionMargin = mesh.Shape.MeshCollisionMargin;

            //Scan the pairs in sequence, updating the state as we go.
            //Touching can be set to true by a single touching subpair.
            Touching = false;
            //Containing can be set to false by a single noncontaining or nontouching subpair.
            Containing = true;


            var            meshData = mesh.Shape.TriangleMesh.Data;
            RigidTransform mobileTriangleTransform, detectorTriangleTransform;

            mobileTriangleTransform.Orientation   = Quaternion.Identity;
            detectorTriangleTransform.Orientation = Quaternion.Identity;
            for (int i = 0; i < meshData.IndexCount; i += 3)
            {
                //Grab a triangle associated with the mobile mesh.
                meshData.GetTriangle(i, out mobileTriangle.vA, out mobileTriangle.vB, out mobileTriangle.vC);
                RigidTransform.Transform(ref mobileTriangle.vA, ref mesh.worldTransform, out mobileTriangle.vA);
                RigidTransform.Transform(ref mobileTriangle.vB, ref mesh.worldTransform, out mobileTriangle.vB);
                RigidTransform.Transform(ref mobileTriangle.vC, ref mesh.worldTransform, out mobileTriangle.vC);
                Vector3.Add(ref mobileTriangle.vA, ref mobileTriangle.vB, out mobileTriangleTransform.Position);
                Vector3.Add(ref mobileTriangle.vC, ref mobileTriangleTransform.Position, out mobileTriangleTransform.Position);
                Vector3.Multiply(ref mobileTriangleTransform.Position, 1 / 3f, out mobileTriangleTransform.Position);
                Vector3.Subtract(ref mobileTriangle.vA, ref mobileTriangleTransform.Position, out mobileTriangle.vA);
                Vector3.Subtract(ref mobileTriangle.vB, ref mobileTriangleTransform.Position, out mobileTriangle.vB);
                Vector3.Subtract(ref mobileTriangle.vC, ref mobileTriangleTransform.Position, out mobileTriangle.vC);

                //Go through all the detector volume triangles which are near the mobile mesh triangle.
                bool        triangleTouching, triangleContaining;
                BoundingBox mobileBoundingBox;
                mobileTriangle.GetBoundingBox(ref mobileTriangleTransform, out mobileBoundingBox);
                DetectorVolume.TriangleMesh.Tree.GetOverlaps(mobileBoundingBox, overlaps);
                for (int j = 0; j < overlaps.Count; j++)
                {
                    DetectorVolume.TriangleMesh.Data.GetTriangle(overlaps.Elements[j], out detectorTriangle.vA, out detectorTriangle.vB, out detectorTriangle.vC);
                    Vector3.Add(ref detectorTriangle.vA, ref detectorTriangle.vB, out detectorTriangleTransform.Position);
                    Vector3.Add(ref detectorTriangle.vC, ref detectorTriangleTransform.Position, out detectorTriangleTransform.Position);
                    Vector3.Multiply(ref detectorTriangleTransform.Position, 1 / 3f, out detectorTriangleTransform.Position);
                    Vector3.Subtract(ref detectorTriangle.vA, ref detectorTriangleTransform.Position, out detectorTriangle.vA);
                    Vector3.Subtract(ref detectorTriangle.vB, ref detectorTriangleTransform.Position, out detectorTriangle.vB);
                    Vector3.Subtract(ref detectorTriangle.vC, ref detectorTriangleTransform.Position, out detectorTriangle.vC);

                    //If this triangle collides with the convex, we can stop immediately since we know we're touching and not containing.)))
                    //[MPR is used here in lieu of GJK because the MPR implementation tends to finish quicker than GJK when objects are overlapping.  The GJK implementation does better on separated objects.]
                    if (MPRToolbox.AreShapesOverlapping(detectorTriangle, mobileTriangle, ref detectorTriangleTransform, ref mobileTriangleTransform))
                    {
                        triangleTouching = true;
                        //The convex can't be fully contained if it's still touching the surface.
                        triangleContaining = false;
                        overlaps.Clear();
                        goto finishTriangleTest;
                    }
                }

                overlaps.Clear();
                //If we get here, then there was no shell intersection.
                //If the convex's center point is contained by the mesh, then the convex is fully contained.
                //This test is only needed if containment hasn't yet been outlawed or a touching state hasn't been established.
                if ((!Touching || Containing) && DetectorVolume.IsPointContained(ref mobileTriangleTransform.Position, overlaps))
                {
                    triangleTouching   = true;
                    triangleContaining = true;
                    goto finishTriangleTest;
                }

                //If we get here, then there was no surface intersection and the convex's center is not contained- the volume and convex are separate!
                triangleTouching   = false;
                triangleContaining = false;

finishTriangleTest:
                //Analyze the results of the triangle test.

                if (triangleTouching)
                {
                    Touching = true; //If one child is touching, then we are touching too.
                }
                else
                {
                    Containing = false;  //If one child isn't touching, then we aren't containing.
                }
                if (!triangleContaining) //If one child isn't containing, then we aren't containing.
                {
                    Containing = false;
                }

                if (!Containing && Touching)
                {
                    //If it's touching but not containing, no further pairs will change the state.
                    //Containment has been invalidated by something that either didn't touch or wasn't contained.
                    //Touching has been ensured by at least one object touching.
                    break;
                }
            }

            //There is a possibility that the MobileMesh is solid and fully contains the DetectorVolume.
            //In this case, we should be Touching, but currently we are not.
            if (mesh.Shape.solidity == MobileMeshSolidity.Solid && !Containing && !Touching)
            {
                //To determine if the detector volume is fully contained, check if one of the detector mesh's vertices
                //are in the mobile mesh.

                //This *could* fail if the mobile mesh is actually multiple pieces, but that's not a common or really supported case for solids.
                Vector3 vertex;
                DetectorVolume.TriangleMesh.Data.GetVertexPosition(0, out vertex);
                Ray ray;
                ray.Direction = Vector3.Up;
                RayHit hit;
                RigidTransform.TransformByInverse(ref vertex, ref mesh.worldTransform, out ray.Position);
                if (mesh.Shape.IsLocalRayOriginInMesh(ref ray, out hit))
                {
                    Touching = true;
                }
            }

            NotifyDetectorVolumeOfChanges();
        }
 /// <summary>
 /// Constructs a new force field shape using a detector volume.
 /// </summary>
 /// <param name="volume">Volume to use.</param>
 public VolumeForceFieldShape(DetectorVolume volume)
 {
     Volume = volume;
 }
 void detectorVolume_EntityBeginsTouching(BEPUphysics.Entities.Entity toucher, DetectorVolume volume)
 {
     contactEntity = BepuEntityObject.RecoverObjectFromEntity(toucher);
     evt.Code      = FireBeginsTouching;
     evt.FireEvent(this);
 }
 void detectorVolume_VolumeStopsContainingEntity(DetectorVolume volume, BEPUphysics.Entities.Entity entity)
 {
     contactEntity = BepuEntityObject.RecoverObjectFromEntity(entity);
     evt.Code      = FireEndsContaining;
     evt.FireEvent(this);
 }
示例#7
0
 private void BeginsTouching(Entity toucher, DetectorVolume volume)
 {
     displayBox.TextureIndex = 1;
 }
示例#8
0
 /// <summary>
 /// Constructs a new force field shape using a detector volume.
 /// </summary>
 /// <param name="volume">Volume to use.</param>
 public VolumeForceFieldShape(DetectorVolume volume)
 {
     Volume = volume;
 }
示例#9
0
 private void BeginsContaining(DetectorVolume volume, Entity entity)
 {
     displayBox.TextureIndex = 2;
 }
示例#10
0
 private void StopsTouching(Entity toucher, DetectorVolume volume)
 {
     displayBox.TextureIndex = 0;
 }
示例#11
0
 private void StopsContaining(DetectorVolume volume, Entity entity)
 {
     displayBox.TextureIndex = 3;
 }
示例#12
0
 private void BeganTouching(DetectorVolume volume, Entity toucher)
 {
     SetColor(1);
 }
示例#13
0
 private void StoppedTouching(DetectorVolume volume, Entity toucher)
 {
     SetColor(0);
 }
示例#14
0
 private void BeganContaining(DetectorVolume volume, Entity entity)
 {
     SetColor(2);
 }
示例#15
0
 private void StoppedContaining(DetectorVolume volume, Entity entity)
 {
     SetColor(3);
 }