public CollideDemo(CollideType primOne, CollideType primTwo) : base() { // Turn this on to keep a reference to all contacts with both bodies CaptureBodyContacts = false; Restitution = 0.2f; Friction = 0.0f;// 1f; int positionIterations = 5; int velocityIterations = 5; float penetrationEpsilon = 0.0001f;// 26f; float velocityEpisilon = 0.0001f;// 01f; ContactResolver = new ContactResolver( positionIterations, velocityIterations, penetrationEpsilon, velocityEpisilon); primOneCollideType = primOne; primTwoCollideType = primTwo; DefaultSphereMass = 1f; DefaultSphereRadius = 1f; DefaultSpawnPosOne = new Vector3(-DefaultSphereRadius * 3.51f, 10.001f, 0.001f); DefaultSpawnPosTwo = new Vector3(DefaultSphereRadius * 3.51f, 10.001f, 0.001f); DefaultSpawnPosOne = new Vector3(0f, 10f, 0f); DefaultSpawnPosTwo = new Vector3(-0.5f, 15f, 0f); LastContact = new Contact(); }
public void Contact_XAxis() { NoBody bodyZero = new NoBody(Vector3.Right); bodyZero.Velocity = Vector3.Left; bodyZero.Mass = 1f; bodyZero.calculateDerivedData(); NoBody bodyOne = new NoBody(Vector3.Left * .9f); bodyOne.Velocity = Vector3.Right; bodyOne.Mass = 1f; bodyOne.calculateDerivedData(); Contact contact = new Contact(); contact.Bodies[0] = bodyZero; contact.Bodies[1] = bodyOne; contact.Point = Vector3.Zero; contact.Penetration = 0.1f; contact.Restitution = 1f; contact.Friction = 0f; contact.Normal = Vector3.Right; contact.ReCalc(1f); Assert.AreEqual(Vector3.Left, contact.CalcLocalVelocity(0, 1f)); Assert.AreEqual(Vector3.Right, contact.CalcLocalVelocity(1, 1f)); Assert.AreEqual(Vector3.Left, contact.RelativeContactPositions[0]); Assert.AreEqual(Vector3.Right * 0.9f, contact.RelativeContactPositions[1]); Assert.AreEqual(new Vector3(-2f, 0f, 0f), contact.ContactVelocity); // Position Change Vector3[] linearChange = new Vector3[2]; Vector3[] angularChange = new Vector3[2]; contact.ApplyPositionChange( ref linearChange, ref angularChange, contact.Penetration); Assert.AreEqual(Vector3.Zero, angularChange[0], "Zero angular change for object 0"); Assert.AreEqual(Vector3.Zero, angularChange[1], "Zero angular change for object 1"); Assert.AreEqual(new Vector3(0.05f, 0f, 0f), linearChange[0], "Body 0 is pushed right by half penetration"); Assert.AreEqual(new Vector3(-0.05f, 0f, 0f), linearChange[1], "Body 1 is pushed left by half penetration"); //Velocity Change Vector3[] velocityChange = new Vector3[2]; Vector3[] rotationChange = new Vector3[2]; contact.ApplyVelocityChange(ref velocityChange, ref rotationChange); Assert.AreEqual( Vector3.Zero, rotationChange[0], "Zero rotation change for object 0"); Assert.AreEqual( Vector3.Zero, rotationChange[1], "Zero rotation change for object 1"); Assert.AreEqual( Vector3.Right * 2f, velocityChange[0], "Counter velocity applied to body 0"); Assert.AreEqual( Vector3.Left * 2f, velocityChange[1], "Counter velocity applied to body 1"); Assert.AreEqual( Vector3.Right, bodyZero.Velocity); Assert.AreEqual(Vector3.Zero, bodyZero.Rotation); Assert.AreEqual( Vector3.Left, bodyOne.Velocity); Assert.AreEqual(Vector3.Zero, bodyOne.Rotation); }
void saveLastContact(Contact contact) { LastContact.DiscoveryHint = contact.DiscoveryHint; LastContact.Bodies = contact.Bodies; LastContact.Normal = contact.Normal; LastContact.Point = contact.Point; LastContact.Friction = contact.Friction; LastContact.Penetration = contact.Penetration; LastContact.Restitution = contact.Restitution; LastContact.DesiredDeltaVelocity = contact.DesiredDeltaVelocity; LastContact.ContactVelocity = contact.ContactVelocity; }
private static void fillPointFaceBoxBox(Box one, Box two, Vector3 toCenter, CollisionData data, int best, float penetration) { Contact contact = new Contact(); Vector3 normal = one.getAxis(best); if (Vector3.Dot(normal, toCenter) > 0) { normal *= -1f; } Vector3 vertex = two.HalfSizes; float oneDot = Vector3.Dot(two.getAxis(0), normal); if (oneDot < 0) { vertex *= new Vector3(-1f, 0f, 0f); } float twoDot = Vector3.Dot(two.getAxis(1), normal); if (twoDot < 0) { vertex *= new Vector3(0f, -1f, 0f); } float threeDot = Vector3.Dot(two.getAxis(2), normal); if (threeDot < 0) { vertex *= new Vector3(0f, 0f, -1f); } contact.DiscoveryHint = ContactDiscoveryHint.BoxOnBox_Point_Face; contact.Normal = normal; contact.Penetration = penetration; contact.Point = Vector3.Transform(vertex, two.Transform); contact.Bodies[0] = one.Body; contact.Bodies[1] = two.Body; contact.Restitution = data.restitution; contact.Friction = data.friction; data.contacts.Add(contact); }
public void Contact_BoxSpinningOnYAxisFallsOnToXZPlane() { // X-Z plane NoBody planeBody = new NoBody(Vector3.Zero); planeBody.InverseMass = 0f; //infinite mass Forever.Physics.Collide.Plane plane = new Forever.Physics.Collide.Plane(planeBody, Vector3.Zero, Vector3.Up); planeBody.calculateDerivedData(); // Spinning box colliding with the plane NoBody boxBody = new NoBody(Vector3.Up * 0.95f); boxBody.Mass = 1f; boxBody.Rotation = new Vector3(0f, 0.01f, 0f); boxBody.Velocity = new Vector3(0f, -1f, 0f); boxBody.calculateDerivedData(); Box box = new Box(boxBody, Matrix.Identity, new Vector3(1f, 1f, 1f)); Contact contact = new Contact(); contact.Bodies[0] = planeBody; contact.Bodies[1] = boxBody; contact.Point = Vector3.Zero; contact.Penetration = 0.05f; contact.Restitution = 1f; contact.Friction = 0f; contact.Normal = Vector3.Up; contact.ReCalc(1f); Assert.AreEqual(Vector3.Zero, contact.CalcLocalVelocity(0, 1f)); Assert.AreEqual(Vector3.Left, contact.CalcLocalVelocity(1, 1f)); Assert.AreEqual(Vector3.Zero, contact.RelativeContactPositions[0]); Assert.AreEqual(Vector3.Down * 0.95f, contact.RelativeContactPositions[1]); Assert.AreEqual(Vector3.Right, contact.ContactVelocity); // Position Change Vector3[] linearChange = new Vector3[2]; Vector3[] angularChange = new Vector3[2]; contact.ApplyPositionChange( ref linearChange, ref angularChange, contact.Penetration); Assert.AreEqual(Vector3.Zero, angularChange[0], "Zero angular change for object 0"); Assert.AreEqual(Vector3.Zero, angularChange[1], "Zero angular change for object 1"); Assert.AreEqual(new Vector3(0.0f, 0f, 0f), linearChange[0], "Body 0 is not pushed at all because it has infinite mass"); Assert.AreEqual(new Vector3(0f, -0.05f, 0f), linearChange[1], "Body 1 is pushed up (forward in contact direction) by half penetration"); //Velocity Change Vector3[] velocityChange = new Vector3[2]; Vector3[] rotationChange = new Vector3[2]; contact.ApplyVelocityChange(ref velocityChange, ref rotationChange); Assert.AreEqual( Vector3.Zero, rotationChange[0], "Zero rotation change for object 0"); Assert.AreEqual( Vector3.Zero, rotationChange[1], "Zero rotation change for object 1"); Assert.AreEqual( Vector3.Zero, velocityChange[0], "Zero velocity applied to body 0"); Assert.AreEqual( Vector3.Up * 2f, velocityChange[1], "Counter velocity applied to body 1"); }
public static int sphereAndHalfSpace(Sphere sphere, Plane plane, CollisionData data) { Vector3 pos = sphere.Body.Position; //d = p * L - l float ballDist = Vector3.Dot(plane.Normal, pos) - (sphere.Radius - plane.Offset); if (ballDist > 0) return 0; Contact contact = new Contact(); contact.Normal = plane.Normal; contact.Penetration = -ballDist; contact.Point = pos - (plane.Normal * (ballDist + sphere.Radius)); contact.Bodies[0] = sphere.Body; contact.Bodies[1] = null; contact.Restitution = data.restitution; contact.Friction = data.friction; data.contacts.Add(contact); return 1; }
public static int sphereAndSphere(Sphere one, Sphere two, CollisionData data) { //make sure we have contacts if (data.contactsLeft <= 0) return 0; // Cache the sphere positions Vector3 positionOne = one.Body.Position; Vector3 positionTwo = two.Body.Position; if (positionOne == positionTwo) { throw new ArgumentException("Two bodies with the same position are unsupported....TODO"); } // 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 0; } // we manually create the normal, because we have the size at hand; Vector3 normal = midline * (1f / size); Contact contact = new Contact(); contact.Normal = normal; contact.Point = positionTwo + midline * 0.5f; contact.Penetration = (one.Radius + two.Radius - size); //write the appropriate data contact.Bodies[0] = one.Body; contact.Bodies[1] = two.Body; contact.Restitution = data.restitution; contact.Friction = data.friction; data.contacts.Add(contact); return 1; }
public static int edgeAndEdge( Box left, Vector3 leftEdgeOne, Vector3 leftEdgeTwo, Box right, Vector3 rightEdgeOne, Vector3 rightEdgeTwo, CollisionData data) { //return 0; //TODO Vector3 leftCenter = left.Body.Position; Vector3 rightCenter = right.Body.Position; Vector3 midLineRight = rightCenter - leftCenter; float distCenters = midLineRight.Length(); Vector3 projectedLeftEdge = TrickyMath.Project(leftEdgeOne - leftCenter, midLineRight); float distLeftCenterToEdge = projectedLeftEdge.Length(); Vector3 projectedRightEdge = TrickyMath.Project(rightEdgeOne - rightCenter, -midLineRight); float distRightCenterToEdge = projectedRightEdge.Length(); if (distCenters > (distLeftCenterToEdge + distRightCenterToEdge)) { return 0; } float distEdges = (distLeftCenterToEdge + distRightCenterToEdge) - distCenters; Contact contact = new Contact(); contact.DiscoveryHint = ContactDiscoveryHint.BoxOnBox_Edge_Edge; contact.Restitution = data.restitution; contact.Friction = data.friction; contact.Bodies[0] = left.Body; contact.Bodies[1] = right.Body; contact.Penetration = (distLeftCenterToEdge + distRightCenterToEdge) - distCenters; contact.Point = leftCenter + (midLineRight * 0.5f); Vector3 x = leftEdgeOne - leftEdgeTwo; ; x.Normalize(); Vector3 y, z; TrickyMath.MakeOrthonormalBasis(x, out y, out z); Vector3 realY = y; Vector3 normal = TrickyMath.VectorProduct(x, realY); normal.Normalize(); contact.Normal = normal; data.contacts.Add(contact); return 1; }
public static int boxAndSphere(Box box, Sphere sph, CollisionData data) { Vector3 center = sph.Body.Position; Vector3 relCenter = Vector3.Transform(center, Matrix.Invert(box.Transform)); double x = relCenter.X; double y = relCenter.Y; double z = relCenter.Z; if (Math.Abs(x) - sph.Radius > box.HalfSizes.X || Math.Abs(y) - sph.Radius > box.HalfSizes.Y || Math.Abs(z) - sph.Radius > box.HalfSizes.Z) { return 0; } Vector3 closest = Vector3.Zero; float dist; dist = relCenter.X; if (dist > box.HalfSizes.X) dist = box.HalfSizes.X; if (dist < -box.HalfSizes.X) dist = -box.HalfSizes.X; closest.X = dist; dist = relCenter.Y; if (dist > box.HalfSizes.Y) dist = box.HalfSizes.Y; if (dist < -box.HalfSizes.Y) dist = -box.HalfSizes.Y; closest.Y = dist; dist = relCenter.Z; if (dist > box.HalfSizes.Z) dist = box.HalfSizes.Z; if (dist < -box.HalfSizes.Z) dist = -box.HalfSizes.Z; closest.Z = dist; dist = (closest - relCenter).Length(); if (dist > sph.Radius && !TrickyMath.AlmostEquals(0f, dist - sph.Radius)) return 0; Vector3 closestWorld = Vector3.Transform(closest, box.Transform); Contact contact = new Contact(); Vector3 normal = closestWorld - center;// (center - closestWorld); normal.Normalize(); contact.Normal = normal; Debug.Sanity(contact.Normal); contact.Point = closestWorld; contact.Penetration = sph.Radius - dist;// (float)Math.Sqrt((float)dist); contact.Bodies[0] = box.Body; contact.Bodies[1] = sph.Body; contact.Restitution = data.restitution; contact.Friction = data.friction; data.contacts.Add(contact); return 1; }
//TODO - write unit tests!!!!!! public static int boxAndPoint(Box box, Vector3 pointWorld, CollisionData data) { Vector3 pointLocal = Vector3.Transform(pointWorld, Matrix.Invert(box.Body.World * box.OffsetMatrix)); Vector3 halfSizes = box.HalfSizes; Vector3 absPoint = new Vector3( TrickyMath.Abs(pointLocal.X), TrickyMath.Abs(pointLocal.Y), TrickyMath.Abs(pointLocal.Z) ); Vector3 check = halfSizes - absPoint; if (check.X < 0 || check.Y < 0 || check.Z < 0) { return 0; } Vector3 depth = new Vector3( TrickyMath.Abs(absPoint.X - halfSizes.X), TrickyMath.Abs(absPoint.Y - halfSizes.Y), TrickyMath.Abs(absPoint.Z - halfSizes.Z) ); depth = new Vector3( depth.X != 0f ? depth.X : float.MaxValue, depth.Y != 0f ? depth.Y : float.MaxValue, depth.Z != 0f ? depth.Z : float.MaxValue ); if (depth.X <= depth.Y && depth.X <= depth.Z) { Contact contact = new Contact(); contact.DiscoveryHint = ContactDiscoveryHint.BoxOnBox_Point_Face; Vector3 boxAxis = box.getAxis(0); contact.Normal = boxAxis * (pointLocal.X > 0 ? -1f : 1f); contact.Point = pointWorld; contact.Penetration = TrickyMath.Abs(absPoint.X - halfSizes.X); contact.Bodies[0] = box.Body; contact.Bodies[1] = null; contact.Restitution = data.restitution; contact.Friction = data.friction; data.contacts.Add(contact); return 1; } else if (depth.Y <= depth.X && depth.Y <= depth.Z) { Contact contact = new Contact(); contact.DiscoveryHint = ContactDiscoveryHint.BoxOnBox_Point_Face; Vector3 boxAxis = box.getAxis(1); contact.Normal = boxAxis * (pointLocal.Y > 0 ? -1f : 1f); contact.Point = pointWorld; contact.Penetration = TrickyMath.Abs(absPoint.Y - halfSizes.Y); contact.Bodies[0] = box.Body; contact.Bodies[1] = null; contact.Restitution = data.restitution; contact.Friction = data.friction; data.contacts.Add(contact); return 1; } else if (depth.Z <= depth.X && depth.Z <= depth.Y) { Contact contact = new Contact(); contact.DiscoveryHint = ContactDiscoveryHint.BoxOnBox_Point_Face; Vector3 boxAxis = box.getAxis(2); contact.Normal = boxAxis * (pointLocal.Z > 0 ? -1f : 1f); contact.Point = pointWorld; contact.Penetration = TrickyMath.Abs(absPoint.Z - halfSizes.Z); contact.Bodies[0] = box.Body; contact.Bodies[1] = null; contact.Restitution = data.restitution; contact.Friction = data.friction; data.contacts.Add(contact); return 1; } else { return 0; } }
/// <summary> /// danger! This works for ContactDemo, but there is one hang up. The call to Math.Abs() should not /// be needed. It isn't in the orange book text. However, we only get negative numbers from that Dot. /// </summary> /// <param name="box"></param> /// <param name="plane"></param> /// <param name="data"></param> /// <returns></returns> public static int boxAndPlane(Box box, Plane plane, CollisionData data) { if (data.contactsLeft <= 0) return 0; int contacts_found = 0; // p dot L < l Vector3[] verts = box.WorldVerts(); foreach (Vector3 vert in verts) { //float vertexDist = (float)Math.Abs( (double)Vector3.Dot(plane.Normal, vert)); float vertexDist = Vector3.Dot(-plane.Normal, vert); if (vertexDist >= plane.Offset + data.tolerance) { // the contact point is halfway between the vertex and the plane // we multiply the direction by half the spearation distance // and add the vertex location Contact contact = new Contact(); contact.DiscoveryHint = ContactDiscoveryHint.BoxOnPlane_Corner; contact.Bodies[0] = box.Body; contact.Bodies[1] = null; contact.Point = plane.ClosestPoint(vert); contact.Normal = plane.Normal; contact.Penetration = vertexDist - plane.Offset;//TrickyMathHelper.Abs(plane.Offset - vertexDist); contact.Restitution = data.restitution; contact.Friction = data.friction; data.contacts.Add(contact); contacts_found++; } } return contacts_found; }
public static int boxAndBox(Box one, Box two, CollisionData data) { Vector3 toCenter = two.Body.Position - one.Body.Position; float pen = float.MaxValue; int best = int.MaxValue; if (!tryAxis(one, two, one.getAxis(0), toCenter, 0, ref pen, ref best)) return 0; if (!tryAxis(one, two, one.getAxis(1), toCenter, 1, ref pen, ref best)) return 0; if (!tryAxis(one, two, one.getAxis(2), toCenter, 2, ref pen, ref best)) return 0; if (!tryAxis(one, two, one.getAxis(0), toCenter, 3, ref pen, ref best)) return 0; if (!tryAxis(one, two, one.getAxis(1), toCenter, 4, ref pen, ref best)) return 0; if (!tryAxis(one, two, one.getAxis(2), toCenter, 5, ref pen, ref best)) return 0; int bestSingleAxis = best; if (!tryAxis(one, two, TrickyMath.VectorProduct(one.getAxis(0), two.getAxis(0)), toCenter, 6, ref pen, ref best)) return 0; if (!tryAxis(one, two, TrickyMath.VectorProduct(one.getAxis(0), two.getAxis(1)), toCenter, 7, ref pen, ref best)) return 0; if (!tryAxis(one, two, TrickyMath.VectorProduct(one.getAxis(0), two.getAxis(2)), toCenter, 8, ref pen, ref best)) return 0; if (!tryAxis(one, two, TrickyMath.VectorProduct(one.getAxis(1), two.getAxis(0)), toCenter, 9, ref pen, ref best)) return 0; if (!tryAxis(one, two, TrickyMath.VectorProduct(one.getAxis(1), two.getAxis(1)), toCenter, 10, ref pen, ref best)) return 0; if (!tryAxis(one, two, TrickyMath.VectorProduct(one.getAxis(1), two.getAxis(2)), toCenter, 11, ref pen, ref best)) return 0; if (!tryAxis(one, two, TrickyMath.VectorProduct(one.getAxis(2), two.getAxis(0)), toCenter, 12, ref pen, ref best)) return 0; if (!tryAxis(one, two, TrickyMath.VectorProduct(one.getAxis(2), two.getAxis(1)), toCenter, 13, ref pen, ref best)) return 0; if (!tryAxis(one, two, TrickyMath.VectorProduct(one.getAxis(2), two.getAxis(2)), toCenter, 14, ref pen, ref best)) return 0; if (best == int.MaxValue) { throw new Exception("Found no good axis"); } if (best < 3) { fillPointFaceBoxBox(one, two, toCenter, data, best, pen); return 1; } else if (best < 6) { // We've got a vertex of box one on a face of box two. // We use the same algorithm as above, but swap around // one and two (and therefore also the vector between their // centres). fillPointFaceBoxBox(two, one, toCenter * -1.0f, data, best - 3, pen); return 1; } else { best -= 6; //move this back to talk about a cardinal axis int oneAxisIndex = best / 3; int twoAxisIndex = best % 3; Vector3 oneAxis = one.getAxis(oneAxisIndex); Vector3 twoAxis = two.getAxis(twoAxisIndex); Vector3 axis = TrickyMath.VectorProduct(oneAxis, twoAxis); axis.Normalize(); if (Vector3.Dot(axis, toCenter) > 0) { axis *= -1f; } float[] ptOnOneEdge = TrickyMath.Vector3ToFloatArray(one.HalfSizes); float[] ptOnTwoEdge = TrickyMath.Vector3ToFloatArray(two.HalfSizes); for (int i = 0; i < 3; i++) { if (i == oneAxisIndex) { ptOnOneEdge[i] = 0f; } else if (Vector3.Dot(one.getAxis(i), axis) > 0) { ptOnOneEdge[i] *= -1f; } if (i == twoAxisIndex) { ptOnTwoEdge[i] = 0; } else if (Vector3.Dot(two.getAxis(i), axis) < 0) { ptOnTwoEdge[i] *= -1f; } } Vector3 vertexOnOneEdge = Vector3.Transform( TrickyMath.FloatArrayToVector3(ptOnOneEdge), one.Transform ); Vector3 vertexOnTwoEdge = Vector3.Transform( TrickyMath.FloatArrayToVector3(ptOnTwoEdge), two.Transform ); float[] oneHalfSizes = TrickyMath.Vector3ToFloatArray(one.HalfSizes); float[] twoHalfSizes = TrickyMath.Vector3ToFloatArray(two.HalfSizes); Vector3 vertex = contactPoint( vertexOnOneEdge, oneAxis, oneHalfSizes[oneAxisIndex], vertexOnTwoEdge, twoAxis, twoHalfSizes[twoAxisIndex], bestSingleAxis > 2 ); Contact contact = new Contact(); contact.DiscoveryHint = ContactDiscoveryHint.BoxOnBox_Edge_Edge; contact.Penetration = pen; contact.Normal = axis; contact.Point = vertex; contact.Bodies[0] = one.Body; contact.Bodies[1] = two.Body; contact.Restitution = data.restitution; contact.Friction = data.friction; data.contacts.Add(contact); return 1; } return 0; }