473,385 Members | 1,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Game collision detection with multiple objects

42
This is 2D game where there are breakable boxes in the middle two bats on both sides and one or more balls breaking the boxes. Kind of like a mixup of pong and breakout.

Collision detection generally works find but I have trouble when the ball is hitting corners of the box. That is particularly annoying to check especially when levels are very different of each other.

This is the function where I check collision with boxes.



Expand|Select|Wrap|Line Numbers
  1. public void checkCollideWithBox(int x, int y, int r, Ball ball, boolean second){
  2.         boolean collidedwithbox = false;
  3.         for(int j = 0; j<insidebox.length;j++){
  4.             if(insidebox[j]){
  5.                 if((((x+r>=boxes[j].getPositionX()&&(x<=boxes[j].getPositionX()+20)
  6.    &&ball.getDirectionX()>0))||((x<=boxes[j].getPositionX()+20
  7.    &&(x+r>=boxes[j].getPositionX())
  8.    &&ball.getDirectionX()<=0)))
  9.    &&(y+r>=boxes[j].getPositionY()
  10.    &&y<=boxes[j].getPositionY()+40)
  11.    &&!boxes[j].getDestroyed())collidedwithbox = true;
  12.                 else insidebox[j] = false;
  13.             }
  14.         }
  15.         boolean yhit = false, xhit = false;
  16.         for(int i = 0; i<boxes.length && !collidedwithbox&&!(yhit||xhit); i++){
  17.             if(!boxes[i].getDestroyed()){ //if box is not destroyed already
  18.                 if((((x+r>=boxes[i].getPositionX()&&(x<=boxes[i].getPositionX()+20)
  19.     &&ball.getDirectionX()>0))||((x<=boxes[i].getPositionX()+20
  20.     &&(x+r>=boxes[i].getPositionX())&&ball.getDirectionX()<=0)))
  21.     &&(y+r>=boxes[i].getPositionY()
  22.     &&y<=boxes[i].getPositionY()+40)){ //if ball is touching or inside the box
  23.                     if((y==boxes[i].getPositionY()+40&&!(ball.getDirectionY()>=0))
  24.        ||(y+r==boxes[i].getPositionY()
  25.        &&!(ball.getDirectionY()<=0))){ //if ball is on upper or lower side of the box
  26.  
  27.                         //trying to solve corner cases, this is not enough:
  28.                         if(!((x+r==boxes[i].getPositionX()&&ball.getDirectionX()<0)||(x==boxes[i].getPositionX()
  29.      &&ball.getDirectionX()>0))){
  30.                             yhit =  true;
  31.                             xhit = false;
  32.                         }else{
  33.                             yhit = false;
  34.                             xhit = true;
  35.                         }
  36.                     } else xhit = true;
  37.                     if(boxes[i].getHits()<=1){
  38.                         boxes[i].getEffect(ball, b1, b2, ballthread);
  39.                         boxes[i].setDestroyed();
  40.                         if(!boxes[i].isIndestructible()){
  41.                             g.playSound(2);
  42.                             boxesleft--;
  43.                             if(lasthitplayer1ball1)scoreplayer1 +=10;
  44.                             else scoreplayer2 +=10;
  45.                             da.updateScoreBox(scoreplayer1, scoreplayer2);
  46.                         }else g.playSound(3); //indestructible sound
  47.                         if(boxesleft <= 0){
  48.                             collidedwithbox = true;
  49.                             this.changeLevel();
  50.                         }
  51.                     }else{
  52.                         g.playSound(2);
  53.                         boxes[i].setHits(boxes[i].getHits()-1);
  54.                         boxes[i].setColor(boxes[i].getColor()-1);
  55.                         if(!twoballs){
  56.                             if(lasthitplayer1ball1)scoreplayer1 +=10;
  57.                             else scoreplayer2 +=10;
  58.                         }else{
  59.                             if(!second){
  60.                                 if(lasthitplayer1ball1)scoreplayer1 +=10;
  61.                                 else scoreplayer2 +=10;
  62.                             }else{
  63.                                 if(lasthitplayer1ball2)scoreplayer1 +=10;
  64.                                 else scoreplayer2 +=10;
  65.                             }
  66.  
  67.                         }
  68.                         da.updateScoreBox(scoreplayer1, scoreplayer2);
  69.                     }
  70.                 insidebox[i] = true;
  71.                 }
  72.             }
  73.         }
  74.         if(!collidedwithbox){
  75.             if(xhit) ball.setDirectionX(-ball.getDirectionX());
  76.             else if(yhit)ball.setDirectionY(-ball.getDirectionY());
  77.         }
  78.     }
X is the ball's x location, y is likewise ball's y location, r is the radius of ball, boolean is just flag to check whether ball is first or second.

Insidebox is boolean array for checking if ball is out of box's area.

Any help to solve this problem?
Jul 30 '09 #1
7 5525
Humakt
42
Just found out that Java 2D has hit detection, maybe it will help?
Jul 30 '09 #2
JosAH
11,448 Expert 8TB
@Humakt
I thought it could only do an intersect() on rectangular shapes ... if your balls move fast enough you could try that method with the bounding rectangle of the ball; the user won't see the difference.

kind regards,

Jos
Jul 30 '09 #3
Humakt
42
I don't think that is a problem (especially when I am already treating the ball as a square) and ellipse in Java 2D is a rectangular shape according to the link below so maybe I should use that.
http://java.sun.com/docs/books/tutor...rimitives.html

And to be specific, the problem doesnt' have as much to do with ball hitting corner but: ball hitting a corner of box when there is another box adjacent to it which ball is also hitting.
Jul 30 '09 #4
JosAH
11,448 Expert 8TB
@Humakt
If you define that a ball can only hit one box at a time only and things move fast enough, a user may not even notice the (physical) inaccuracy.

kind regards,

Jos
Jul 30 '09 #5
Humakt
42
@JosAH

Actually I've done that, but problem is whether ball should go to opposite direction x- or y-wise on coordinates and for that I have to check adjacent boxes as well. I just need to think it through but any external help is still appreciated. :)
Jul 30 '09 #6
JosAH
11,448 Expert 8TB
@Humakt
I need to have a visual on that because I don't understand what the problem is.

kind regards,

Jos
Jul 31 '09 #7
Humakt
42
@JosAH
No problem anymore, it was just a bad oversight on my side. Thanks for help though.
Jul 31 '09 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Thorsten Reichelt | last post by:
Hi, I'm involved in a research project on spatial prepositions. In that project we use very simple, static 3D maps that are represented in a tiny subset of x3d enriched with some few linguistic...
60
by: Fotios | last post by:
Hi guys, I have put together a flexible client-side user agent detector (written in js). I thought that some of you may find it useful. Code is here: http://fotios.cc/software/ua_detect.htm ...
4
by: Dave | last post by:
Hi folks, I am trying to develop a routine that will handle sphere-sphere and sphere-triangle collisions and interactions. My aim is to develop a quake style collision engine where a player can...
15
by: Michael Rybak | last post by:
hi, everyone. I'm writing a 2-players game that should support network mode. I'm now testing it on 1 PC since I don't have 2. I directly use sockets, and both client and server do...
17
by: Mark | last post by:
uhhmmm... not really sure how to word this. i cant get get this to compile.. i'm not sure what the proper syntax to do this is.. hopefully it's self explanatory. here's my class: ------ class...
1
by: Brian Basquille | last post by:
Hello all. Have been working on the Air Hockey game on and off for a couple of weeks now.. but have had plenty of other assignments to keep me busy along with it. But i would like some...
2
by: Daniel | last post by:
Hi , i was just wondering if it is possible to create a mini game in vb.net say for example like space invaders with a spacecraft coming across the top of the screen in a web form dropping...
10
by: Conrad Lender | last post by:
In a recent thread in this group, I said that in some cases object detection and feature tests weren't sufficient in the development of cross-browser applications, and that there were situations...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.