Ejemplo n.º 1
0
        // Find collisions between a point model and a set of world planes
        // For each collision found, notify point and plane controllers so that they can react to the collision
        private void CheckCollisionsAgainstPlanes(PhysicWorldModel world, PhysicPointModel pointModel, PhysicPointController pointController, List <PhysicPlaneModel> planes)
        {
            PhysicPlaneController planeController;
            FixedVector3          intersection;

            // Do a few iterations until collisions get stable, or we reach a limit on iterations
            bool collisionsAreStable = false;

            for (int i = 0; i < collisionIterationsCount && !collisionsAreStable; ++i)
            {
                collisionsAreStable = true;
                int numCollisions = 0;
                foreach (PhysicPlaneModel planeModel in planes)
                {
                    if (PhysicPlaneController.CheckIntersection(planeModel, pointModel, out intersection))
                    {
                        ++numCollisions;
                        collisionsAreStable &= pointController.OnCollision(world, pointModel, planeModel, intersection);
                        planeController      = planeModel.Controller() as PhysicPlaneController;
                        collisionsAreStable &= planeController == null || planeController.OnCollision(world, pointModel, planeModel, intersection);
                    }
                }
                //Debug.Log("Collisions: " + numCollisions);
            }
        }
Ejemplo n.º 2
0
        // Check collisions between physic models, and apply gravity to them
        public FixedVector3 Raycast(PhysicWorldModel world, FixedVector3 origin, FixedVector3 direction)
        {
            // A raycast long enough
            FixedVector3 target = origin + direction * 99;

            // Get all planes to check collisions
            List <PhysicPlaneModel> allPlanes = GetAllPlanes(world);

            FixedVector3 intersection;

            // Find closest intersection
            FixedVector3 closestIntersection         = new FixedVector3(FixedFloat.MaxValue, FixedFloat.MaxValue, FixedFloat.MaxValue);
            FixedFloat   closestIntersectionDistance = FixedFloat.MaxValue;

            foreach (PhysicPlaneModel planeModel in allPlanes)
            {
                if (PhysicPlaneController.CheckIntersection(planeModel, origin, target, FixedVector3.Zero, out intersection))
                {
                    FixedFloat distance = FixedVector3.Distance(origin, intersection);
                    if (distance < closestIntersectionDistance)
                    {
                        closestIntersectionDistance = distance;
                        closestIntersection         = intersection;
                    }
                }
            }
            return(closestIntersection);
        }