// This function calculates OBB to OBB colisions public static CollisionInfo OBBToOBBCollision(CollisionHull2D a, CollisionHull2D b) { // Compute the R hat and U hat for both collision hulls Vector2 ARHat = new Vector2(Mathf.Abs(Mathf.Cos(a.GetRotation())), Mathf.Abs(-Mathf.Sin(a.GetRotation()))); Vector2 BRHat = new Vector2(Mathf.Abs(Mathf.Cos(b.GetRotation())), Mathf.Abs(-Mathf.Sin(b.GetRotation()))); Vector2 AUHat = new Vector2(Mathf.Abs(Mathf.Sin(a.GetRotation())), Mathf.Abs(Mathf.Cos(a.GetRotation()))); Vector2 BUHat = new Vector2(Mathf.Abs(Mathf.Sin(b.GetRotation())), Mathf.Abs(Mathf.Cos(b.GetRotation()))); // Create a list of all penetrations on each axis List <float> overlaps = new List <float>(); // Do axis checks overlaps.Add(CheckOBBAxis(a, b, ARHat)); // Does the check pass? if (overlaps[0] == Mathf.Infinity) { // If no, return nothing return(null); } // Do axis checks overlaps.Add(CheckOBBAxis(a, b, AUHat)); // Does the check pass? if (overlaps[1] == Mathf.Infinity) { // If no, return nothing return(null); } // Do axis checks overlaps.Add(CheckOBBAxis(a, b, BRHat)); // Does the check pass? if (overlaps[2] == Mathf.Infinity) { // If no, return nothing return(null); } // Do axis checks overlaps.Add(CheckOBBAxis(a, b, AUHat)); // Do the axis checks pass? if (overlaps[3] != Mathf.Infinity) { // If yes, then inform the parents of the complex shape object (if applicable) ReportCollisionToParent(a, b); } else { // If no, return nothing return(null); } // Return full details of the Collision list if the two collide return(new CollisionInfo(a, b, CollisionResolution.GetFinalPenetration(overlaps))); }
// Update is called once per frame void Update() { collisions.Clear(); for (int i = 0; i < particles.Count; i++) { particles[i].ResetCollidingChecker(); } // Iterate through all particles for (int x = 0; x < particles.Count; x++) { for (int y = 0; y < particles.Count; y++) { // If the one being checked equal to itself? if (x != y && (particles[x].transform.parent != particles[y].transform.parent || particles[x].transform.parent == null)) { CollisionPairKey key = new CollisionPairKey(particles[y].collisionType, particles[x].collisionType); CollisionInfo collision; if (particles[x].collisionType > particles[y].collisionType) { collision = _collisionTypeCollisionTestFunctions[key](particles[y], particles[x]); } else { collision = _collisionTypeCollisionTestFunctions[key](particles[x], particles[y]); } if (collision != null) { bool isDuplicate = false; for (int i = 0; i < collisions.Count; i++) { if ((collisions[i].a == particles[y] && collisions[i].b == particles[x]) || (collisions[i].a == particles[x] && collisions[i].b == particles[y])) { isDuplicate = true; } } if (!isDuplicate) { collisions.Add(collision); } } } } } CollisionResolution.ResolveCollisions(collisions, Time.deltaTime); }
// This function intializes the collision info class based on given information public CollisionInfo(CollisionHull2D _a, CollisionHull2D _b, float _penetration) { // Is collision A's collision type have less priority to collision B? if (_a.collisionType > _b.collisionType) { // If yes, then switch their priorities a = _b; b = _a; } else { // If no, then keep them as so a = _a; b = _b; } // Based on collision hulls, calculate the rest of the values separatingVelocity = CollisionResolution.CalculateSeparatingVelocity(a, b); normal = (b.GetPosition() - a.GetPosition()).normalized; penetration = _penetration; }
private void ProcessImportModulePackage(ExportPackage exportPackage, string tempFolder, CollisionResolution collisionResolution) { try { var filePath = Path.Combine(tempFolder, exportPackage.PackageFileName); if (!File.Exists(filePath)) { return; } var packageType = exportPackage.PackageType; var packageName = exportPackage.PackageName; var version = exportPackage.Version; var existPackage = PackageController.Instance.GetExtensionPackage( Null.NullInteger, p => p.PackageType == packageType && p.Name == packageName); if (existPackage != null && (existPackage.Version > version || (existPackage.Version == version && collisionResolution == CollisionResolution.Ignore))) { this.Result.AddLogEntry( "Import Package ignores", $"{packageName} has higher version {existPackage.Version} installed, ignore import it"); return; } this.InstallPackage(filePath); this.Result.AddLogEntry("Import Package completed", $"{packageName} version: {version}"); } catch (Exception ex) { this.Result.AddLogEntry( "Import Package error", $"{exportPackage.PackageName} : {exportPackage.Version} - {ex.Message}"); Logger.Error(ex); } }
// This function calculates OBB to OBB colisions public static CollisionInfo OBBToOBBCollision(CollisionHull3D a, CollisionHull3D b) { List <float> overlaps = new List <float>(); List <Vector3> axes = new List <Vector3>(); // Get the transform values for each axis for each shape Vector3 x1 = a.GetComponent <Particle3D>().transformMatrix.GetColumn(0); Vector3 y1 = a.GetComponent <Particle3D>().transformMatrix.GetColumn(1); Vector3 z1 = a.GetComponent <Particle3D>().transformMatrix.GetColumn(2); Vector3 x2 = b.GetComponent <Particle3D>().transformMatrix.GetColumn(0); Vector3 y2 = b.GetComponent <Particle3D>().transformMatrix.GetColumn(1); Vector3 z2 = b.GetComponent <Particle3D>().transformMatrix.GetColumn(2); // Go through and check through all overlapping axes // Face/Face Object 1 overlaps.Add(CheckOBBAxis(a, b, x1)); axes.Add(x1); if (overlaps[overlaps.Count - 1] < 0) { return(null); } overlaps.Add(CheckOBBAxis(a, b, y1)); axes.Add(y1); if (overlaps[overlaps.Count - 1] < 0) { return(null); } overlaps.Add(CheckOBBAxis(a, b, z1)); axes.Add(z1); if (overlaps[overlaps.Count - 1] < 0) { return(null); } // Face/Face Object 2 overlaps.Add(CheckOBBAxis(a, b, x2)); axes.Add(x2); if (overlaps[overlaps.Count - 1] < 0) { return(null); } overlaps.Add(CheckOBBAxis(a, b, y2)); axes.Add(y2); if (overlaps[overlaps.Count - 1] < 0) { return(null); } overlaps.Add(CheckOBBAxis(a, b, z2)); axes.Add(z2); if (overlaps[overlaps.Count - 1] < 0) { return(null); } // Edge/Edge overlaps.Add(CheckOBBAxis(a, b, Vector3.Cross(x1, x2))); axes.Add(Vector3.Cross(x1, x2)); if (overlaps[overlaps.Count - 1] < 0) { return(null); } overlaps.Add(CheckOBBAxis(a, b, Vector3.Cross(x1, y2))); axes.Add(Vector3.Cross(x1, y2)); if (overlaps[overlaps.Count - 1] < 0) { return(null); } overlaps.Add(CheckOBBAxis(a, b, Vector3.Cross(x1, z2))); axes.Add(Vector3.Cross(x1, z2)); if (overlaps[overlaps.Count - 1] < 0) { return(null); } overlaps.Add(CheckOBBAxis(a, b, Vector3.Cross(y1, x2))); axes.Add(Vector3.Cross(y1, x2)); if (overlaps[overlaps.Count - 1] < 0) { return(null); } overlaps.Add(CheckOBBAxis(a, b, Vector3.Cross(y1, y2))); axes.Add(Vector3.Cross(y1, y2)); if (overlaps[overlaps.Count - 1] < 0) { return(null); } overlaps.Add(CheckOBBAxis(a, b, Vector3.Cross(y1, z2))); axes.Add(Vector3.Cross(y1, z2)); if (overlaps[overlaps.Count - 1] < 0) { return(null); } overlaps.Add(CheckOBBAxis(a, b, Vector3.Cross(z1, x2))); axes.Add(Vector3.Cross(z1, x2)); if (overlaps[overlaps.Count - 1] < 0) { return(null); } overlaps.Add(CheckOBBAxis(a, b, Vector3.Cross(z1, y2))); axes.Add(Vector3.Cross(z1, y2)); if (overlaps[overlaps.Count - 1] < 0) { return(null); } overlaps.Add(CheckOBBAxis(a, b, Vector3.Cross(z1, z2))); axes.Add(Vector3.Cross(z1, z2)); if (overlaps[overlaps.Count - 1] < 0) { return(null); } float bestOverlap = Mathf.Infinity; int bestIndex = 0; for (int i = 0; i < overlaps.Count; i++) { if (overlaps[i] < bestOverlap) { bestOverlap = overlaps[i]; bestIndex = i; } } if (bestIndex > 2) { Vector3 normal = axes[bestIndex]; Vector3 axis = axes[bestIndex]; if (Vector3.Dot(axis, (b.GetPosition() - a.GetPosition())) > 0) { axis *= -1; } Vector3 vertex = a.GetDimensions(); if (Vector3.Dot(a.GetComponent <Particle3D>().transformMatrix.GetColumn(0), normal) > 0) { vertex.x = -vertex.x; } if (Vector3.Dot(a.GetComponent <Particle3D>().transformMatrix.GetColumn(1), normal) > 0) { vertex.y = -vertex.y; } if (Vector3.Dot(a.GetComponent <Particle3D>().transformMatrix.GetColumn(2), normal) > 0) { vertex.z = -vertex.z; } vertex = a.GetComponent <Particle3D>().invTransformMatrix.MultiplyPoint(vertex); // If all axis are overlaping, then we have a collision ReportCollisionToParent(a, b); return(new CollisionInfo(a, b, CollisionResolution.GetFinalPenetration(overlaps), normal, vertex, CollisionResolution3D.CalculateSeparatingVelocity(a, b, a.GetPosition(), b.GetPosition()))); } else if (bestIndex > 5) { Vector3 normal = axes[bestIndex]; Vector3 axis = axes[bestIndex]; if (Vector3.Dot(axis, (a.GetPosition() - b.GetPosition())) > 0) { axis *= -1; } Vector3 vertex = b.GetDimensions(); if (Vector3.Dot(b.GetComponent <Particle3D>().transformMatrix.GetColumn(0), normal) > 0) { vertex.x = -vertex.x; } if (Vector3.Dot(b.GetComponent <Particle3D>().transformMatrix.GetColumn(1), normal) > 0) { vertex.y = -vertex.y; } if (Vector3.Dot(b.GetComponent <Particle3D>().transformMatrix.GetColumn(2), normal) > 0) { vertex.z = -vertex.z; } vertex = b.GetComponent <Particle3D>().invTransformMatrix.MultiplyPoint(vertex); // If all axis are overlaping, then we have a collision ReportCollisionToParent(a, b); return(new CollisionInfo(a, b, CollisionResolution.GetFinalPenetration(overlaps), normal, vertex, CollisionResolution3D.CalculateSeparatingVelocity(a, b, a.GetPosition(), b.GetPosition()))); } else { int index = bestIndex - 6; int oneIndex = index / 3; int twoIndex = index % 3; Vector3 oneAxis = axes[oneIndex]; Vector3 twoAxis = axes[twoIndex]; Vector3 axis = axes[bestIndex].normalized; if (Vector3.Dot(axis, b.GetPosition() - a.GetPosition()) > 0) { axis *= -1; } Vector3 pointOnOneEdge = a.GetDimensions(); Vector3 pointOnTwoEdge = b.GetDimensions(); if (oneIndex == 0) { pointOnOneEdge.x = 0; } else if (Vector2.Dot(x1, axis) > 0) { pointOnOneEdge.x = -pointOnOneEdge.x; } if (twoIndex == 3) { pointOnTwoEdge.x = 0; } else if (Vector2.Dot(x2, axis) > 0) { pointOnTwoEdge.x = -pointOnTwoEdge.x; } if (oneIndex == 1) { pointOnOneEdge.y = 0; } else if (Vector2.Dot(y1, axis) > 0) { pointOnOneEdge.y = -pointOnOneEdge.y; } if (twoIndex == 4) { pointOnTwoEdge.y = 0; } else if (Vector2.Dot(y2, axis) > 0) { pointOnTwoEdge.y = -pointOnTwoEdge.y; } if (oneIndex == 2) { pointOnOneEdge.z = 0; } else if (Vector2.Dot(z1, axis) > 0) { pointOnOneEdge.z = -pointOnOneEdge.z; } if (twoIndex == 5) { pointOnTwoEdge.z = 0; } else if (Vector2.Dot(z2, axis) > 0) { pointOnTwoEdge.z = -pointOnTwoEdge.z; } pointOnOneEdge = a.GetComponent <Particle3D>().invTransformMatrix.MultiplyPoint(pointOnOneEdge); pointOnTwoEdge = b.GetComponent <Particle3D>().invTransformMatrix.MultiplyPoint(pointOnTwoEdge); Vector3 contactPoint = GetCollisionPoint(pointOnOneEdge, oneAxis, a.GetDimensions().x, pointOnTwoEdge, twoAxis, b.GetDimensions().x, bestIndex > 2); ReportCollisionToParent(a, b); return(new CollisionInfo(a, b, CollisionResolution.GetFinalPenetration(overlaps), axis, contactPoint, CollisionResolution3D.CalculateSeparatingVelocity(a, b, a.GetPosition(), b.GetPosition()))); } }
public override Manifold GenerateManifold(PolygonEntity other) { return(CollisionResolution.GenerateManifold(other, this)); }
public override Manifold GenerateManifold(CircleEntity other) { return(CollisionResolution.GenerateManifold(this, other)); }
// This function calculates OBB to OBB colisions public static CollisionInfo OBBToOBBCollision(CollisionHull3D a, CollisionHull3D b) { List <float> overlaps = new List <float>(); // Get the transform values for each axis for each shape Vector3 x1 = a.GetComponent <Particle3D>().transformMatrix.GetColumn(0); Vector3 y1 = a.GetComponent <Particle3D>().transformMatrix.GetColumn(1); Vector3 z1 = a.GetComponent <Particle3D>().transformMatrix.GetColumn(2); Vector3 x2 = b.GetComponent <Particle3D>().transformMatrix.GetColumn(0); Vector3 y2 = b.GetComponent <Particle3D>().transformMatrix.GetColumn(1); Vector3 z2 = b.GetComponent <Particle3D>().transformMatrix.GetColumn(2); // Go through and check through all overlapping axes overlaps.Add(CheckOBBAxis(a, b, x1)); if (overlaps[overlaps.Count - 1] < 0) { return(null); } overlaps.Add(CheckOBBAxis(a, b, y1)); if (overlaps[overlaps.Count - 1] < 0) { return(null); } overlaps.Add(CheckOBBAxis(a, b, z1)); if (overlaps[overlaps.Count - 1] < 0) { return(null); } overlaps.Add(CheckOBBAxis(a, b, x2)); if (overlaps[overlaps.Count - 1] < 0) { return(null); } overlaps.Add(CheckOBBAxis(a, b, y2)); if (overlaps[overlaps.Count - 1] < 0) { return(null); } overlaps.Add(CheckOBBAxis(a, b, z2)); if (overlaps[overlaps.Count - 1] < 0) { return(null); } overlaps.Add(CheckOBBAxis(a, b, Vector3.Cross(x1, x2))); if (overlaps[overlaps.Count - 1] < 0) { return(null); } overlaps.Add(CheckOBBAxis(a, b, Vector3.Cross(x1, y2))); if (overlaps[overlaps.Count - 1] < 0) { return(null); } overlaps.Add(CheckOBBAxis(a, b, Vector3.Cross(x1, z2))); if (overlaps[overlaps.Count - 1] < 0) { return(null); } overlaps.Add(CheckOBBAxis(a, b, Vector3.Cross(y1, x2))); if (overlaps[overlaps.Count - 1] < 0) { return(null); } overlaps.Add(CheckOBBAxis(a, b, Vector3.Cross(y1, y2))); if (overlaps[overlaps.Count - 1] < 0) { return(null); } overlaps.Add(CheckOBBAxis(a, b, Vector3.Cross(y1, z2))); if (overlaps[overlaps.Count - 1] < 0) { return(null); } overlaps.Add(CheckOBBAxis(a, b, Vector3.Cross(z1, x2))); if (overlaps[overlaps.Count - 1] < 0) { return(null); } overlaps.Add(CheckOBBAxis(a, b, Vector3.Cross(z1, y2))); if (overlaps[overlaps.Count - 1] < 0) { return(null); } overlaps.Add(CheckOBBAxis(a, b, Vector3.Cross(z1, z2))); if (overlaps[overlaps.Count - 1] < 0) { return(null); } // If all axis are overlaping, then we have a collision ReportCollisionToParent(a, b); return(new CollisionInfo(a, b, CollisionResolution.GetFinalPenetration(overlaps))); }