943,964 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 3909
  • Java RSS
Feb 8th, 2007
0

Collision Detection - Micro Java

Expand Post »
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.
Java Syntax (Toggle Plain Text)
  1. public void moveBall() {
  2. while (true) {
  3. ballX += incrementX;
  4. ballY += incrementY;
  5. //hits sides
  6. if (ballX < 0 || ballX > (getWidth()-10)) {
  7. incrementX = -incrementX;
  8. }
  9. //hits paddle or roof
  10. if (ballY < 0 || ballY > (getHeight()-25) && ballX > paddleX && ballX < (paddleX +50)) {
  11. incrementY = -incrementY;
  12. }
  13. //goes out past the paddle
  14. if (ballY >= getHeight()) {
  15. lives -= 1;
  16. ballX = ((getWidth()/2)-5);
  17. ballY = ((getHeight()/2)-5);
  18. }
  19. //hits a block
  20. for (int i = 0; i < 14; i++) {
  21. if ((ballY < blockArray[i].getY() + 10) && (ballX > blockArray[i].getX()) && (ballX < blockArray[i].getX()+30)) {
  22. incrementY = -incrementY;
  23. }
  24. }
  25. repaint();
  26.  
  27. try {
  28. Thread.sleep(10);
  29. } catch (InterruptedException e) {
  30. e.printStackTrace();
  31. }
  32.  
  33. }
  34. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Doops is offline Offline
7 posts
since Mar 2006
Feb 8th, 2007
0

Re: Collision Detection - Micro Java

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":
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..
Reputation Points: 20
Solved Threads: 6
Junior Poster in Training
Cudmore is offline Offline
74 posts
since Nov 2005
Feb 10th, 2007
0

Re: Collision Detection - Micro Java

BallX and BallY refers to the top left most pixel of the sprite. Thanks for the help
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Doops is offline Offline
7 posts
since Mar 2006
Feb 12th, 2007
0

Re: Collision Detection - Micro Java

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.

Java Syntax (Toggle Plain Text)
  1. public boolean overlap(int bx, int by, int radius, Block block)
  2. {
  3. int x1 = block.getX();
  4. int y1 = block.getY();
  5. int x2 = block.getX() + 30;
  6. int y2 = block.getY() + 10;
  7.  
  8. if ((bx+radius > x1) && (bx-radius < x2) && (by+radius > y1) && (by-radius < y2))
  9. {
  10. return true;
  11. }
  12. return false;
  13. }

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.
Reputation Points: 92
Solved Threads: 51
Practically a Posting Shark
Phaelax is offline Offline
856 posts
since Mar 2004
Feb 12th, 2007
0

Re: Collision Detection - Micro Java

Hey, thanks for the replies, I have managed o sort it out using the collideswith(Sprite s) method. Still got lots of little glitches but I think I can Iron those out easy enough. Thanks again.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Doops is offline Offline
7 posts
since Mar 2006
Sep 11th, 2008
-1

Re: Collision Detection - Micro Java

hello!!can you give me all the code?thanks
Reputation Points: 9
Solved Threads: 0
Newbie Poster
Bunter is offline Offline
3 posts
since Sep 2008
Nov 15th, 2008
0

Re: Collision Detection - Micro Java

hi mr. cudmore... can you please tell me some tutorial site or where can i get codes about making java applets. im planning to make a m-learning using netbeans... but i cant start because i dont have background about this... thanks
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mjta18 is offline Offline
7 posts
since Nov 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: searching an array
Next Thread in Java Forum Timeline: Need help in code





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC