public bool CheckForPlayerHit(Ball ball, Player player) { double playerTop = height - player.Height + 1; if (ball.Center.Y + ball.Radius >= playerTop) { double leftMost = player.PosX - player.SpriteWidth / 2; double rightMost = player.PosX + player.SpriteWidth / 2; if (ball.Center.X >= leftMost && ball.Center.X <= rightMost) return true; if (ball.Center.Y < playerTop) { double distance = 0; if (ball.Center.X < leftMost) distance = Math.Sqrt(Math.Pow(ball.Center.X - leftMost, 2) + Math.Pow(ball.Center.Y - playerTop, 2)); else distance = Math.Sqrt(Math.Pow(ball.Center.X - rightMost, 2) + Math.Pow(ball.Center.Y - playerTop, 2)); if (distance <= ball.Radius) return true; else return false; } else { if (ball.Center.X < leftMost) return ball.Center.X + ball.Radius >= leftMost; else return ball.Center.X - ball.Radius <= rightMost; } } else return false; }
public void CollideWithBall(Ball other) { if (!IntersectWithBall(other)) return; Tuple<int, int> velocityA = VelocityComponents(); Tuple<int, int> velocityB = other.VelocityComponents(); int newVelAx, newVelAy, newVelBx, newVelBy; newVelAx = (int)Math.Round(((1.0 * velocityA.Item1 * (mass - other.mass) + 2.0 * other.mass * velocityB.Item1) / (mass + other.mass))); newVelAy = (int)Math.Round(((1.0 * velocityA.Item2 * (mass - other.mass) + 2.0 * other.mass * velocityB.Item2) / (mass + other.mass))); newVelBx = (int)Math.Round(((1.0 * velocityB.Item1 * (mass - other.mass) + 2.0 * mass * velocityA.Item1) / (mass + other.mass))); newVelBy = (int)Math.Round(((1.0 * velocityB.Item2 * (mass - other.mass) + 2.0 * mass * velocityA.Item2) / (mass + other.mass))); speed = (int)Math.Sqrt(1.0 * Math.Pow(newVelAx, 2) + 1.0 * Math.Pow(newVelAy, 2)); other.speed = (int)Math.Sqrt(1.0 * Math.Pow(newVelBx, 2) + 1.0 * Math.Pow(newVelBy, 2)); angle = (int)Math.Round((180.0) / Math.PI * AngleFromVelocity(Tuple.Create(newVelAx, newVelAy))); other.angle = (int)Math.Round((180.0) / Math.PI * AngleFromVelocity(Tuple.Create(newVelBx, newVelBy))); Update(); }
private static int screenWidth = 800; // Width of Canvas screen #endregion Fields #region Methods public static void Main(string[] args) { CDrawer canvas = new CDrawer(screenWidth, screenHeight); // Initializes new Canvas window Random rng = new Random(); // Random number generator Ball example = new Ball(screenWidth, screenHeight, 25, 150, 100, 10, 1, 350); Ball example2 = new Ball(screenWidth, screenHeight, 25, 300, 300, 1, 1, 300); for (int i = 0; i < 10000; i++) { canvas.AddEllipse(example.centerX - example.radius, example.centerY - example.radius, 2 * example.radius, 2 * example.radius); canvas.AddEllipse(example2.centerX - example2.radius, example2.centerY - example2.radius, 2 * example2.radius, 2 * example2.radius); System.Threading.Thread.Sleep(5); canvas.Clear(); example.Update(); example.CollideWithWall(); example2.CollideWithWall(); example.CollideWithBall(example2); } Console.ReadKey(); }
public static void Main(string[] args) { ManualResetEvent Terminated = new ManualResetEvent(false); Initialize(); WriteLine("Press ESC to close the application when running."); OnKeyDown += (sender, e) => { if (e.Key == Key.Escape || (e.Key == Key.C && e.Control)) Terminated.Set(); }; // Sound borrowed from: http://www.freesound.org/people/davidou/sounds/88451/ int BoingSound = UploadAudioSample(GetResourceWavAudio("88451__davidou__boing.wav")); int BallTexture = AddSpriteTexture(GetResourceBitmap("Ball.png"), new System.Drawing.Point(25, 25), System.Drawing.Color.Black, true); System.Random Rnd = new System.Random(); Ball[] Balls = new Ball[20]; int i; for (i = 0; i < Balls.Length; i++) { Balls[i] = new Ball( CreateSprite(Rnd.Next(50, RasterWidth - 50), Rnd.Next(50, RasterHeight - 50), BallTexture), Rnd.Next(500000) - 250000, BoingSound); } OnUpdateModel += (sender, e) => { foreach (Ball Ball in Balls) Ball.Move(); }; while (!Terminated.WaitOne(1000)) ; Terminate(); }
private void ChangeBallDirection(Ball ball, int outOfBounds) { if ((outOfBounds & ((int)OutOfBounds.Left | (int)OutOfBounds.Right)) != 0) { ball.Direction = new Vector2(-ball.Direction.X, ball.Direction.Y); } if ((outOfBounds & ((int)OutOfBounds.Top | (int)OutOfBounds.Bottom)) != 0) { ball.Direction = new Vector2(ball.Direction.X, -ball.Direction.Y); } }
private bool CheckCollision(Ball ball, Laser laser) { bool horizontal = IsBetween((int)ball.Position.X, laser.CollisionRect.Left - (int)ball.Radius, laser.CollisionRect.Right + (int)ball.Radius); bool vertical = IsBetween((int)ball.Position.Y, laser.CollisionRect.Top - (int)ball.Radius, laser.CollisionRect.Bottom + (int)ball.Radius); return horizontal && vertical; }
public bool IntersectWithBall(Ball other) { double euclideanDistance = Math.Sqrt(1.0*(Math.Pow(centerX - other.centerX, 2) + Math.Pow(centerY - other.centerY, 2))); return euclideanDistance <= radius + other.radius ? true : false; }
public void BallDivide(Ball ballToDevide) { if (ballToDevide.Radius >= 10) { double oldRadius = ballToDevide.Radius; Point oldCenter = ballToDevide.Center; Point oldDirection = ballToDevide.Direction; Color oldColor = ballToDevide.Color; double oldSpeed = ballToDevide.Speed; double leftBallRadius = oldRadius / 2; Point leftBallCenter = new Point(oldCenter.X - oldRadius / 2, oldCenter.Y); Point leftBallDirection = new Point(-Math.Abs(oldDirection.X), oldDirection.Y); Color leftBallColor = oldColor; double leftBallSpeed = oldSpeed; Ball leftBall = new Ball(leftBallRadius, leftBallCenter, leftBallDirection, leftBallColor, leftBallSpeed); double rightBallRadius = oldRadius / 2; Point rightBallCenter = new Point(oldCenter.X + oldRadius / 2, oldCenter.Y); Point rightBallDirection = new Point(Math.Abs(oldDirection.X), oldDirection.Y); Color rightBallColor = oldColor; double rightBallSpeed = oldSpeed; Ball rightBall = new Ball(rightBallRadius, rightBallCenter, rightBallDirection, rightBallColor, rightBallSpeed); balls.Remove(ballToDevide); mainGrid.Children.Remove(ballToDevide); balls.Add(leftBall); mainGrid.Children.Add(leftBall); balls.Add(rightBall); mainGrid.Children.Add(rightBall); } else { mainGrid.Children.Remove(ballToDevide); balls.Remove(ballToDevide); } }