Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can someone help me on how i can detect collision between two objects. At first i thought IntersectsWith would help but i get the error saying my solution doesnot contain the definition for IntersectsWith. Here is my code that gives me errors:

C#
class Car
    {
        private int _x,_y, _width, _height,_xvel;

     
        public Car(Random r, int y, int width, int height, int xvel)
        {
            this._x = r.Next(500)+20;
            //this._x = x;
            this._y = y;
            this._width = width;
            this._height = height;
            this._xvel = xvel;
        }

        public int X { get { return _x; } }
        public int Y { get { return _y; } }
        public int Width { get { return _width; } }
        public int Height{ get { return _height; } }
        public int Xvel { get { return _xvel; } }
    
        public void DrawCar(Graphics g)
        {
            g.DrawRectangle(Pens.Blue, new Rectangle(X, Y, Width, Height));            
        }
     
        public void MoveCar(int gamewidth)
        {
            if( _x + _width >= gamewidth)
            {              
                _x = 0;
            }
            _x = _x + _xvel;        
        }        
    }

    class Player
    {
         private int _x, _y, _width, _height, _xvel,_yvel;  

       
         public Player(int x, int y, int width,int height, int xvel, int yvel)
        {
            this._x = x;
            this._y = y;
            this._width = width;
            this._height = height;
            this._xvel = xvel;
            this._yvel = yvel;
        }

        public int X { get { return _x; } }
        public int Y { get { return _y; } }
        public int Width { get { return _width; } }
        public int Height{ get { return _height; } }
        public int Xvel { get { return _xvel; } }
        public int Yvel { get { return _yvel; } }
    
        public void DrawPlayer(Graphics g)
        {
            g.DrawRectangle(Pens.Red, new Rectangle(X, Y, Width, Height));            
        }

        public void CollisionDetection(Rectangle player, Rectangle car)
        {
            if (player.IntersectsWith(car))
            {
                MessageBox.Show("You Lose!");                
            }

        }      

        public void MovePlayerLeft(int gamewidth)
        {
            if (_x > 0)
            {
                _x -= _xvel;
            }
        }
    }

public partial class Form1 : Form
    {     
      
        Car cars;

       Player player;    

        public Form1()
        {
            InitializeComponent();
          
            player = new Player(((Width / 2) - 15), (Height - 75),30, 30, 10, 10);
            
            Random r = new Random();
            cars = new Car(r, 200, 30, 30, 10);         
            
        }
      
        private void Form1_Paint(object sender, PaintEventArgs e)
        {            
                Graphics g = e.Graphics;
                g.Clear(Color.White);
                cars.DrawCar(g);
                player.DrawPlayer(g);
                player.CollisionDetection(player, cars); //This is the part that implements collision detection
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            cars.MoveCar(this.Width);
            this.Invalidate();
        }
    }
Posted

Looks pretty straight fporward, perhaps you ned to draw the graphics first?
http://msdn.microsoft.com/en-us/library/system.drawing.rectangle.intersectswith.aspx[^]
 
Share this answer
 
Comments
Qobacha 28-Apr-13 8:27am    
I've already drawn the graphics on cars.DrawCar(g) and player.DrawPlayer(g). But when calling player.CollisionDetection(player, cars); i got the error saying thesolution doesnot contain the definition for IntersectsWith. So its clear the player and car objects aren't recognized as Rectangle objects. Maybe the question can be better put as how can I return car and player from both classes as Rectangle objects?
You seem a little confused here:
C#
player.CollisionDetection(player, cars);

C#
Car cars;
Player player;

C#
class Player
{
...
    public void CollisionDetection(Rectangle player, Rectangle car)
    {
        if (player.IntersectsWith(car))
...
So, since Player isn't related to Rectangle, you can't pass it to your collision detection method.
Which is probably something to do with the error message you get: your Player class does not implement IntersectsWith so you can't call it.

I think you need to take a step back, and look at deriving some of your classes from base classes, possibly rectangle (though I suspect that would be a poor move - I would have an abstract "Sprite" class or similar to derive both from, which might happen to contain a rectangle, or possibly a Region for later use, and which does the collision detection for derived classes)
 
Share this answer
 
Comments
Qobacha 28-Apr-13 8:36am    
I got what you are saying. But is in there a workaround solution for this? Maybe trying to make both car and player class return a Rectangle.
OriginalGriff 28-Apr-13 9:25am    
You could, but why?
If you create a Sprite class and derive both from it, then you can treat them as the same kind of object when you do common things: they both move, they can both intersect which other sprites and so forth. Why artificially return a shape (that doesn't describe either of the objects that well and that would be a pain to change at a later date if you decide you need to) when you could use a generic class and have it (and only it) aware of what it needs?
Qobacha 29-Apr-13 5:23am    
Thanks a lot. I will try it the way you suggested and see how far i'll go.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900