Hi,
I need to implement collision detection algorithm for the small game I'm developing. The objects i have here is ball/circle and a rectangle. The balls are moving vertically, while the rectangle is moving horizontally. The condition is that if the ball and rectangle touch each other then an event should be raised. I have been trying to do that for a while without success. This is my first program in C# so please bear with me.
Here is my code for the two objects that i want to check their collision
Code:class Ball { public float x, y, yvel, radius; public Brush brush; public Ball(int gamewidth,int gameHeight,Random r) { x = r.Next(gamewidth); y = r.Next(gameHeight); yvel = r.Next(2) + 5; radius = r.Next(10) + 5; brush = new SolidBrush(Color.Blue); } public void Move(int gameHeight) { if (y + radius >= gameHeight) { y = 0; } y += yvel; } public void Draw(Graphics g) { g.FillEllipse(brush, new RectangleF(x-radius,y - radius, radius * 2, radius * 2)); } } class Player { public float x, y, xvel; public Brush brush; public Player() { } public void DrawPlayer(Graphics g) { g.FillRectangle(brush, new RectangleF(x, y, 30,30)); } public void MovePlayerLeft(int gameWidth) { if (x > 0) { x -= xvel; } } }




Reply With Quote