private void ReIntitializeTag4Rectangle(Vector3 positionCenter, float distanceX, BoxRigid rectIn) { (rectIn).ReInitializeData ( MathHelperModule.GetPositionXNA(positionCenter, distanceX / 2, rectIn.GetHalfHeight()), rectIn.GetMaterial(), new Vector3(distanceX / 2, rectIn.GetHalfHeight(), 0) ); }
private void ReIntitializeTag3Rectangle(Vector3 positionCenter, float distanceY, BoxRigid rectIn) { (rectIn).ReInitializeData ( MathHelperModule.GetPositionXNA(positionCenter, rectIn.GetHalfWidth(), distanceY / 2), rectIn.GetMaterial(), new Vector3(rectIn.GetHalfWidth(), distanceY / 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); }
public static bool boxAndHalfSpace( BoxRigid box, CollisionPlane plane, ref CollisionData data ) { // Make sure we have contacts if (data.contactsLeft <= 0) { return(false); } // Check for intersection if (!IntersectionTestboxAndHalfSpace(box, plane)) { return(false); } bool b = false; int contactsUsed = 0; for (int i = 0; i < 4; i++) { Vector3 vertexPos = ((BoxRigid)box).vertices[i].Position; // Calculate the distance from the plane float vertexDistance = Vector3.Dot(vertexPos, plane.Direction); // Compare this to the plane's distance if (vertexDistance <= plane.Offset) { b = true; // Create the contact data. // The contact point is halfway between the vertex and the // plane - we multiply the direction by half the separation // distance and add the vertex location. Contact c = new Contact(); c.ContactPoint = plane.Direction; c.ContactPoint *= (0.5f * (plane.Offset - vertexDistance)); c.ContactPoint = vertexPos; c.ContactNormal = plane.Direction; c.Penetration = plane.Offset - vertexDistance; c.Particle[0] = box; c.Particle[1] = null; c.Friction = data.friction; c.Restitution = data.restitution; c.Friction = StaticData.FrictionTable[(int)plane.Material][(int)box.GetMaterial()]; c.Restitution = StaticData.RestitutionTable[(int)plane.Material][(int)box.GetMaterial()]; 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); box.SetCanSleep(true); data.index++; data.contactsLeft--; data.contactCount++; contactsUsed++; } } return(b); }