Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Computer Forums > Programmers Lounge > Programming Discussions » Collision Detection in Applet Games
Closed Thread
Old 07-12-2004, 10:33 PM   #1 (permalink)
 
Newb Techie

Join Date: Jul 2004

Posts: 23

Punch-M6.net

Default Collision Detection in Applet Games

Hi all,

I'm learning how to program Java Applet games at the moment, and I'm currently making a simple Space-Invaders type game. I'm having trouble with finding help for how to program collision detection, though, as most tutorials just instruct you to use a pre-made library (which I don't really want to do).

The routine I have at the moment is as follows:

//Position of ship
int x_pos;
int y_pos;
//Stats of ship
private int height = 40;
private int width = 40;

public boolean collision(int posX, int posY){
if (posX >= x_pos && posX <= x_pos + width && posY >= y_pos && posY <= y_pos + height){
return true;
}
else{
return false;
}
}

This works by taking an X,Y point (posX, posY) and testing whether or not it falls into the ships 'collision range', however it only works for a single pixel and seems like it will get clumsy and hard to use if I wanted to test, for example, one ship colliding with another.

Any ideas?

Thanks.
Punch-M6.net is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On