示例#1
0
 private void ReIntitializeTag4Circle(Vector3 positionCenter, float distanceX, SphereRigid circleIn)
 {
     (circleIn).ReInitializeData
     (
         MathHelperModule.GetPositionXNA(positionCenter, distanceX / 2, circleIn.Radius),
         circleIn.GetMaterial(),
         (distanceX / 2)
     );
 }
示例#2
0
        public static bool boxAndSphere(
            BoxRigid box,
            SphereRigid sphere,
            ref CollisionData data
            )
        {
            // Transform the centre of the sphere into box coordinates
            Vector3 centre    = sphere.PositionCenterEngine;
            Vector3 relCentre = centre - box.PositionCenterEngine; //box.transform.transformInverse(centre);

            relCentre = Matrix2.M_V(relCentre, -box.GetOrientation());

            // Early out check to see if we can exclude the contact
            if (Math.Abs(relCentre.X) - sphere.Radius > box.HalfSize.X ||
                Math.Abs(relCentre.Y) - sphere.Radius > box.HalfSize.Y
                )
            {
                return(false);
            }

            Vector3 closestPt = new Vector3(0, 0, 0);
            float   dist;

            // Clamp each coordinate to the box.
            dist = relCentre.X;
            if (dist > box.HalfSize.X)
            {
                dist = box.HalfSize.X;
            }
            if (dist < -box.HalfSize.X)
            {
                dist = -box.HalfSize.X;
            }
            closestPt.X = dist;

            dist = relCentre.Y;
            if (dist > box.HalfSize.Y)
            {
                dist = box.HalfSize.Y;
            }
            if (dist < -box.HalfSize.Y)
            {
                dist = -box.HalfSize.Y;
            }
            closestPt.Y = dist;

            // Check we're in contact
            Vector3 temp1 = closestPt - relCentre;

            if (temp1 == Vector3.Zero)
            {
                return(true);
            }
            float  temp2 = temp1.Length();
            double temp3 = temp2;

            dist = (float)Math.Pow(temp3, 2);
            if (dist > sphere.Radius * sphere.Radius)
            {
                return(false);
            }

            // Compile the contact
            Vector3 closestPtWorld = closestPt;//box.transform.transform(closestPt);

            closestPtWorld  = Matrix2.M_V(closestPtWorld, box.GetOrientation());
            closestPtWorld += box.PositionCenterEngine;
            //Contact* contact = data->contacts;


            // Create the contact data

            Vector3 temp = closestPtWorld - centre;

            temp.Normalize();


            Contact c = new Contact();

            c.ContactNormal = temp;
            c.Penetration   = (float)(sphere.Radius - Math.Sqrt(dist));
            c.ContactPoint  = closestPtWorld;
            c.SetBodyData(box, sphere,
                          data.friction, data.restitution);

            c.Friction = StaticData.FrictionTable[(int)sphere.GetMaterial()][(int)box.GetMaterial()];
            // between bump and cookie
            c.Restitution = 1f; // StaticData.RestitutionTable[(int)sphere.GetMaterial()][(int)box.GetMaterial()];

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


            data.addContacts(1);

            data.contacts.Add(c);

            return(true);
        }