Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a Java Expert!

Join 244,307 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 792 people online right now. Registration is fast and FREE... Join Now!




Collision detection with objects

 
Reply to this topicStart new topic

Collision detection with objects, Need help detecting collision

gawdlike
4 Dec, 2008 - 11:20 AM
Post #1

New D.I.C Head
*

Joined: 16 Nov, 2008
Posts: 41


My Contributions
Ok, so here is the problem:

I need to be able to detect if the ball hits the bricks or player "board".
Right now i got it all moving, but no collisions are detected. Any suggestions?

java

//-------------
//import
//-------------
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//-------------------------------------------------------------------------------
public class Bricks extends Applet implements Runnable, KeyListener{

//-------------------------------------------------------------------------------
// deklarationer
//-------------------------------------------------------------------------------

Rectangle[][] target = new Rectangle[5][8];
int targetBredd = 60;
int targetHojd = 20;
int width = 500;
int height = 390;
int spelareHojd = 20;
int spelareBredd = 80;
int radie = 7;
double spelareX = width/2-40;
double spelareY = 370;
double hastBollX = 2;
double hastBollY = 2;
double bollY = 300;
double bollX = 140;
double spelareHast = 3;
boolean leftDown=false;
boolean rightDown=false;
Image minnesbild;
Graphics rityta;
Rectangle spelare = new Rectangle();


//-----------------------------------------------------------------
//init
//-----------------------------------------------------------------

public void init(){

addKeyListener(this);
requestFocus();
minnesbild = createImage(width,height);
rityta = minnesbild.getGraphics();

}//end init

//-------------------------------------------------------------------------------
// tråden start
//-------------------------------------------------------------------------------

public void start(){

Thread ny = new Thread(this);
ny.start();

} //end start

//-------------------------------------------------------------------------------
// skapa spelplanen forsta gangen
//-------------------------------------------------------------------------------

public void paint(Graphics g) {

for(int i=0; i<5; i=i+1){
for(int j=0; j<8; j=j+1){
g.setColor(Color.blue);
target[i][j] = new Rectangle();
target[i][j].setLocation( 100 + i*(5 + targetBredd), j * (5 +targetHojd));
target[i][j].setSize( targetBredd, targetHojd );
g.fillRect(100 + i*(targetBredd+5) , j * (targetHojd+5) , targetBredd, targetHojd);
} //end for j
} //end for i

g.setColor(Color.black);
spelare.setLocation((int)(spelareX),(int)(spelareY));
spelare.setSize(spelareBredd,spelareHojd);
g.fillRect((int)(spelareX),(int)(spelareY), spelareBredd, spelareHojd);

g.setColor(Color.black);
g.fillOval((int)(bollX-radie),(int)(bollY-radie),
(int)(2*radie),(int) (2*radie));
} //end paint

//-------------------------------------------------------------------------------
// run metoden
//-------------------------------------------------------------------------------

public void run(){

while ( true){

bollX = bollX + hastBollX;
bollY = bollY + hastBollY;
flytta();
innanforRam();
repaint();

try { Thread.sleep(10);} catch(InterruptedException ie){}
} //end while
} //end run

//-------------------------------------------------------------------------------
// keyevent
//-------------------------------------------------------------------------------

public void keyPressed(KeyEvent ke){
if(ke.getKeyCode() == KeyEvent.VK_LEFT) leftDown = true;
if(ke.getKeyCode() == KeyEvent.VK_RIGHT) rightDown = true;

} //end keypressed
public void keyReleased(KeyEvent ke){
if(ke.getKeyCode() == KeyEvent.VK_LEFT) leftDown = false;
if(ke.getKeyCode() == KeyEvent.VK_RIGHT) rightDown = false;


} //end key released
public void keyTyped(KeyEvent ke){} //end typed

//-----------------------------------------------------------------
//flimmerfritt
//-----------------------------------------------------------------

public void update(Graphics g){

rityta.clearRect(0,0,width,height);
paint(rityta);
g.drawImage(minnesbild, 0, 0, this);

} //end update

//-----------------------------------------------------------------
//hålla innanför ramen
//-----------------------------------------------------------------

public void innanforRam(){
//hall bollen inne
if (bollX+radie > width)
hastBollX = -hastBollX;
if (bollX-radie < 0)
hastBollX = -hastBollX;
if (bollY+radie > height)
hastBollY = -hastBollY;
if (bollY-radie < 0)
hastBollY = -hastBollY;
//hall spelaren inne
if (spelareX+spelareBredd > width)
spelareHast = 0;
if (spelareX < 0)
spelareHast = 0;
if (spelareX + spelareBredd > width && leftDown)
spelareHast = 3;
if (spelareX < 0 && rightDown)
spelareHast = 3;

} //end ram

//-----------------------------------------------------------------
//flytta bilen
//-----------------------------------------------------------------

public void flytta(){

if(leftDown){
spelareX = spelareX - spelareHast;
} //end if
if(rightDown){
spelareX = spelareX + spelareHast;
} //end if
} //end flytta


}//end class
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------


User is offlineProfile CardPM
+Quote Post


Jayman
RE: Collision Detection With Objects
4 Dec, 2008 - 11:48 AM
Post #2

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 8,063



Thanked: 159 times
Dream Kudos: 500
Expert In: Everything

My Contributions
Topic renamed to be more descriptive of the problem.
User is offlineProfile CardPM
+Quote Post

gawdlike
RE: Collision Detection With Objects
4 Dec, 2008 - 02:46 PM
Post #3

New D.I.C Head
*

Joined: 16 Nov, 2008
Posts: 41


My Contributions
I solved the above problem, but now i need help removing the rectangles once they are hit.

Is there any simple way of doing this?
User is offlineProfile CardPM
+Quote Post

5thWall
RE: Collision Detection With Objects
4 Dec, 2008 - 03:52 PM
Post #4

D.I.C Head
**

Joined: 17 Sep, 2008
Posts: 209



Thanked: 6 times
My Contributions
You could extend the rectangle class to hold an isHit boolean and set that to true if the rectangle is hit. Then on each update cycle loop through all your rectangles and remove the ones that have been hit.

Edit: Or if you don't want to extend the class, you could hold a list of the index of rectangles that have been hit and remove them using that list.

This post has been edited by 5thWall: 4 Dec, 2008 - 03:54 PM
User is offlineProfile CardPM
+Quote Post

gawdlike
RE: Collision Detection With Objects
5 Dec, 2008 - 03:05 AM
Post #5

New D.I.C Head
*

Joined: 16 Nov, 2008
Posts: 41


My Contributions
I have set a flag that keeps track of the rectangle being hit. But what i need is a way to remove the rectangle from the screen, since now if i hit a rectangle, the only difference is that if i hit it again i wont get any points. It doesnt dissapear.

my code:

java

public void kollaTraff(){
//kolla traff vanster
for(int i=0; i<5; i=i+1){
for(int j=0; j<8; j=j+1){
if (bollX + radie + 3 == 100 + i*(5 + targetBredd) && bollY + radie > j * (5 +targetHojd) && bollY + radie < j * (5 +targetHojd) + targetHojd && flagga[i][j] == false){
flagga[i][j] = true;
raknare = raknare + 20;
hastBollY = -hastBollY;
hastBollX = -hastBollX;
} //end if
} //end for j
} //end for i
//kolla traff hoger
for(int i=0; i<5; i=i+1){
for(int j=0; j<8; j=j+1){
if (bollX - radie - 3 == 100 + i*(5 + targetBredd) + targetBredd && bollY + radie > j * (5 +targetHojd) && bollY + radie < j * (5 +targetHojd) + targetHojd && flagga[i][j] == false){
flagga[i][j] = true;
raknare = raknare + 20;
hastBollY = -hastBollY;
hastBollX = -hastBollX;
} //end if
} //end for j
} //end for i
//kolla traff uppe
for(int i=0; i<5; i=i+1){
for(int j=0; j<8; j=j+1){
if (bollY - radie - 3 == j * (5 +targetHojd) && bollX + radie > 100 + i*(5 + targetBredd) && bollX < 100 + i*(5 + targetBredd) + targetBredd && flagga[i][j] == false){
flagga[i][j] = true;
raknare = raknare + 20;
hastBollY = -hastBollY;
hastBollX = -hastBollX;
} //end if
} //end for j
} //end for i
//kolla traff nere
for(int i=0; i<5; i=i+1){
for(int j=0; j<8; j=j+1){
if (bollY + radie + 3 == j * (5 +targetHojd) + targetHojd && bollX + radie > 100 + i*(5 + targetBredd) && bollX < 100 + i*(5 + targetBredd) + targetBredd && flagga[i][j] == false){
flagga[i][j] = true;
raknare = raknare + 20;
hastBollY = -hastBollY;
hastBollX = -hastBollX;
} //end if
} //end for j
} //end for i
} //end kollatraff


This post has been edited by gawdlike: 5 Dec, 2008 - 05:23 AM
User is offlineProfile CardPM
+Quote Post

5thWall
RE: Collision Detection With Objects
5 Dec, 2008 - 03:52 PM
Post #6

D.I.C Head
**

Joined: 17 Sep, 2008
Posts: 209



Thanked: 6 times
My Contributions
What you need to do is look at that flag whenever you see if the ball has hit a target. If it has been hit then just move on to the next target without doing anything to the ball.

You need to do the same thing when you're drawing the targets. Check the flag before you draw a rectangle and if it is set as hit then just move on to the next one without drawing the hit one.



User is offlineProfile CardPM
+Quote Post

gawdlike
RE: Collision Detection With Objects
6 Dec, 2008 - 03:21 AM
Post #7

New D.I.C Head
*

Joined: 16 Nov, 2008
Posts: 41


My Contributions
ok, now my collision detection code still isnt 100% sad.gif
java

public void kollaTraff(){
//kolla traff vanster
for(int i=0; i<5; i=i+1){
for(int j=0; j<8; j=j+1){
if (bollX + radie + 3 == 100 + i*(5 + targetBredd) && bollY + radie > j * (5 +targetHojd) && bollY + radie < j * (5 +targetHojd) + targetHojd && flagga[i][j] == false){
flagga[i][j] = true;
raknare = raknare + 20;
} //end if
} //end for j
} //end for i
//kolla traff hoger
for(int i=0; i<5; i=i+1){
for(int j=0; j<8; j=j+1){
if (bollX - radie - 3 == 100 + i*(5 + targetBredd) + targetBredd && bollY + radie > j * (5 +targetHojd) && bollY + radie < j * (5 +targetHojd) + targetHojd && flagga[i][j] == false){
flagga[i][j] = true;
raknare = raknare + 20;
} //end if
} //end for j
} //end for i
//kolla traff uppe
for(int i=0; i<5; i=i+1){
for(int j=0; j<8; j=j+1){
if (bollY - radie - 3 == j * (5 +targetHojd) && bollX + radie > 100 + i*(5 + targetBredd) && bollX < 100 + i*(5 + targetBredd) + targetBredd && flagga[i][j] == false){
flagga[i][j] = true;
raknare = raknare + 20;
} //end if
} //end for j
} //end for i
//kolla traff nere
for(int i=0; i<5; i=i+1){
for(int j=0; j<8; j=j+1){
if (bollY + radie + 3 == j * (5 +targetHojd) + targetHojd && bollX + radie > 100 + i*(5 + targetBredd) && bollX < 100 + i*(5 + targetBredd) + targetBredd && flagga[i][j] == false){
flagga[i][j] = true;
raknare = raknare + 20;
} //end if
} //end for j
} //end for i
} //end kollatraff


My problem is that sometimes it just jumps over a target when it goes through another one.

This post has been edited by gawdlike: 6 Dec, 2008 - 03:22 AM
User is offlineProfile CardPM
+Quote Post

5thWall
RE: Collision Detection With Objects
6 Dec, 2008 - 12:41 PM
Post #8

D.I.C Head
**

Joined: 17 Sep, 2008
Posts: 209



Thanked: 6 times
My Contributions
Well, a ball jumping past targets seems like it would be a problem in your update method, because you don't change the position of the ball at all in your checkHit method. What exactly do you mean by jumps over? Do you have anything to make the ball bounce when it hits a target?

User is offlineProfile CardPM
+Quote Post

gawdlike
RE: Collision Detection With Objects
6 Dec, 2008 - 02:01 PM
Post #9

New D.I.C Head
*

Joined: 16 Nov, 2008
Posts: 41


My Contributions
well, jumping over might be wrong of me to say, it goes through without it "deleting" tongue.gif

atm it just seems to be the logic in the ifs thats wrong, because it works just fine sometimes, but at certain situations, it just goes through. Especially when hitting from top or bottom. Complete code:

java

//-------------
//import
//-------------
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//-------------------------------------------------------------------------------
public class Bricks extends Applet implements Runnable, KeyListener{

//-------------------------------------------------------------------------------
// deklarationer
//-------------------------------------------------------------------------------

Rectangle[][] target = new Rectangle[5][8];
boolean[][] flagga = new boolean[5][8];
int targetBredd = 60;
int targetHojd = 20;
int width = 500;
int height = 390;
int spelareHojd = 20;
int spelareBredd = 80;
int radie = 7;
int raknare = 0;
double spelareX = width/2-40;
double spelareY = 370;
double hastBollX = 2;
double hastBollY = 2;
double bollY = 300;
double bollX = 150;
double spelareHast = 3;
boolean leftDown=false;
boolean rightDown=false;
Image minnesbild;
Graphics rityta;
Rectangle spelare = new Rectangle();
Graphics g;


//-----------------------------------------------------------------
//init
//-----------------------------------------------------------------

public void init(){

addKeyListener(this);
requestFocus();
minnesbild = createImage(width,height);
rityta = minnesbild.getGraphics();

}//end init

//-------------------------------------------------------------------------------
// tråden start
//-------------------------------------------------------------------------------

public void start(){

Thread ny = new Thread(this);
ny.start();

} //end start

//-------------------------------------------------------------------------------
// skapa spelplanen forsta gangen
//-------------------------------------------------------------------------------

public void paint(Graphics g) {

for(int i=0; i<5; i=i+1){
for(int j=0; j<8; j=j+1){
g.setColor(Color.blue);
target[i][j] = new Rectangle();
target[i][j].setLocation( 100 + i*(5 + targetBredd), j * (5 +targetHojd));
target[i][j].setSize( targetBredd, targetHojd );
g.fillRect(100 + i*(targetBredd+5) , j * (targetHojd+5) , targetBredd, targetHojd);
if (flagga[i][j]){
g.setColor(Color.white);
target[i][j] = new Rectangle();
target[i][j].setLocation( 100 + i*(5 + targetBredd), j * (5 +targetHojd));
target[i][j].setSize( targetBredd, targetHojd );
g.fillRect(100 + i*(targetBredd+5) , j * (targetHojd+5) , targetBredd, targetHojd);
} //end if
} //end for j
} //end for i

g.setColor(Color.black);
spelare.setLocation((int)(spelareX),(int)(spelareY));
spelare.setSize(spelareBredd,spelareHojd);
g.fillRect((int)(spelareX),(int)(spelareY), spelareBredd, spelareHojd);

g.setColor(Color.black);
g.fillOval((int)(bollX-radie),(int)(bollY-radie),
(int)(2*radie),(int) (2*radie));
} //end paint

//-------------------------------------------------------------------------------
// run metoden
//-------------------------------------------------------------------------------

public void run(){

while ( true){

bollX = bollX + hastBollX;
bollY = bollY + hastBollY;
flytta();
innanforRam();
kollaTraff();
kollaForlust();
kollaKrock();
showStatus(raknare + " poäng");
repaint();

try { Thread.sleep(10);} catch(InterruptedException ie){}
} //end while
} //end run

//-------------------------------------------------------------------------------
// keyevent
//-------------------------------------------------------------------------------

public void keyPressed(KeyEvent ke){
if(ke.getKeyCode() == KeyEvent.VK_LEFT) leftDown = true;
if(ke.getKeyCode() == KeyEvent.VK_RIGHT) rightDown = true;

} //end keypressed
public void keyReleased(KeyEvent ke){
if(ke.getKeyCode() == KeyEvent.VK_LEFT) leftDown = false;
if(ke.getKeyCode() == KeyEvent.VK_RIGHT) rightDown = false;


} //end key released
public void keyTyped(KeyEvent ke){} //end typed

//-----------------------------------------------------------------
//flimmerfritt
//-----------------------------------------------------------------

public void update(Graphics g){

rityta.clearRect(0,0,width,height);
paint(rityta);
g.drawImage(minnesbild, 0, 0, this);

} //end update

//-----------------------------------------------------------------
//hålla innanför ramen
//-----------------------------------------------------------------

public void innanforRam(){
//hall bollen inne
if (bollX+radie > width)
hastBollX = -hastBollX;
if (bollX-radie < 0)
hastBollX = -hastBollX;
if (bollY+radie > height)
hastBollY = -hastBollY;
if (bollY-radie < 0)
hastBollY = -hastBollY;
//hall spelaren inne
if (spelareX+spelareBredd > width)
spelareHast = 0;
if (spelareX < 0)
spelareHast = 0;
if (spelareX + spelareBredd > width && leftDown)
spelareHast = 3;
if (spelareX < 0 && rightDown)
spelareHast = 3;

} //end ram

//-----------------------------------------------------------------
//flytta spelare
//-----------------------------------------------------------------

public void flytta(){

if(leftDown){
spelareX = spelareX - spelareHast;
} //end if
if(rightDown){
spelareX = spelareX + spelareHast;
} //end if
} //end flytta

//-----------------------------------------------------------------
//kolla krock
//-----------------------------------------------------------------

public void kollaKrock(){

if ( bollX < spelareX + spelareBredd && bollX > spelareX && bollY + radie + 1 == spelareY){
hastBollY = -hastBollY;

} //end if
} //end krock

//-----------------------------------------------------------------
//kolla forlust
//-----------------------------------------------------------------/*

public void kollaForlust(){

if ( bollY + radie + 5 == height){

JOptionPane.showMessageDialog(null, "du forlorade");
bollY = 300;
bollX = 150;
spelareX = width/2-40;
spelareY = 370;
hastBollY = 2;
hastBollX = 2;
spelareHast = 0;
raknare = 0;

} //end if
} //end kollaforlust

//-----------------------------------------------------------------
//kolla traff
//-----------------------------------------------------------------/*

public void kollaTraff(){

for(int i=0; i<5; i=i+1){ //kolla traff vanster
for(int j=0; j<8; j=j+1){
if (bollX + radie + 3 == 100 + i*(5 + targetBredd) && bollY + radie > j * (5 +targetHojd) && bollY + radie < j * (5 +targetHojd) + targetHojd && flagga[i][j] == false){
flagga[i][j] = true;
raknare = raknare + 20;
hastBollX = -hastBollX;
} //end if
} //end for j
} //end for i

for(int i=0; i<5; i=i+1){ //kolla traff hoger
for(int j=0; j<8; j=j+1){
if (bollX - radie - 3 == 100 + i*(5 + targetBredd) + targetBredd && bollY + radie > j * (5 +targetHojd) && bollY + radie < j * (5 +targetHojd) + targetHojd && flagga[i][j] == false){
flagga[i][j] = true;
raknare = raknare + 20;
hastBollX = -hastBollX;
} //end if
} //end for j
} //end for i

for(int i=0; i<5; i=i+1){ //kolla traff uppe
for(int j=0; j<8; j=j+1){
if (bollY + radie + 3 == j * (5 + targetHojd) && bollX + radie > 100 + i*(5 + targetBredd) && bollX < 100 + i*(5 + targetBredd) + targetBredd && flagga[i][j] == false){
flagga[i][j] = true;
raknare = raknare + 20;
hastBollY = -hastBollY;
} //end if
} //end for j
} //end for i

for(int i=0; i<5; i=i+1){ //kolla traff nere
for(int j=0; j<8; j=j+1){
if (bollY - radie - 3 == j * (5 + targetHojd) + targetHojd && bollX + radie > 100 + i*(5 + targetBredd) && bollX < 100 + i*(5 + targetBredd) + targetBredd && flagga[i][j] == false){
flagga[i][j] = true;
raknare = raknare + 20;
hastBollY = -hastBollY;
} //end if
} //end for j
} //end for i
} //end kollatraff

}//end class
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------


Thanks for the help btw smile.gif


User is offlineProfile CardPM
+Quote Post

5thWall
RE: Collision Detection With Objects
6 Dec, 2008 - 02:49 PM
Post #10

D.I.C Head
**

Joined: 17 Sep, 2008
Posts: 209



Thanked: 6 times
My Contributions
Okay just ran your code and I see what you mean.

If I remember correctly, to find out if the ball collided with a block we need to use something like this:

pseudo

FOR all targets:
IF ballX + radius > left_edge
AND ballX - radius < right_edge
AND ballY - radius < bottom
AND ballY + radius > top
THEN target hit;


The question then becomes, which side did it hit? Because the intervals between updates are small, we could just assume that the side the ball is closest to is the one it hit. And since we're just reversing the x or y direction, all we should need to find out is if it hit one of the sides or the top or bottom.

pseudo

int sides;
int topOrBottom;

//Find out which side the ball is closest to
sides = Math.Abs(ballX - right_side)
IF Math.Abs(ballX - left_side) < sides
THEN sides = Math.Abs(ballX - left_side)

//Find out if the ball is closer to the top or bottom
topOrBottom = Math.Abs(ballY - top)
IF Math.Abs(ballY - bottom) < topOrBottom
THEN topOrBottom = Math.Abs(ballY - bottom)

//If the ball is closer to the sides then change the x direction
//Otherwise change the y direction
IF sides < topOrBottom
THEN hastBollX = -hastBollX
ELSE hastBollY = - hastBollY

User is offlineProfile CardPM
+Quote Post

gawdlike
RE: Collision Detection With Objects
7 Dec, 2008 - 01:33 PM
Post #11

New D.I.C Head
*

Joined: 16 Nov, 2008
Posts: 41


My Contributions
nvm, i got it, had to asign the top or bottom or sides variables to be doubles smile.gif

This post has been edited by gawdlike: 7 Dec, 2008 - 01:38 PM
User is offlineProfile CardPM
+Quote Post

gawdlike
RE: Collision Detection With Objects
7 Dec, 2008 - 02:42 PM
Post #12

New D.I.C Head
*

Joined: 16 Nov, 2008
Posts: 41


My Contributions
ok, so this didnt really solve it. in fact, now i have no idea why, but it still skips and sometimes removes whole columns at a time. tongue.gif

java

public void kollaTraff(){

for(int i=0; i<5; i=i+1){ //kolla traff vanster
for(int j=0; j<8; j=j+1){
if (bollX + radie > 100 + i*(5 + targetBredd) && bollX - radie < 100 + (i * (5 +targetBredd) + targetBredd) && bollY + radie > j * (5 +targetHojd) && bollY - radie < j * (5 +targetHojd) + targetHojd && flagga[i][j] == false){
flagga[i][j] = true;
raknare = raknare + 20;

sidor = Math.abs(bollX - 100 + i * (5 + targetBredd)); //avstand hoger
if(Math.abs(bollX - 100 + (i * (5 + targetBredd) + targetBredd)) < sidor){
sidor = Math.abs(bollX - 100 + i * (5 + targetBredd) + targetBredd); // om avstandet till vanster ar mindre
} //end if math
uppeNere = Math.abs(bollY - j * (5 + targetHojd));
if(Math.abs(bollY - (j * (5 + targetHojd) + targetHojd)) < uppeNere){
uppeNere = Math.abs(j * (5 + targetHojd) + targetHojd);
}
if (sidor < uppeNere)
hastBollX = -hastBollX;
else
hastBollY = -hastBollY;
} //end if kolla traffen forst
} //end for j
} //end for i



User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 7/4/09 06:46PM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month