You are viewing our Forum Archives. To view or take place in current topics click here.
Give each rectangle a collision detection? [XNA] [VB/C#]
Posted:

Give each rectangle a collision detection? [XNA] [VB/C#]Posted:

XBLToothPik
  • Rated Awesome
Status: Offline
Joined: Oct 31, 201014Year Member
Posts: 2,084
Reputation Power: 95
Status: Offline
Joined: Oct 31, 201014Year Member
Posts: 2,084
Reputation Power: 95
Never really worked with XNA before, so I wanted to try something simple, I got all the basic controls down for character movement and stuff, and the 'map'/'maze' for the character to go through, I am doing the Rectangle.Intersects() method, which I know isn't the best to be doing but all of the walls/maps are made up of rectangles so that's why I'm using that. Now when drawing the map/putting the objects onto the screen, how exactly do I give each rectangle it's own rectangle/collision detection, to stop me from having to write code for each and every rectangle that I want on the screen.

Cutting me from doing this

If PlayerRect.Intersects(Rect1.Boundaries) Then MsgBox("You Lose!")
if PlayerRect.Intersects(Rect2.Boundaries) Then MsgBox("You Lose!")


To Something like this

If PlayerRect.Intersects(AllRects.Boundaires) Then MsgBox("You Lose!")


I would imagine it would be something like this

For Each rect As Rectangle in...
     if Player.Intersects(rect) Then MsgBox("You Lose")
Next

Or throwing them all into an array
^^^^ Or something close to that!

Thanks TTG!
#2. Posted:
Crunchy_Nut
  • Wise One
Status: Offline
Joined: May 04, 201014Year Member
Posts: 554
Reputation Power: 55
Status: Offline
Joined: May 04, 201014Year Member
Posts: 554
Reputation Power: 55
You would need to place them in an Array. Don't use XNA at all so I don't know if it is the same syntax as Java, but it would be something like this:

private ArrayList<Rectangle> rectangleList = new ArrayList<Rectangle>();

After that you need to add the rectangles to store into the Array.. Same as before not sure if the syntax is the same:


rectangleList.add(rectangle);

You will need to change the (rectangle) to the name of the Rectangle you made. Just keep adding them like that.

For detecting the collision I have no idea. This is most likely horribly wrong but I would have tried something like this first. Again its Java. If its not same syntax then try convert it

   for (int i = 0; i < rectList.size(); i++)
        {
           if(rect1.intersects(rectList.get(i)))
           {
              //do stuff
           }
        }


If that doesn't work try:

       for (int i = 0; i < rectList.size(); i++)
        {
              if(rect1.intersects(rectList.get(i).getX(), rectList.get(i).getY(), rectList.get(i).getWidth(), rectList.get(i).getHeight()))
              {
                 //do some stuff
              }
        }


Sorry if this is wrong, Im just taking a little guess.
#3. Posted:
A4_Games
  • New Member
Status: Offline
Joined: May 01, 201212Year Member
Posts: 17
Reputation Power: 0
Status: Offline
Joined: May 01, 201212Year Member
Posts: 17
Reputation Power: 0
It looks like you're trying to use VB. Are you using a forms application or a Windows/Xbox Game?

You can use a regular array like this:

private Rectangle[] rects = new Rectangle[10];
then
rects[0] = new rectangle(x, y, width, height);
etc.

and in update:
foreach (Rectangle rect in rects)
if (playerRect.Intersects(rect))
//do stuff
}

Using an ArrayList wouldn't be too different I think, but I haven't used them much.
If you want to use an ArrayList try something like this:

Add: using System.Collections;

Declare: private ArrayList rects = new ArrayList();
Not sure, but I think do something like:
Rectangle rect1 = new Rectangle(x, y, width, height);
rects.Add(rect1);

And in update:
foreach (Rectangle rect in rects)
{
if (playerRect.Intersects(rect))
//do stuff
}


Sorry, I tend to overexplain things and then I get tripped up. Sorry if I can't help, it's good if I can.
Also it's good to see you using XNA.
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.