Results 1 to 4 of 4
  1. #1
    Chetans is offline Member
    Join Date
    Feb 2010
    Posts
    15
    Rep Power
    0

    Default Problem in collision between objects

    Hi
    I am creating Game
    in which i am getting problem in collision detection
    and threading
    Game has a spaceship and number of obstacles
    i want to detect the collision between them
    How can i do this?

    Following is my code :
    Java Code:
    package Game;
    import java.applet.Applet;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.MediaTracker;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.net.MalformedURLException;
    import java.net.URL;
    
    public class ThreadInApplet extends Applet implements KeyListener
    {
        private static final long serialVersionUID = 1L;
        
        Image[] asteroidImage;
        Image spaceshipImage;
         
        int[] XPosObst,YPosObst;
        int numberOfObstacles=0,XPosOfSpaceship,YPosOfSpaceship;
        int spaceButtnCntr=0,noOfObstaclesLevel=20;
       
        boolean gameStart=false,collideUp=false,collideDown=false,collideLeft=false,collideRight=false;
        
        private Image offScreenImage;
        private Dimension offScreenSize,d;
        private Graphics offScreenGraphics;
    
        int speedObstacles=1;
        String spaceshipImagePath="images/spaceship.png",obstacleImagepath="images/asteroid.png";
        String buttonToStart="Press Space to start";
        
        public void init()
        { 
                try 
                {
                     asteroidImage=new Image[noOfObstaclesLevel];
                     XPosObst=new int[noOfObstaclesLevel];
                     YPosObst=new int[noOfObstaclesLevel];
                     XPosOfSpaceship=getWidth()/2-35;
                     YPosOfSpaceship=getHeight()-100;
                     spaceshipImage=getImage(new URL(getCodeBase(),spaceshipImagePath));
                     for(int i=0;i<noOfObstaclesLevel;i++)
                     {
                         asteroidImage[i]=getImage(new URL(getCodeBase(),obstacleImagepath));  
                         XPosObst[i]=(int) (Math.random()*700);
                         YPosObst[i]=0;
                     }
                     MediaTracker tracker = new MediaTracker (this);
                     for(int i=0;i<noOfObstaclesLevel;i++)
                     {
                         tracker.addImage (asteroidImage[i], 0);
                     }
                
                } 
                catch (MalformedURLException e) 
                {
                    e.printStackTrace();
                }
                setBackground(Color.black);
                addKeyListener(this);
                
                
        }
        
        
        public void paint(Graphics g)
        { 
            g.setColor(Color.white);
            if(gameStart==false)
                {
                    g.drawString(buttonToStart, (getWidth()/2)-60, getHeight()/2);
                }
            g.drawString("HEADfitted Solutions Pvt.Ltd.", (getWidth()/2)-80, getHeight()-20);
            
            for(int n=0;n<numberOfObstacles;n++)
            {
                if(n>0)
                	g.drawImage(asteroidImage[n],XPosObst[n],YPosObst[n],this);
            }
            g.drawImage(spaceshipImage,XPosOfSpaceship,YPosOfSpaceship,this);
    
        }
        
    	@SuppressWarnings("deprecation")
    	public void update(Graphics g)
        {
            d = size();
            if((offScreenImage == null) || (d.width != offScreenSize.width) || (d.height != offScreenSize.height))
            {
    	        offScreenImage = createImage(d.width, d.height);
    	        offScreenSize = d;
    	        offScreenGraphics = offScreenImage.getGraphics();
            }
    
    	    offScreenGraphics.clearRect(0, 0, d.width, d.height);
    	    paint(offScreenGraphics);
    	    g.drawImage(offScreenImage, 0, 0, null);
        }
    
        
        public void keyReleased(KeyEvent arg0){}
        public void keyTyped(KeyEvent arg0) {}
        
        Thread mainThread=new Thread()
        {
            
    		synchronized public void run () 
            {
                  try     
                        {
                	  		
                	  		
                          //System.out.println("in main thread");
                          if (gameStart==true)
                          {
                        	  moveObstacles.start();
                        	  if(collide()==false)
    	          	  			{
    	          	  				createObsThread.start();
    	          	  			}
    	                   } 
                          
                        }
                        catch (Exception e) 
                        {
                            e.printStackTrace();
                        }
    		                       
            }
        };
        
        Thread createObsThread=new Thread()
        {
            synchronized public void run () 
            {
            	 if (spaceButtnCntr==1)
                 {
            		 if (collide()==false)
            		 {
            		    for(int g=0;g<noOfObstaclesLevel;g++)
                       {
    		                        try     
    		                        {
    		                            sleep(1000);
    		                        } 
    		                        catch (InterruptedException e) 
    		                        {
    		                            e.printStackTrace();
    		                        }
    		                       numberOfObstacles++;
                       	 }
                       
                     }
            		
            		 }
            }
        };
        
       Thread moveObstacles=new Thread() // Moving Obstacle images downwards after every 10 ms
               {
    				synchronized public void run () 
                   {
                       while(YPosObst[19]!=600)
                       {
                    	   if (collide()==false)
                    	   {
                       		 	//createObsThread.start();
    		                        for(int l=0;l<numberOfObstacles;l++)
    		                        {
    		                            YPosObst[l]+=speedObstacles;
    		                           
    		                        }
    		                        repaint();
    		                        try 
    		                        {
    		                            sleep(10);
    		                        } 
    		                        catch (InterruptedException e)
    		                        {
    		                            e.printStackTrace();
    		                        }
                       	 } 
    					
                    	   
                       }
                   }
                   
               };
        
          
    	public void keyPressed(KeyEvent e)
        {
    		
            if(e.getKeyCode()==32)
            {
                gameStart=true;
                spaceButtnCntr++;
              
                if (spaceButtnCntr==1)
                {
                	 mainThread.start();
                	 
    	        }
            }
            
            if(gameStart==true) 
            {
            	
    
                if(e.getKeyCode()==37 && collideLeft==false)//Spaceship movement left
                {
                    new Thread () 
                    {
                        synchronized public void run () 
                        {
                           XPosOfSpaceship-=10;
                            repaint();
                        }
                    }.start();
                }
                if(e.getKeyCode()==38 && collideUp==false)//Spaceship movement up
                {
                    new Thread () 
                    {
                        synchronized public void run () 
                        {
                            YPosOfSpaceship-=10;
                            repaint();
                        }
                    }.start();
                }
                if(e.getKeyCode()==39 && collideRight==false)//Spaceship movement right
                {
                    new Thread () 
                    {
                        synchronized public void run () 
                        {
                           XPosOfSpaceship+=10;
                            repaint();
                        }
                    }.start();
                }
                if(e.getKeyCode()==40 && collideDown==false)//Spaceship movement down
                {
                    new Thread () 
                    {
                        synchronized public void run () 
                        {
                            YPosOfSpaceship+=10;
                            repaint();
                        }
                    }.start();
                }
            }  
        }
    	/*public boolean collide() 
        { 
    		int x0, y0, w0, h0, x2, y2, w2, h2;
    		x0=XPosOfSpaceship;
    		y0=YPosOfSpaceship;
    		h0=spaceshipImage.getHeight(null);
    		w0=spaceshipImage.getWidth(null);
    		 
    		 for(int i=0;i<20;i++)
             {
                 x2=XPosObst[i];
                 y2=YPosObst[i];
                 h2=asteroidImage[i].getHeight(null);
                 w2=asteroidImage[i].getWidth(null);
                 
                 if ((x0 > (x2 + w2)) || ((x0 + w0) < x2)) return false;
                 System.out.println(x2+" "+y2+" "+h2+" "+w2);
             	 if ((y0 > (y2 + h2)) || ((y0 + h0) < y2)) return false;        
                     
             }
    		 return true;
    	
        }*/
    	public boolean collide()
        {
            int x1,y1,x2,y2,x3,y3,x4,y4;    //coordinates of obstacles 
            int a1,b1,a2,b2,a3,b3,a4,b4;    //coordinates of spaceship
            a1 =XPosOfSpaceship;
            b1=YPosOfSpaceship;
            a2=a1+spaceshipImage.getWidth(this);
            b2=b1;
            a3=a1;
            b3=b1+spaceshipImage.getHeight(this);
            a4=a2;
            b4=b3;
            for(int a=0;a<numberOfObstacles;a++)
            {
                x1 =XPosObst[a];
                y1=YPosObst[a];
                x2=x1+asteroidImage[a].getWidth(this);
                y2=y1;
                x3=x1;
                y3=y1+asteroidImage[a].getHeight(this);
                x4=x2;
                y4=y3;
                
                if(x1>=a1 && x1<=a2 && x1<=b3 && x1>=b1)
                	return (true);
                if(x2>=a1 && x2<=a2 && x2<=b3 && x2>=b1)
                	return(true);
                
                //********checking asteroid touch spaceship from up direction******** 
                if(y3==b1 && x4>=a1 && x4<=a2)
                {
                    collideUp = true;
                    return(true);
                    
                    
                }
                if(y3==b1 && x3>=a1 && x3<=a2)
                {
                    collideUp = true;
                    return(true);
                    
                }
                
                //********checking asteroid touch spaceship from left direction******
                if(x2==a1 && y4>=b1 && y4<=b3)
                {
                    collideLeft=true;
                    return(true);
                    
                }
                if(x2==a1 && y2>=b1 && y2<=b3)
                {
                    collideLeft=true;
                    return(true);
                }
                
                //********checking asteroid touch spaceship from right direction*****
                if(x1==a2 && y3>=b2 && y3<=b4)
                {
                    collideRight=true;
                    return(true);
                }
                if(x1==a2 && y1>=b2 && y1<=b4)
                {
                    collideRight=true;
                    return(true);
                }
                
                //********checking asteroid touch spaceship from down direction*****
                if(y1==b3 && x2>=a3 && x2<=a4)
                {
                    collideDown=true;
                    return(true);
                }
                if(y1==b3 && x1>=a3 && x1<=a4)
                {    
                    collideDown=true;
                    return(true);
                }
                else
                {
                    collideUp=false;
                    collideDown=false;
                    collideLeft=false;
                    collideRight=false;
                }
                
                
    
            }
             return(false);
        }
    }

  2. #2
    programmer_007's Avatar
    programmer_007 is offline Senior Member
    Join Date
    Aug 2009
    Posts
    125
    Rep Power
    0

    Default

    try and try until u succedd!

  3. #3
    Chetans is offline Member
    Join Date
    Feb 2010
    Posts
    15
    Rep Power
    0

    Default

    Quote Originally Posted by programmer_007 View Post
    try and try until u succedd!
    Plz help me to solve this problem
    i hve tried so much

  4. #4
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Hi Chetans

    i can't help you because i have just begun with programming java games. but if you enter java collision detect in google you will get a lot of hits. believe me, you are not the first programmer having this problem ...

Similar Threads

  1. Collision Detection
    By dotabyss in forum Java Gaming
    Replies: 0
    Last Post: 03-14-2010, 07:13 PM
  2. HashMap: Obtaining all values in a collision?
    By markus-sukram in forum New To Java
    Replies: 2
    Last Post: 03-29-2008, 11:25 PM
  3. Collision Detection (Game)
    By mscwd in forum Sun Java Wireless Toolkit
    Replies: 0
    Last Post: 01-28-2008, 09:34 PM
  4. Two Problems Rotating and collision detection help
    By jaferris in forum Java Applets
    Replies: 2
    Last Post: 01-08-2008, 12:19 AM
  5. Listener collision on game
    By cachi in forum Java Applets
    Replies: 1
    Last Post: 08-07-2007, 08:48 AM

Posting Permissions

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