•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 401,948 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,305 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 1632 | Replies: 4 | Solved
![]() |
ok, I have created a mobile application for a java enabled phone. I want to make the classic pong game with one paddle, and a number of blocks to break. I have managed to get the ball bouncing around the screen and off the block using a set of if statements checking the location of the ball against the location of the paddle and walls. So the next step is to get ball bouncing off the blocks. So i have a Block() class and within the initialisation I have managed to make an array of blocks to appear on the screen. So the collision detection is next. I have tried the following code but the ball just misses the blocks altogether, I don't understand why the ball is not bouncing off the blocks. The code is on the ball thread, I am simply trying to check the location of the ball against each blocks location. Any help would be great.
public void moveBall() {
while (true) {
ballX += incrementX;
ballY += incrementY;
//hits sides
if (ballX < 0 || ballX > (getWidth()-10)) {
incrementX = -incrementX;
}
//hits paddle or roof
if (ballY < 0 || ballY > (getHeight()-25) && ballX > paddleX && ballX < (paddleX +50)) {
incrementY = -incrementY;
}
//goes out past the paddle
if (ballY >= getHeight()) {
lives -= 1;
ballX = ((getWidth()/2)-5);
ballY = ((getHeight()/2)-5);
}
//hits a block
for (int i = 0; i < 14; i++) {
if ((ballY < blockArray[i].getY() + 10) && (ballX > blockArray[i].getX()) && (ballX < blockArray[i].getX()+30)) {
incrementY = -incrementY;
}
}
repaint();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} Does (ballX, ballY) represent the center of the ball, or the top-left edge of the bounding square?
For one, this if statement looks somewhat "iffy":
Consider grouping the conditions with parenthesis more.. I assume the paddle appears on the top and bottom of the screen.
For blocks, you have to test:
Ball intersects block horizontally (at left OR right) AND vertically (at top OR bottom), therefore...
Horizontally:
If (ballX + ballWidth) >= blockX
Or if ballX <= (blockX + blockWidth)
And vertically:
If (ballY + ballHeight) >= blockY
Or if ballY <= (blockY + blockHeight)
The above considers the ball's bounding box... So, it treats the ball like a square, with (ballX, ballY) represening the top-left corner.
Helpful? Hmmm..
For one, this if statement looks somewhat "iffy":
if (ballY < 0 || ballY > (getHeight()-25) && ballX > paddleX && ballX < (paddleX +50))Consider grouping the conditions with parenthesis more.. I assume the paddle appears on the top and bottom of the screen.
For blocks, you have to test:
Ball intersects block horizontally (at left OR right) AND vertically (at top OR bottom), therefore...
Horizontally:
If (ballX + ballWidth) >= blockX
Or if ballX <= (blockX + blockWidth)
And vertically:
If (ballY + ballHeight) >= blockY
Or if ballY <= (blockY + blockHeight)
The above considers the ball's bounding box... So, it treats the ball like a square, with (ballX, ballY) represening the top-left corner.
Helpful? Hmmm..
synchronized (theWorld) { System.out.println ("It's all mine..."); }
How many people have code in their Sigs?
How many people have code in their Sigs?
•
•
Join Date: Mar 2004
Posts: 717
Reputation:
Rep Power: 6
Solved Threads: 29
This method would take the ball's coordinates from its center, its radius, and a Block object. It'll return true if they're overlapping.
While this works for simple games, it only works as long as the ball's speed does not exceed the width or height of a brick. For instance, if a brick is 10 units tall and a ball moves vertically at 15units per loop, then there's a possibility that it could skip right over the brick and never trigger a collision. So even though we would see that the ball appeared to pass right through the block, the code never sees the two objects overlap.
A solution for this is to use swept-collision. This is where you take the ball's old position of the last game loop and its new position of the current loop and form a line segment. Then using line segment AB (the ball's path) you check if it intersects the rectangle of the block. The algorithm for that could return a time index of when the line would intersect the block. If the value is between o and 1, then there was a collision. Anything outside of that range means there could be an intersection should the line segment continue indefinitely.
public boolean overlap(int bx, int by, int radius, Block block)
{
int x1 = block.getX();
int y1 = block.getY();
int x2 = block.getX() + 30;
int y2 = block.getY() + 10;
if ((bx+radius > x1) && (bx-radius < x2) && (by+radius > y1) && (by-radius < y2))
{
return true;
}
return false;
}While this works for simple games, it only works as long as the ball's speed does not exceed the width or height of a brick. For instance, if a brick is 10 units tall and a ball moves vertically at 15units per loop, then there's a possibility that it could skip right over the brick and never trigger a collision. So even though we would see that the ball appeared to pass right through the block, the code never sees the two objects overlap.
A solution for this is to use swept-collision. This is where you take the ball's old position of the last game loop and its new position of the current loop and form a line segment. Then using line segment AB (the ball's path) you check if it intersects the rectangle of the block. The algorithm for that could return a time index of when the line would intersect the block. If the value is between o and 1, then there was a collision. Anything outside of that range means there could be an intersection should the line segment continue indefinitely.
![]() |
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- DirectX - Collision detection (Game Development)
- Collision Detection (C++)
- Need java tutor? (Java)
Other Threads in the Java Forum
- Previous Thread: Sound Camparison.
- Next Thread: Every problem needs solving


Linear Mode