Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Collision detection

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Collision detection

    Hello, I'm just casually working on this game. For some reason I can not get the collision to work 100% of the time. Ill post the main class. Just ask if you want to see the others.

    I apologize for the lack of comments. I was really only working on this for myself.

     
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Toolkit;
    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.JPanel;
    import javax.swing.Timer;
     
    public class Board extends JPanel implements ActionListener{
     
    	 private Timer timer;
    	 private Spacecraft spaceCraft;
    	 private Meteor meteor;
    	 private Spaceman astronaut;
     
     
    	 private boolean ingame;
    	 private int B_WIDTH;
    	 private int B_HEIGHT;
    	 private int count = 0;
    	 private int counta = 0;
    	 private int lives = 5;
    	 private int score = 0;
     
     
    	public Board(){
    		addKeyListener(new TAdapter());
            setFocusable(true);
            setBackground(Color.BLACK);
            setDoubleBuffered(true);
     
            B_WIDTH = 800;
            B_HEIGHT = 500;
     
            count = 0;  
            counta = 0;
            lives = 5;
            score = 0;
     
     
            spaceCraft = new Spacecraft();
            meteor = new Meteor();
            astronaut = new Spaceman();
     
            timer = new Timer(5,this);
            timer.start();
    	}
     
    	public void paint(Graphics g) {
            super.paint(g);
     
            Graphics2D g2d = (Graphics2D)g;
     
            g2d.drawImage(spaceCraft.getImage(), spaceCraft.getX(), spaceCraft.getY(), this);
            g2d.drawString(spaceCraft.getX() + "," + spaceCraft.getY(), spaceCraft.getX() + 10, spaceCraft.getY() - 10);
            g2d.drawOval(spaceCraft.getX(),  spaceCraft.getY(), 20, 20);
            Font k = new Font("Serif", Font.BOLD, 18);
            g2d.setFont(k);
            g2d.drawString("Lives: "+lives,(( B_WIDTH/2 ) + 50), 25);
            g2d.drawString("Score: "+score,(( B_WIDTH/2 ) - 50), 25);
     
            ArrayList ms = meteor.getMeteorArray();
            ArrayList as = astronaut.getAstronautArray();
     
            for (int i = 0; i < as.size(); i++ ) {
            	Spaceman a = (Spaceman) as.get(i);
            	g2d.drawImage(a.getImage(), a.getX(), a.getY(),this);
            }
     
            for (int i = 0; i < ms.size(); i++ ) {
                Meteor m = (Meteor) ms.get(i);
                g2d.drawImage(m.getImage(), m.getX(), m.getY(), this);
                g2d.drawOval(m.getX(),m.getY(),64,64);
            }
     
            Toolkit.getDefaultToolkit().sync();
            g.dispose();
        }
    	public void actionPerformed(ActionEvent e) {
     
    		 ArrayList ms = meteor.getMeteorArray();
    		 ArrayList as = astronaut.getAstronautArray();
     
    		 for(int f = 0; f < as.size(); f++){
    			 Spaceman a = (Spaceman) as.get(f);
    			 if (a.isVisible()){
    				 a.move();
    			 }
    			 else as.remove(f);
    		 }
    		 counta++;
    		 if(counta%250 == 0){
    			 astronaut.addAstronaut(); 
    		 }
    		 if(counta >= 1000){
    			 counta=0;
    		 }	
     
    		 for (int f = 0; f < ms.size(); f++) {
    		     Meteor m = (Meteor) ms.get(f);
    		     if (m.isVisible()){ 
    		         m.move();
     
     
    		     }
    		     else ms.remove(f);
    		 }
             collision();
     
     
    		 count++;
    		 if(count%40 == 0){
    			 meteor.addMeteor();	
    		 }
    		 if(count >= 1000){
    			 count=0;
    		 }
     
    		 spaceCraft.move();
    		 repaint();  
    	 }
    	public void collision(){
     
    		//This doesnt work 100%
    		//Seems to only work on the top left for some reason
    		ArrayList ms = meteor.getMeteorArray();
    		for(int z = 0; z < ms.size(); z++){
    			Meteor m = (Meteor) ms.get(z); 
    			if( 10 >= (Math.sqrt((spaceCraft.getX() - m.getX()) * (spaceCraft.getX() - m.getX()) + (spaceCraft.getY() - m.getY()) * (spaceCraft.getY() - m.getY())  ))){
    				lives--;
    				delay(1000);
    				reset();
    			}
    		}
    	}
    	public void delay (int ms)
    	{
    		try
    		{
    			// Stop thread for specified milliseconds in brackets
    			Thread.sleep (ms);
    		}
    		catch (InterruptedException ex)
    		{
    			// Do nothing
    		}
    	}
    	private void reset(){
    		spaceCraft.reset();
     
    	}
    	private class TAdapter extends KeyAdapter {
     
    	        public void keyReleased(KeyEvent e) {
    	        	spaceCraft.keyReleased(e);
    	        }
     
    	        public void keyPressed(KeyEvent e) {
    	        	spaceCraft.keyPressed(e);
    	        }
    	 }
    }


    --- Update ---

    Both objects are circles and this should judge the distance between the two circles.
    i assume my equation is wrong


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Collision detection

    That's too much code to wade through. Point to where in this code mass the collisions are (and sometimes aren't) being detected.

  3. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Collision detection

    I can not get the collision to work 100% of the time.
    ...does not really explain what the problem or symptoms are. Are things passing through others, are they bouncing too early or too late? Are the angles wrong? The list could go on.. Try to explain better what the problem is and/or post a SSCCE

Similar Threads

  1. Collision Detection Between Two Squares
    By thegreatzo in forum Loops & Control Statements
    Replies: 7
    Last Post: August 22nd, 2012, 09:13 AM
  2. AI, Collision Detection, and Timing
    By Staticity in forum Java Theory & Questions
    Replies: 0
    Last Post: March 20th, 2012, 02:12 PM
  3. collision detection not working...
    By skberger21 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 10th, 2011, 09:02 PM
  4. Collision Detection difficulties
    By Uritomi in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 20th, 2011, 10:10 AM
  5. 2D Collision Detection
    By Cuju in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 3rd, 2010, 10:39 AM