Results 1 to 3 of 3
Thread: Collision detection
- 04-14-2012, 11:30 AM #1
Senior Member
- Join Date
- Oct 2011
- Posts
- 106
- Rep Power
- 0
Collision detection
I have a space invaders like game.This is very weird. For about the first 500 cycles of my swing Timer my collision detection doesn't work at all. The more cycles it goes, the better it gets. There are tons of bullets flying through my enemies. What is weird is the last row (the first ones drawn) are the first to register collisions. Almost at the very end the collisions register like they should. I have quite a few classes so I am going to try yo narrow down the code. If there is something else you need to see let me know:
here is my collision method by its self:
swarm is an Arraylist that is an instance variable of this class.
p is the instance of my Player class
here's the whole class:Java Code:public void checkCollision() { ArrayList bullets = p.getBullets(); for(int i = 0; i < bullets.size(); i++) { Bullet m = (Bullet) bullets.get(i); Enemy e = (Enemy)swarm.get(i); if(m.getBounds().intersects(e.getBounds())) { swarm.remove(i); m.setVisible(false); e.setAlive(false); } } }//end of checkCollision
Java Code:package game; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Image; 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.ArrayList; import javax.swing.ImageIcon; import javax.swing.JPanel; import javax.swing.Timer; public class Board extends JPanel implements ActionListener { //instance variables private static final long serialVersionUID = 1L; //player declaration private Player p; //Image object to hold the background image private Image background; //swing timer for action performed (repainting) private Timer time; private long delta; private long delta2; private long lastLeft = 0; private long lastMove = 0; private int counter = 0; private ArrayList swarm = new ArrayList(); //constructor public Board() { //player instance p = new Player(); createSwarm(); //adds listen for key pressed actions this.addKeyListener(new AL()); //sets JPanel focusable so it can register key inputs this.setFocusable(true); //path to load the background image into image object ImageIcon ic = new ImageIcon("src/images/space_background.png"); //actually load background image into image object background = ic.getImage(); //creates new swing timer (5 millis) time = new Timer(5, this); //starts timer time.start(); }//end of constructor public void removeEnemyOut() { for(int m = 0; m < swarm.size(); m++) { Enemy en = (Enemy)swarm.get(m); if(en.getY() > 1000) { swarm.remove(m); } } }//end of removeEnemyOut(); public void moveEnemy(int dx) { delta =System.currentTimeMillis(); if(delta - lastMove > 35) { for(int m = 0; m < swarm.size(); m++) { Enemy en = (Enemy)swarm.get(m); en.setY(en.getY() + dx); } lastMove = delta; } }//end of EnemyMove() public void moveEnemySide(int dx) { delta2 =System.currentTimeMillis(); if(delta2 - lastLeft > 35) { for(int m = 0; m < swarm.size(); m++) { Enemy en = (Enemy)swarm.get(m); en.setX(en.getX() + dx); } lastLeft = delta2; } }//end of MoveEnemySide() public void createSwarm() { for(int i = 0; i < 36; i++) { Enemy m = new Enemy(0, 0, "src/images/alienBlue.png"); m.setX(i * 95 + 50); if(m.getX() > 1100) { m.setX(m.getX() - 1190); m.setY(getY() + 100); if(m.getX() > 1100) { m.setX(m.getX() - 1100); m.setY(getY() + 200); } } swarm.add(m); } }//end of createSwarm() public void checkCollision() { ArrayList bullets = p.getBullets(); for(int i = 0; i < bullets.size(); i++) { Bullet m = (Bullet) bullets.get(i); Enemy e = (Enemy)swarm.get(i); if(m.getBounds().intersects(e.getBounds())) { swarm.remove(i); m.setVisible(false); e.setAlive(false); } } }//end of checkCollision @Override public void actionPerformed(ActionEvent arg0) { //checks for collisions checkCollision(); //counter wrapper used for changing up movement counter++; removeEnemyOut(); moveEnemy(1); if(counter < 400) { moveEnemySide(1); } else if(counter > 400 && counter < 1200) { moveEnemySide(-1); } else if(counter > 1200 && counter < 2000) { moveEnemySide(1); } else if(counter > 2000 && counter < 2800) { moveEnemySide(-1); } else if(counter > 2800 && counter < 3600) { moveEnemySide(1); } else if(counter > 3600 && counter < 4400) { moveEnemySide(-1); } else if(counter > 4400 && counter < 5200) { moveEnemySide(1); } else if(counter > 5200 && counter < 6000) { moveEnemySide(-1); } else if(counter > 6000 && counter < 6800) { moveEnemySide(1); } //moves the bullets ArrayList bullets = p.getBullets(); for(int i = 0; i < bullets.size(); i++) { Bullet m = (Bullet) bullets.get(i); if(m.getVisible()) m.move(); else bullets.remove(i); } p.move(); repaint(); }//end of actionPerformed() //overrides paint method public void paint(Graphics g) { super.paint(g); Graphics2D graphics = (Graphics2D)g; graphics.drawImage(background, 0, 0, this); graphics.drawImage(p.getImage(), p.getX(), p.getY(), this); ArrayList bullets = p.getBullets(); for(int i = 0; i < bullets.size(); i++) { Bullet m = (Bullet) bullets.get(i); graphics.drawImage(m.getImage(), m.getX(), m.getY(), this); } for(int k = 0; k < swarm.size(); k++) { Enemy e = (Enemy)swarm.get(k); if(e.isAlive) { graphics.drawImage(e.getImage(), e.getX(), e.getY(), this); } } }//end of paint() //inner class for key adapter private class AL extends KeyAdapter { public void keyReleased(KeyEvent e) { p.keyReleased(e); } public void keyPressed(KeyEvent e) { p.keyPressed(e); } }//end of AL class }//end of Board class
- 04-14-2012, 11:54 AM #2
Re: Collision detection
Shouldn't you be running a nested loop and checking each bullet against every enemy? As your code stands, it's only checking the bullet at an index in its list against the enemy at the same index in the swarm.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 04-14-2012, 12:45 PM #3
Senior Member
- Join Date
- Oct 2011
- Posts
- 106
- Rep Power
- 0
Similar Threads
-
Collision Detection
By Äppelpaj in forum Java 2DReplies: 1Last Post: 10-13-2011, 03:29 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 -
Really Need help with some collision detection
By Harwad in forum New To JavaReplies: 1Last Post: 01-23-2011, 12:38 AM -
Collision Detection
By dotabyss in forum Java GamingReplies: 0Last Post: 03-14-2010, 06:13 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks