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. -
public void checkCollideWithBox(int x, int y, int r, Ball ball, boolean second){
-
boolean collidedwithbox = false;
-
for(int j = 0; j<insidebox.length;j++){
-
if(insidebox[j]){
-
if((((x+r>=boxes[j].getPositionX()&&(x<=boxes[j].getPositionX()+20)
-
&&ball.getDirectionX()>0))||((x<=boxes[j].getPositionX()+20
-
&&(x+r>=boxes[j].getPositionX())
-
&&ball.getDirectionX()<=0)))
-
&&(y+r>=boxes[j].getPositionY()
-
&&y<=boxes[j].getPositionY()+40)
-
&&!boxes[j].getDestroyed())collidedwithbox = true;
-
else insidebox[j] = false;
-
}
-
}
-
boolean yhit = false, xhit = false;
-
for(int i = 0; i<boxes.length && !collidedwithbox&&!(yhit||xhit); i++){
-
if(!boxes[i].getDestroyed()){ //if box is not destroyed already
-
if((((x+r>=boxes[i].getPositionX()&&(x<=boxes[i].getPositionX()+20)
-
&&ball.getDirectionX()>0))||((x<=boxes[i].getPositionX()+20
-
&&(x+r>=boxes[i].getPositionX())&&ball.getDirectionX()<=0)))
-
&&(y+r>=boxes[i].getPositionY()
-
&&y<=boxes[i].getPositionY()+40)){ //if ball is touching or inside the box
-
if((y==boxes[i].getPositionY()+40&&!(ball.getDirectionY()>=0))
-
||(y+r==boxes[i].getPositionY()
-
&&!(ball.getDirectionY()<=0))){ //if ball is on upper or lower side of the box
-
-
//trying to solve corner cases, this is not enough:
-
if(!((x+r==boxes[i].getPositionX()&&ball.getDirectionX()<0)||(x==boxes[i].getPositionX()
-
&&ball.getDirectionX()>0))){
-
yhit = true;
-
xhit = false;
-
}else{
-
yhit = false;
-
xhit = true;
-
}
-
} else xhit = true;
-
if(boxes[i].getHits()<=1){
-
boxes[i].getEffect(ball, b1, b2, ballthread);
-
boxes[i].setDestroyed();
-
if(!boxes[i].isIndestructible()){
-
g.playSound(2);
-
boxesleft--;
-
if(lasthitplayer1ball1)scoreplayer1 +=10;
-
else scoreplayer2 +=10;
-
da.updateScoreBox(scoreplayer1, scoreplayer2);
-
}else g.playSound(3); //indestructible sound
-
if(boxesleft <= 0){
-
collidedwithbox = true;
-
this.changeLevel();
-
}
-
}else{
-
g.playSound(2);
-
boxes[i].setHits(boxes[i].getHits()-1);
-
boxes[i].setColor(boxes[i].getColor()-1);
-
if(!twoballs){
-
if(lasthitplayer1ball1)scoreplayer1 +=10;
-
else scoreplayer2 +=10;
-
}else{
-
if(!second){
-
if(lasthitplayer1ball1)scoreplayer1 +=10;
-
else scoreplayer2 +=10;
-
}else{
-
if(lasthitplayer1ball2)scoreplayer1 +=10;
-
else scoreplayer2 +=10;
-
}
-
-
}
-
da.updateScoreBox(scoreplayer1, scoreplayer2);
-
}
-
insidebox[i] = true;
-
}
-
}
-
}
-
if(!collidedwithbox){
-
if(xhit) ball.setDirectionX(-ball.getDirectionX());
-
else if(yhit)ball.setDirectionY(-ball.getDirectionY());
-
}
-
}
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?
7 5539
Just found out that Java 2D has hit detection, maybe it will help?
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
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.
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
@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. :)
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
@JosAH
No problem anymore, it was just a bad oversight on my side. Thanks for help though.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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)
|
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
|
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 ...
|
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:
| |
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.
|
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
|
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....
|
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...
|
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,...
|
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,...
| |
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...
|
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...
|
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();...
|
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...
|
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
|
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...
| |