void CollisionWithTheFence(double nextX, double nextY, Fence fence) { // Отскок сбоку if (this.Bottom() >= fence.Top() && (this.Right() > fence.Left() && this.Left() < fence.Right() || nextX + this.RealWidth() > fence.Left() && nextX < fence.Right())) Velocity.X = -Velocity.X; // Отскок сверху double radius2 = Math.Pow(this.RealWidth() / 2.0, 2); Vector center = new Vector(nextX + this.RealWidth() / 2.0, nextY + this.RealHeight() / 2.0); if (center.DistanceTo(new Vector(fence.Left(), fence.Top())) <= radius2 || center.DistanceTo(new Vector(fence.Right(), fence.Top())) <= radius2) Velocity.Y = -Velocity.Y; }
public double DistanceTo(Vector other) { return Math.Pow(X - other.X, 2) + Math.Pow(Y - other.Y, 2); }