i want to set a collision on my tile map using getBounds function.

level class

int map[][] = {
               {2, 0, 0}
               {1, 1, 0}
               };

2 = player
1 = ground
0 = sky
i want to set collision so player cant go though ground. so he should fall at end of map.

in player i just did this:

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

bc there is x and y for player.

in level class each tile is 32X32. so i can add width and height
but i am not sure x and y:

 public Rectangle getBounds()
    { return  new Rectangle(, , tile_size,tile_size);}

do i have to use loop and if statment in that method ex:

public Rectangle getBounds()
{
    for()
    {
        for()
        {
             if(map[y][x] == 1)
             {
                return  new Rectangle(, , tile_size,tile_size);
            }
        }
    }    
}

and to check if they are collsion

if(player_class.getBounds().intersects(level_class.getBounds()))
        {
            System.out.println("collsion");
        }

Maybe this is revealing that the map[][] approach isn't a good one? If you defined ground as ab object like the movable ones, this would be easy?

so far what i have is this:

ArrayList rect = null;
public void getBounds1()
    {
        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*32, y*32, tile_size, tile_size));
                }
            }
        }    
    }
public ArrayList getRect()
{ return rect; }

if(p.getBounds().intersects(this.getRect()) )
        {
            System.out.println("x");
            p.setY(on_ground - p.getHEIGHT());
        }

this gives a error at "intersects"
he method intersects(Rectangle) in the type Rectangle is not applicable for the arguments (ArrayList)

i know what the error means, its arraylist and i need to give Rectangle. but how can i fix this.

also tried ArrayList<Rectangle>

Not sure what you're doing here, but to use intersects with an ArrayList you would have to loop thru the arraylist calling intersects for each of the (Rectangle) elements in the list

i looping though arraylist rect here.
and getting a error

for(int i = 0; i < rect.size(); i++)
    {
        if(p.getBounds().intersects((Rectangle) this.getRect().get(i)) )
        {
        p.setY(on_ground - p.getHEIGHT());
        }
    }

scatch that my way is not working so let me try your way

Maybe this is revealing that the map[][] approach isn't a good one? If you defined ground as ab object like the movable ones, this would be easy?

can you plz expand on this?

Here's a very simple demo of an animation with multiple things ("Sprites" moving about). YOu'll akready be familiar with many of the building blocks, so it shouldn't be too hard to read...

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.TimerTask;
import javax.swing.*;

public class Animation1 extends JPanel {
   // Basic architecture demo for multi-sprite animation

   public static void main(String[] args) {
      // create and display the animation in a JFrame
      final JFrame frame = new JFrame("Animation 1 (close window to exit)");
      Animation1 animationPanel = new Animation1(600, 400);
      frame.add(animationPanel);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);
   }

   private ArrayList<SimpleSprite> sprites = new ArrayList<SimpleSprite>();

   final static int TIMER_MSEC = 30; // mSec per clock tick

   Animation1(int width, int height) {
      setPreferredSize(new Dimension(width, height));
      sprites.add(new SimpleSprite(Color.blue, 0, 35, 2, 1));
      sprites.add(new SimpleSprite(Color.red, 0, 250, 2, -1));
      startTimer();
   }

   void startTimer() {
      // use java.util Timer rather than javax.swing Timer
      // to avoid running intensive simulation code on the swing thread
      java.util.Timer timer = new java.util.Timer();
      TimerTask timerTask = new TimerTask() {
         @Override
         public void run() {
            updateSimulation();
         }
      };
      timer.scheduleAtFixedRate(timerTask, 0, TIMER_MSEC);
   }

   public void updateSimulation() {
      // called by Timer every TIMER_MSEC milliseconds
      for (SimpleSprite s : sprites) {
         s.update(); // update position of the sprite
      }
      repaint(); // request Swing to schedule re-paint of screen
   }

   @Override
   public void paintComponent(Graphics g) {
      // screen refresh - called by Swing as needed
      Graphics2D g2d = (Graphics2D) g;
      paintBackground(g2d);
      for (SimpleSprite s : sprites) {
         s.draw(g2d);// draw the sprite at latest positions
      }
   }

   void paintBackground(Graphics2D g2d) {
      // could be an image file etc, this is just plain grey
      g2d.setColor(Color.LIGHT_GRAY);
      g2d.fillRect(0, 0, getWidth(), getHeight());
   }



}

class SimpleSprite {
   // basic x,y movement at constant velocity

   float x, y, dx, dy; // position and velocity (pixels/TIMER_MSEC)
   Color color;

   public SimpleSprite(Color color, float x, float y, float dx, float dy) {
      // initial position and velocity
      this.color = color;
      this.x = x;
      this.y = y;
      this.dx = dx;
      this.dy = dy;
   }

   public void update() { // update position and velocity every n milliSec
      x += dx; // velocity in x direction
      y += dy; // velocity in y direction
   }

   public void draw(Graphics2D g2d) {
      g2d.setColor(color);
      g2d.fillOval((int) x, (int) y, 100, 100); // draw this at latest position.
   }

}

The next step is to add something like a public Rectangle getBounds() method to Sprite, so then at each update you can loop thru each apir of Sprites to see if any have touched/collided.
A Sprite doesn't have to move, so you can use Sprite for the ground or a mountain, or wall etc and still have just one piece of code that finds all the collisions.
You can also sub-class Sprite to have specialised code for things that are very solid, things that bounce, things that explode when they collide etc etc

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.