473,614 Members | 2,101 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Game collision detection with multiple objects

42 New Member
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 5539
Humakt
42 New Member
Just found out that Java 2D has hit detection, maybe it will help?
Jul 30 '09 #2
JosAH
11,448 Recognized Expert MVP
@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 New Member
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 Recognized Expert MVP
@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 New Member
@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 Recognized Expert MVP
@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 New Member
@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
3170
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 information. Until now, we home-brewed all 3D-related calculations for two reasons: First, we thought we'd get away with pretty basic and simple stuff throughout the project. Second, we didn't detect even one Python module that would support x3d...
60
7243
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 The detector requires javascript 1.0 to work. This translates to netscape 2.0 and IE 3.0 (although maybe IE 2.0 also works with it)
4
3299
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 interact with a rich 3D environment. Seem to be 90% of the way there! My problems are related to calculations where the result tends to zero (or another defined limit.) Have loads of cases where this kind of interaction occurs but this one
15
4466
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 computations, the only data transfered is user mouse/kbd input. It works synchronously, but somehow, when I play in client window, both client and server have 17 fps, while when playing in server window, server has 44 fps while client ...
17
2664
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 TileMap { public:
1
1343
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 suggestions on improving it. It's far from fully working, but some suggestions on the following would be great. Please download it from: http://homepage.eircom.net/~basquilletj/AirHockey.rar.
2
5236
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 bombs no fancy graphics.... with you controlling your spacecraft/tank ( a line ) at the bottom just trying to avoid them :) if anyone knows if its possible and could help me would be much appreciated
10
3250
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 where you could improve the application by detecting the browser vendor/version. Some of the posters here disagreed. Since then, I've had to deal with a few of these cases; some of them could be rewritten to use object detection, and some couldn't....
0
8176
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8120
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8265
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8423
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7047
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5537
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4048
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2560
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1420
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.