Results 1 to 6 of 6
  1. #1
    Join Date
    Nov 2012
    Posts
    29
    Rep Power
    0

    Angry Collision Detection HOW!? Im using intersects(Rectangle)

    Hi, so I was making this game, It is just an alien moving around and getting apples. I want the apple to disappear and reappear at another random location when the rectangle of the apple and the alien intersect, what am I doing wrong?

    This is the Main class
    Java Code:
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Rectangle;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    
    import java.util.Random;
    
    import javax.swing.JPanel;
    import javax.swing.Timer;
    
    public class Main extends JPanel implements ActionListener {
    	private static final long serialVersionUID = 1L;
    
    	private int oPx = 0;
    	private int oPy = 0;
    	private int oPw = 0;
    	private int oPh = 0;
    	private int Px = 0;
    	private int Py = 0;
    	private int randX = 0;
    	private int randY = 0;
    	private int Pw = 0;
    	private int Ph = 0;
    	
    	private boolean aIsVisible = true;
    	private boolean isDebug = false;
    	private boolean isPlayer = true;
    	private boolean isRestarted = false;
    	
    	private String mainTitle = "NotEnoughFood";
    
    	public Timer tick;
    	
    	public Random rand = new Random();
    	
    	public Player PlayerObj;
    	private Apple a = new Apple(12, 12);
    
    	public Main() {
    		tick = new Timer(5, this);
    		
    		PlayerObj = new Player();
    		
    		randX = rand.nextInt(450) + 1;
    		randY = rand.nextInt(450) + 1;
    		
    		this.addKeyListener(new TAdapter());
    		setFocusable(true);
    		setDoubleBuffered(true);
    
    		tick.start();
    	}
    
    	public void paint(Graphics g) {
    		super.paint(g);
    		Graphics2D g2d = (Graphics2D) g;
    		
    		Px = PlayerObj.getX();
    		Py = PlayerObj.getY();
    		Pw = PlayerObj.getWidth();
    		Ph = PlayerObj.getHeight();
    		
    		if(isRestarted){
    			Px = oPx;
    			Py = oPy;
    			Pw = oPw;
    			Ph = oPh;
    			
    			isRestarted = false;
    		}
    		g2d.drawImage(PlayerObj.getTexture(), Px, Py, Pw, Ph, this);
    		
    		checkCollision();
    		
    		g2d.drawImage(a.getTexture(), randX, randY, a.getWidth(), a.getHeight(), this);
    		
    		if (isDebug) {
    			
    		}
    	}
    	
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		if(isPlayer)
    			PlayerObj.actionPerformed(e);
    		Px = PlayerObj.getX();
    		Py = PlayerObj.getY();
    		
    		repaint();
    	}
    
    	class TAdapter extends KeyAdapter {
    
    		@Override
    		public void keyPressed(KeyEvent e) {
    			if(isPlayer)
    				PlayerObj.KeyPressed(e);
    		}
    
    		@Override
    		public void keyReleased(KeyEvent e) {
    			if(isPlayer)
    				PlayerObj.KeyReleased(e);
    		}
    	}
    	
    	public String getTitle() {
    		return mainTitle;
    	}
    	
    	public void checkCollision() {
    		Rectangle P = PlayerObj.getBounds();
    		Rectangle A = a.getBounds();
    		if(A.intersects(P)) {
    			System.out.println("INTERSECTS");
    			changeRand();
    		}	
    	}
    
    	public void changeRand() {
    		randX = rand.nextInt(450) + 1;
    		randY = rand.nextInt(450) + 1;
    		System.out.println("RandChanged");
    	}
    }
    and this is the alien(aka. Player)
    Java Code:
    import java.awt.Image;
    import java.awt.Rectangle;
    import java.awt.event.ActionEvent;
    import java.awt.event.KeyEvent;
    
    import javax.swing.ImageIcon;
    
    
    public class Player {
    	private int x = 50;
    	private int y = 50;
    	private int dx = 0;
    	private int dy = 0;
    	private int stm = 100;
    	private int w = 60;
    	private int h = 60;
    	
    	private boolean isCollision = false;
    	
    	private ImageIcon TexI = new ImageIcon(this.getClass().getResource("/Player.png"));
    	private Image Tex = TexI.getImage();
    	
    	public Player() {
    		
    	}
    	
    	public void KeyPressed(KeyEvent e) {
    		int Key = e.getKeyCode();
    		
    		if(Key == KeyEvent.VK_UP) {
    			dy = -1;
    		}
    		
    		if(Key == KeyEvent.VK_LEFT) {
    			dx = -1;
    		}
    		
    		if(Key == KeyEvent.VK_DOWN) {
    			dy = 1;
    		}
    		
    		if(Key == KeyEvent.VK_RIGHT) {
    			dx = 1;
    		}
    	}
    	
    	public void KeyReleased(KeyEvent e) {
    		int Key = e.getKeyCode();
    		
    		if(Key == KeyEvent.VK_UP) {
    			dy = 0;
    			System.out.println("Released: UP");
    		}
    		
    		if(Key == KeyEvent.VK_LEFT) {
    			dx = 0;
    			System.out.println("Released: LEFT");
    		}
    		
    		if(Key == KeyEvent.VK_DOWN) {
    			dy = 0;
    			System.out.println("Released: DOWN");
    		}
    		
    		if(Key == KeyEvent.VK_RIGHT) {
    			dx = 0;
    			System.out.println("Released: RIGHT");
    		}
    	}
    	
    	public void actionPerformed(ActionEvent e) {
    		move();
    	}
    	
    	public void move() {
    		x += dx;
    		y += dy;
    	}
    	public int getX() {
    		return x;
    	}
    	public int getY() {
    		return y;
    	}
    	public int getStam() {
    		return stm;
    	}
    	public int getHeight() {
    		return (int)h;
    	}
    	public int getWidth() {
    		return (int)w;
    	}
    	public Image getTexture() {
    		return Tex;
    	}
    	public void setDx(int setDxTo) {
    		dx = setDxTo;
    	}
    	public void setDy(int setDyTo) {
    		dy = setDyTo;
    	}
    	public Rectangle getBounds() {
    		return new Rectangle(x, y, w, h);
    	}
    	public void setWidth(int setWidthTo) {
    		w = setWidthTo;
    	}
    	public void setHeight(int setHeightTo) {
    		h = setHeightTo;
    	}
    }
    And the apple.
    Java Code:
    import java.awt.Image;
    import java.awt.Rectangle;
    
    import javax.swing.ImageIcon;
    
    public class Apple {
    	private ImageIcon TexI = new ImageIcon(this.getClass().getResource("/Apple.png"));
    	private Image Tex = TexI.getImage();
    	private int x;
    	private int y;
    	private int w = 10;
    	private int h = 10;
    	
    	public Apple(int X, int Y) {
    		x = X;
    		y = Y;
    	}
    	
    	public Image getTexture() {
    		return Tex;
    	}
    	
    	public int getWidth() {
    		return w;
    	}
    	public int getHeight() {
    		return h;
    	}
    	
    	public Rectangle getBounds() {
    		return new Rectangle(x, y, w, h);
    	}
    }
    And thats all, besides for the JFrame class, but that cannot have anything to do with it. So what do I do!?!?!?!?!

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Collision Detection HOW!? Im using intersects(Rectangle)

    What exactly is going wrong? Is there an error?

  3. #3
    Join Date
    Nov 2012
    Posts
    29
    Rep Power
    0

    Thumbs down Re: Collision Detection HOW!? Im using intersects(Rectangle)

    Quote Originally Posted by quad64bit View Post
    What exactly is going wrong? Is there an error?
    Well, it won't detect collision with intersects() on line 117, in the class, Main. No errors, just nothing happening.

  4. #4
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default Re: Collision Detection HOW!? Im using intersects(Rectangle)

    Java Code:
    public Rectangle getBounds() {
            return new Rectangle(x, y, x + w, y + h);
        }

  5. #5
    Join Date
    Nov 2012
    Posts
    29
    Rep Power
    0

    Default Re: Collision Detection HOW!? Im using intersects(Rectangle)

    Ok, that was obscure. First off, where do I put this code, and why x + w, and y + h? I decided to add them into all of the getBounds in my code, and there was nothing. I know since, nothing appeared in the console. Should this detect a collision, it will print out "INTERSECTS".

  6. #6
    Join Date
    Nov 2012
    Posts
    29
    Rep Power
    0

    Thumbs up Re: Collision Detection HOW!? Im using intersects(Rectangle)

    Ok, I found a solution. SOLVED. Sorry that I cant remember what I did, but I am pretty sure that I made a seperate idea to the getBounds() idea.

Similar Threads

  1. Rotated rectangle collision detection
    By Daslee in forum Java 2D
    Replies: 5
    Last Post: 10-25-2012, 02:30 PM
  2. Collision detection
    By mwr1976 in forum Java 2D
    Replies: 2
    Last Post: 04-14-2012, 01:45 PM
  3. Collision Detection
    By sunde887 in forum Java 2D
    Replies: 2
    Last Post: 10-08-2011, 12:40 AM
  4. Collision Detection?
    By Alerhau in forum New To Java
    Replies: 39
    Last Post: 09-07-2011, 05:55 PM
  5. Collision Detection
    By dotabyss in forum Java Gaming
    Replies: 0
    Last Post: 03-14-2010, 07:13 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •