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!
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{
//------------------------------------------------------------------------------- // run metoden //-------------------------------------------------------------------------------
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 //------------------------------------------------------------------------------- //-------------------------------------------------------------------------------
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
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.
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.
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?
well, jumping over might be wrong of me to say, it goes through without it "deleting"
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{
//------------------------------------------------------------------------------- // run metoden //-------------------------------------------------------------------------------
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
}//end class //------------------------------------------------------------------------------- //-------------------------------------------------------------------------------
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