Getting problem in collision detection in Java Game
- by chetans
Hi
I am developing Spaceship Game
in which i am getting problem in collision detection of moving images
Game has a spaceship and number of asteroids(obstacles)
i want to detect the collision between them
How can i do this?`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
}
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
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);
}
}
`