Пример #1
0
        public static bool sphereAndHalfSpace(
            SphereRigid sphere,
            CollisionPlane plane,
            ref CollisionData data
            )
        {
            // Make sure we have contacts
            if (data.contactsLeft <= 0)
            {
                return(false);
            }

            // Cache the sphere position
            Vector3 position = sphere.PositionCenterEngine;

            // Find the distance from the plane
            float ballDistance =
                Vector3.Dot(plane.Direction, position) -
                sphere.Radius - plane.Offset;

            if (ballDistance >= 0)
            {
                return(false);
            }

            Contact c = new Contact();

            c.ContactNormal = plane.Direction;
            c.Penetration   = -ballDistance;
            c.ContactPoint  =
                position - plane.Direction * (ballDistance + sphere.Radius);
            c.SetBodyData(sphere, null,
                          data.friction, data.restitution);


            c.ContactToWorld.M11 = plane.Direction.X;
            c.ContactToWorld.M12 = -plane.Direction.Y;
            c.ContactToWorld.M21 = plane.Direction.Y;
            c.ContactToWorld.M22 = plane.Direction.X;

            data.contacts.Add(c);

            sphere.SetCanSleep(true);

            data.addContacts(1);

            return(true);
        }
Пример #2
0
        public static bool sphereAndSphere(
            SphereRigid one,
            SphereRigid two,
            ref CollisionData data
            )
        {
            // Cache the sphere positions
            Vector3 positionOne = one.PositionCenterEngine;
            Vector3 positionTwo = two.PositionCenterEngine;

            // Find the vector between the objects
            Vector3 midline = positionOne - positionTwo;
            float   size    = midline.Length();

            // See if it is large enough.
            if (size <= 0.0f || size >= one.Radius + two.Radius)
            {
                return(false);
            }

            // We manually create the normal, because we have the
            // size to hand.
            Vector3 normal = midline * (((float)1.0) / size);

            Contact c = new Contact();

            c.ContactNormal = normal;
            c.ContactPoint  = positionTwo + midline * (float)0.5;
            c.Penetration   = (one.Radius + two.Radius - size);
            c.SetBodyData(one, two,
                          data.friction, data.restitution);



            c.ContactToWorld.M11 = normal.X;
            c.ContactToWorld.M12 = -normal.Y;
            c.ContactToWorld.M21 = normal.Y;
            c.ContactToWorld.M22 = normal.X;


            data.contacts.Add(c);

            one.SetCanSleep(true);
            two.SetCanSleep(true);
            data.addContacts(1);
            return(true);
        }