/// <summary> /// Checks if two objects are equal. /// </summary> /// <param name="obj">The object to check against.</param> /// <returns>Returns true if they are equal, otherwise false.</returns> public override bool Equals(object obj) { OverlapPair other = (OverlapPair)obj; return(other.Entity1.Equals(Entity1) && other.Entity2.Equals(Entity2) || other.Entity1.Equals(Entity2) && other.Entity2.Equals(Entity1)); }
private void DirtySortAxis(List <SweepPoint> axis) { axis.Sort(QuickSort); activeList.Clear(); for (int i = 0, length = axis.Count; i < length; i++) { SweepPoint keyelement = axis[i]; if (keyelement.Begin) { foreach (IBroadphaseEntity body in activeList) { if (CheckBoundingBoxes(body, keyelement.Body)) { //fullOverlaps.Add(new OverlapPair(body, keyelement.Body)); OverlapPair pair = OverlapPair.Pool.GetNew(); pair.Entity1 = body; pair.Entity2 = keyelement.Body; fullOverlaps.Add(pair); } } activeList.Add(keyelement.Body); } else { activeList.Remove(keyelement.Body); } } }
/// <summary> /// Tells the collisionsystem to check all bodies for collisions. Hook into the /// <see cref="CollisionSystem.PassedBroadphase"/> /// and <see cref="CollisionSystem.CollisionDetected"/> events to get the results. /// </summary> public override void Detect() { if (addCounter > AddedObjectsBruteForceIsUsed) { fullOverlaps.Clear(); DirtySortAxis(axis1); DirtySortAxis(axis2); DirtySortAxis(axis3); } else { sortCallback(axis1); sortCallback(axis2); sortCallback(axis3); } addCounter = 0; for (int index = 0, length = fullOverlaps.Count; index < length; index++) { OverlapPair pair = fullOverlaps[index]; if (this.CheckBothStaticNonKinematic(pair.Entity1, pair.Entity2)) { continue; } if (base.RaisePassedBroadphase(pair.Entity1, pair.Entity2)) { Detect(pair.Entity1, pair.Entity2); } } }
private void SortAxis(List <SweepPoint> axis) { for (int j = 1; j < axis.Count; j++) { SweepPoint keyelement = axis[j]; FP key = keyelement.Value; int i = j - 1; while (i >= 0 && axis[i].Value > key) { SweepPoint swapper = axis[i]; if (keyelement.Begin && !swapper.Begin) { if (CheckBoundingBoxes(swapper.Body, keyelement.Body)) { lock (fullOverlaps) { //fullOverlaps.Add(new OverlapPair(swapper.Body, keyelement.Body)); OverlapPair pair = OverlapPair.Pool.GetNew(); pair.Entity1 = swapper.Body; pair.Entity2 = keyelement.Body; fullOverlaps.Add(pair); } } } if (!keyelement.Begin && swapper.Begin) { lock (fullOverlaps) { //fullOverlaps.Remove(new OverlapPair(swapper.Body, keyelement.Body)); OverlapPair pair = OverlapPair.Pool.GetNew(); pair.Entity1 = swapper.Body; pair.Entity2 = keyelement.Body; fullOverlaps.Remove(pair); } } axis[i + 1] = swapper; i = i - 1; } axis[i + 1] = keyelement; } }
public override bool RemoveEntity(IBroadphaseEntity body) { int count; count = 0; for (int i = 0, length = axis1.Count; i < length; i++) { if (axis1[i].Body == body) { count++; axis1.RemoveAt(i); if (count == 2) { break; } i--; } } count = 0; for (int i = 0, length = axis2.Count; i < length; i++) { if (axis2[i].Body == body) { count++; axis2.RemoveAt(i); if (count == 2) { break; } i--; } } count = 0; for (int i = 0, length = axis3.Count; i < length; i++) { if (axis3[i].Body == body) { count++; axis3.RemoveAt(i); if (count == 2) { break; } i--; } } foreach (var pair in fullOverlaps) { if (pair.Entity1 == body || pair.Entity2 == body) { deprecated.Push(pair); } } while (deprecated.Count > 0) { OverlapPair pair = deprecated.Pop(); fullOverlaps.Remove(pair); OverlapPair.Pool.GiveBack(pair); } bodyList.Remove(body); return(true); }
public override bool RemoveEntity(IBroadphaseEntity body) { int count; count = 0; for (int i = 0, length = axis1.Count; i < length; i++) { if (axis1[i].Body == body) { count++; axis1.RemoveAt(i); if (count == 2) { break; } i--; } } count = 0; for (int i = 0, length = axis2.Count; i < length; i++) { if (axis2[i].Body == body) { count++; axis2.RemoveAt(i); if (count == 2) { break; } i--; } } count = 0; for (int i = 0, length = axis3.Count; i < length; i++) { if (axis3[i].Body == body) { count++; axis3.RemoveAt(i); if (count == 2) { break; } i--; } } for (int index = 0, length = fullOverlaps.Count; index < length; index++) { OverlapPair pair = fullOverlaps[index]; if (pair.Entity1 == body || pair.Entity2 == body) { deprecated.Push(pair); } } fullOverlaps.RemoveAll(deprecated); bodyList.Remove(body); return(true); }