Collision Detection between two objects

Page 1 of 1

1 Replies - 684 Views - Last Post: 28 April 2013 - 04:40 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=319749&amp;s=db19c9792ffcfdda72587d2d1acdb720&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 qobacha

Reputation: 0
• Posts: 9
• Joined: 27-April 12

Collision Detection between two objects

Posted 28 April 2013 - 04:17 AM

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:

```
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);

//foreach (Car c in cars)
//{
//    c.MoveCar(this.Width);
//}

this.Invalidate();
}
}

```

Is This A Good Question/Topic? 0

Replies To: Collision Detection between two objects

#2 Skydiver

• Code herder

Reputation: 3965
• Posts: 12,584
• Joined: 05-May 12

Re: Collision Detection between two objects

Posted 28 April 2013 - 04:40 AM

You need to let your Car (and possibly Player) class return a Rectangle property the represents their bounds. When you have the Rectangle, you can use IntersectsWith():
http://msdn.microsof...rsectswith.aspx

The C# compiler would just automatically deduce that you've got the bit of data that represents a Rectangle and automatically make a Rectangle for you. You'll need to write code to create that Rectangle.