les say i want collision between two rect than i cant set up bounds on both rect:

public Rectangle getBounds(){ 
   return new Rectangle(x, y, width, height); 
}

than to check for the collision i can do this:

if(rectOne.getBounds().intersects(rectTwo.getBounds())
{ ...}

now les say what if i want 2d array:

0, 0, 2, 2, 0
1, 0, 2, 0, 2

i want to check for collision between '1'(player) and '2'(walls). i think of doing this by same idea as top of my code.
'rect' is a arraylist
minus cameraX or cameraY is to move the screen.

public void getBounds(){
    for(int y = 0; y < map01.length; y++){
        for(int x = 0; x < map01[y].length; x++){
            if(map01[y][x] == 2) {
               rect.add(new Rectangle(x * tileWidth - cameraX, y * tileHeight - cameraY, tileWidth, tileHeight));
              }
            }
        }    
    }

now to check for collision:
player is '1' and arraylist should have all the bounds for walls '2'

       this.getBounds();

        for(int i = 0; i < rect.size(); i++)
        {
            if(player.getBounds().intersects(this.rect.get(i)))
                System.out.println("COLLISION");
            else
               System.out.println("NO COLLISION");
        }

the problem is this is that for some reason it is only printing "NO COLLISION". Is this bc of for loops or arraylist?

Recommended Answers

All 11 Replies

If the player is in one tile, and the wall is in another tile, and the Rectanges correspond to whole tiles, and one tile cannot be both 1 (player) and 2 (wall), then I don't see how any of there rectangles will ever intersect. Touch, yes, but overlap, no. Maybe you should print out the Rectanges to see if they ever have overlapping coordinates.
And just out of curiosity, how can a collision depend on where the camera happens to be?

i have question on same topic. below i am creating some rectangles and storing in arraylist. now can i print these arrayList rectangle? bc i think these arrayList rect are created in wrong postion.

create arrayList of rect

ArrayList<Rectangle> rect = new ArrayList<Rectanlge>();

    public void getBounds(){
        for(int y = 0; y < map01.length; y++){
            for(int x = 0; x < map01[y].length; x++){
                if(map01[y][x] == 2) {
                   rect.add(new Rectangle(x * tileWidth - cameraX, y * tileHeight - cameraY, tileWidth, tileHeight));
                  }
                }
            }    
        }

in paint method how can i print this arrayList?

for(int y = 0; y < rect.size(); y++)
                       {
                        Rectangle r = (Rectangle)rect.get(y);
                        //i cant do r.paint(g); // this isnt in main method.
                         // i was thinking r.drawRect some thing

                    }

Why not just print the Rectangles' coordinates to System.out to see if they are ok?

bc no it has x and y but its a single for loop.

for(int y = 0; y < rect.size(); y++)
                       {
                       g.drawRect(x * tileWidth - cameraX, y * tileHeight - cameraY, tileWidth, tileHeight);

                    }

I don't understand.
After you have populated rect just System.out.println(rect); so you can see and check the values

o found the problem i think. the problem is here. i am testing player intersects with the whole arraylist. so if arraylist size is 20 than it will print "hi" once and it will print nothing 19 times bc no else.

> for(int i = 0; i < rect.size(); i++)
>         {
>             if(player.getBounds().intersects(this.rect.get(i)))
>                 System.out.println("COLLISION");
>             else
>                System.out.println("NO COLLISION");
>         }

so i was thinking some thing like this. this way it will only test if player is intersect any rect in arraylist.

for(int i = 0; i < rect.size(); i++)
{
      if(p.getBounds().intersects(this.rect.get(i))){
           flag = true;
          break;
      }
    else
       flag = false;
}

System.out.println(flag);
if(flag )
   System.out.println("Collision");
else
System.out.println("no collision");

**now flag prints:

true
false
false
false
....**

it should prints true, true, true, true, true, bc player is touching the ground rect from arraylist.

I don't think "touching" is what the intersects method tests. AFAIK the two rectangles must overlap for intersects to return true.
ps This still looks crazy. The player and wall Rectangles will only overlap if a cell in map1 is both 1 and 2. :~

ah ok. how to check for collision?

i just learn about tile map collision from google and having hard time understanding other people code.

also i was using intersect method on google people were using it.

I'd like to answer that but, without understanding your overall design, I can't.
If the player just moves from square to square (like PacMan) then you just need to test the square in the direction that you are trying to move, eg

 if (moveUpRequested) 
    if (map[player.x][player.y-1] != 0) // can't move upwards
 // ditto for left, right, down

Youu would use intersect if yuo have the kind of game where things can move at any speed in any direction.

ah ok i think i start to understand it now:

map01

00000
10000
22222




public void levelWCollision(){
        /*** FIND PLAYER POSTION ***/
            int y = 0;
            int x = 0;
            for(y = 0; y < map01.length; y++){
                for( x = 0; x < map01[y].length; x++){
                    if(map01[y][x] == 1){
                        break;
                    }
                }
            }

            //player is standing ontop of ground
            if(map01[y+1][x] == 2){
                System.out.println("WORKS");
            }

}

but getting a error on line 114:

if(map01[y+1][x] == 2){

full error:

Exception in thread "AWT-EventQueue-1" java.lang.ArrayIndexOutOfBoundsException: 14
    at Level.levelWCollision(Level.java:114)
    at Main.actionPerformed(Main.java:123)
    at javax.swing.Timer.fireActionPerformed(Unknown Source)
    at javax.swing.Timer$DoPostEvent.run(Unknown Source)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

You exit the loop lines 12-18 with y = map01.length then on line 21 you have map01[y+1] so yes, index out of bounds.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.