public void UpdateRectangle(PhysicsRectangle a, int Id) { if (a.Hit == null) { a.Hit = new object[4]; } PhysicsRectangles[Id] = a; }
public int AddRectangle(PhysicsRectangle a) { if (a.Hit == null) { a.Hit = new object[4]; } return(PhysicsRectangles.Add(a)); }
void BroadPhase(float Delta) { PossibleContacts.Clear(); ActualContacts.Clear(); for (int i = 0; i < PhysicsRectangles.Count; i++) { PhysicsRectangle a = PhysicsRectangles[i]; for (int j = 0; j < 4; j++) { a.Hit[j] = null; } PhysicsRectangles[i] = a; } for (int i = 0; i < PhysicsRectangles.Count; i++) { for (int j = 0; j < PhysicsRectangles.Count; j++) { if (i != j) { PhysicsRectangle a = PhysicsRectangles[i].Clone(); PhysicsRectangle b = PhysicsRectangles[j].Clone(); float AMinX = Math.Min(a.X, a.X + (a.VelX * Delta)); float AMinY = Math.Min(a.Y, a.Y + (a.VelY * Delta)); float AMaxX = Math.Max(a.X + a.SizeX, a.X + a.SizeX + (a.VelX * Delta)); float AMaxY = Math.Max(a.Y + a.SizeY, a.Y + a.SizeY + (a.VelY * Delta)); float BMinX = Math.Min(b.X, b.X + (b.VelX * Delta)); float BMinY = Math.Min(b.Y, b.Y + (b.VelY * Delta)); float BMaxX = Math.Max(b.X + b.SizeX, b.X + b.SizeX + (b.VelX * Delta)); float BMaxY = Math.Max(b.Y + b.SizeY, b.Y + b.SizeY + (b.VelY * Delta)); a.SizeX = Math.Abs(AMaxX - AMinX); a.SizeY = Math.Abs(AMaxY - AMinY); a.X = AMinX; a.Y = AMinY; b.SizeX = Math.Abs(BMaxX - BMinX); b.SizeY = Math.Abs(BMaxY - BMinY); b.X = BMinX; b.Y = BMinY; if (CollisionUtils.RectangleIntersect(a, b)) { PossibleContacts.Add(new Tuple <int, int>(i, j)); } } } } }
void UpdatePosition(float Delta) { for (int i = 0; i < PhysicsRectangles.Count; i++) { PhysicsRectangle a = PhysicsRectangles[i]; if (a.Mass > 0) { a.X = a.X + (a.VelX * Delta); a.Y = a.Y + (a.VelY * Delta); if (a.Hit[(int)Direction.Down] == null) { a.VelX += (GravityX * Delta); a.VelY += (GravityY * Delta); } else { a.VelX *= a.Friction; a.VelY *= a.Friction; } } PhysicsRectangles[i] = a; } }
void NarrowPhase(float Delta) { for (int i = 0; i < PossibleContacts.Count; i++) { PhysicsRectangle a = PhysicsRectangles[PossibleContacts[i].Item1]; PhysicsRectangle b = PhysicsRectangles[PossibleContacts[i].Item2]; Contact c = CollisionUtils.RectangleVSRectangle(a, b, Delta); //Console.WriteLine($"Contact: {c.Collided} Normal: x:{c.NX} y:{c.NY} ContactPoint: x:{c.X} y:{c.Y} ContactTime: {c.TimeOfContact}"); if (c.Collided) { if (c.TimeOfContact >= 1) { continue; } if (c.NX < 0) { a.Hit[(int)Direction.Right] = PhysicsRectangles[PossibleContacts[i].Item2].Parent; } else { a.Hit[(int)Direction.Right] = null; } if (c.NX > 0) { a.Hit[(int)Direction.Left] = PhysicsRectangles[PossibleContacts[i].Item2].Parent; } else { a.Hit[(int)Direction.Left] = null; } if (c.NY < 0) { a.Hit[(int)Direction.Down] = PhysicsRectangles[PossibleContacts[i].Item2].Parent; } else { a.Hit[(int)Direction.Down] = null; } if (c.NY > 0) { a.Hit[(int)Direction.Up] = PhysicsRectangles[PossibleContacts[i].Item2].Parent; } else { a.Hit[(int)Direction.Up] = null; } a.VelX += c.NX * Math.Abs(a.VelX) * (1 - c.TimeOfContact); a.VelY += c.NY * Math.Abs(a.VelY) * (1 - c.TimeOfContact); PhysicsRectangles[PossibleContacts[i].Item1] = a; ActualContacts.Add(c); } else { PossibleContacts.RemoveAt(i); } } }