protected virtual void CheckBorders() { if (Position.X > Game1.Width) { Position = new Vector2(Game1.Width, Position.Y); Velocity = Velocity.FlipX(); } if (Position.X < 0) { Position = new Vector2(0, Position.Y); Velocity = Velocity.FlipX(); } if (Position.Y > Game1.Height) { Position = new Vector2(Position.X, Game1.Height); Velocity = Velocity.FlipY(); } if (Position.Y < 0) { Position = new Vector2(Position.X, 0); Velocity = Velocity.FlipY(); } }
private Vector2 Move(float time) { Vector2 newPosition = Position + _velocity.GetMovement() * time; var boundingBox = new Rectangle(Point.Zero, _clientBounds.Size); if (newPosition.X + _radius > boundingBox.Right) { float percentageBeforeCollide = (boundingBox.Right - _radius - Position.X) / (newPosition.X - Position.X); if (percentageBeforeCollide < 0.99) { float timeBeforeCollide = percentageBeforeCollide * time; Move(timeBeforeCollide); time -= timeBeforeCollide; } _velocity.FlipX(); newPosition = Move(time); } else if (newPosition.X - _radius < 0) { float percentageBeforeCollide = (_radius - Position.X) / (newPosition.X - Position.X); if (percentageBeforeCollide < 0.99) { float timeBeforeCollide = percentageBeforeCollide * time; Move(timeBeforeCollide); time -= timeBeforeCollide; } _velocity.FlipX(); newPosition = Move(time); } if (newPosition.Y + _radius > _clientBounds.Height) { float percentageBeforeCollide = (boundingBox.Bottom - _radius - Position.Y) / (newPosition.Y - Position.Y); if (percentageBeforeCollide < 0.99) { float timeBeforeCollide = percentageBeforeCollide * time; Move(timeBeforeCollide); time -= timeBeforeCollide; } _velocity.FlipY(); newPosition = Move(time); } else if (newPosition.Y - _radius < 0) { float percentageBeforeCollide = (_radius - Position.Y) / (newPosition.Y - Position.Y); if (percentageBeforeCollide < 0.99) { float timeBeforeCollide = percentageBeforeCollide * time; Move(timeBeforeCollide); time -= timeBeforeCollide; } _velocity.FlipY(); newPosition = Move(time); } return(newPosition); }