Results 1 to 6 of 6
- 11-20-2012, 01:57 AM #1
Member
- Join Date
- Nov 2012
- Posts
- 27
- Rep Power
- 0
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
and this is the alien(aka. Player)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 the apple.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 thats all, besides for the JFrame class, but that cannot have anything to do with it. So what do I do!?!?!?!?!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); } }
- 11-20-2012, 07:59 PM #2
Re: Collision Detection HOW!? Im using intersects(Rectangle)
What exactly is going wrong? Is there an error?
- 11-21-2012, 03:10 AM #3
Member
- Join Date
- Nov 2012
- Posts
- 27
- Rep Power
- 0
- 11-21-2012, 05:01 AM #4
Senior Member
- Join Date
- Oct 2012
- Posts
- 108
- Rep Power
- 0
Re: Collision Detection HOW!? Im using intersects(Rectangle)
Java Code:public Rectangle getBounds() { return new Rectangle(x, y, x + w, y + h); }
- 11-21-2012, 09:03 PM #5
Member
- Join Date
- Nov 2012
- Posts
- 27
- Rep Power
- 0
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".
- 11-22-2012, 04:57 AM #6
Member
- Join Date
- Nov 2012
- Posts
- 27
- Rep Power
- 0
Similar Threads
-
Rotated rectangle collision detection
By Daslee in forum Java 2DReplies: 5Last Post: 10-25-2012, 01:30 PM -
Collision detection
By mwr1976 in forum Java 2DReplies: 2Last Post: 04-14-2012, 12:45 PM -
Collision Detection
By sunde887 in forum Java 2DReplies: 2Last Post: 10-07-2011, 11:40 PM -
Collision Detection?
By Alerhau in forum New To JavaReplies: 39Last Post: 09-07-2011, 04:55 PM -
Collision Detection
By dotabyss in forum Java GamingReplies: 0Last Post: 03-14-2010, 06:13 PM


LinkBack URL
About LinkBacks
Reply With Quote


Bookmarks