Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (581)
games submitted by our members
Games in WIP (500)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Collision Detection  (Read 587 times)
0 Members and 1 Guest are viewing this topic.
Offline Bassex96

Senior Member


Medals: 4



« Posted 2013-04-09 06:18:54 »

Hey guys. I'm working on the collision detection for my brickbreaker game. Originally, I was just checking if the rectangles of the ball and the brick overlapped..As you can imagine, this won't work, so it's time to upgrade and fix it. I'm wondering what the best way to implement it would be...I was in the process of doing

1  
if (ballRect.x >=brick.brickRect.x && ballRect.x <=brick.brickRect.x && ballRect.y >=brick.brickRect.y && ballRect.y <= brick.brickRect.y )


But there has to be a better way..plus that way isn't working lol
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 117
Projects: 4
Exp: 3 years



« Reply #1 - Posted 2013-04-09 06:25:15 »

I don't see anything wrong with your overlapping idea...
1  
if(ballRectangle.intersects(blockRectangle))
works perfectly fine
Offline Bassex96

Senior Member


Medals: 4



« Reply #2 - Posted 2013-04-09 07:19:05 »

I don't see anything wrong with your overlapping idea...
1  
if(ballRectangle.intersects(blockRectangle))
works perfectly fine

I'm trying to make it a little more accurate to tell which direction of the ball to change.

I've got
1  
2  
3  
4  
5  
6  
if (brick.brickRect.overlaps(ballRect))
      {
         if (brick.brickRect.x <= ballRect.x+ballRect.width) ballSpeedX = -ballSpeedX;
         if (brick.brickRect.x + brick.brickRect.width >= ballRect.x) ballSpeedX = -ballSpeedX;
         if (brick.brickRect.y <= ballRect.y + ballRect.height) ballSpeedY = -ballSpeedY;
         if (brick.brickRect.y + brick.brickRect.height <= ballRect.y) ballSpeedY = -ballSpeedY;


I wish I could get over this damn learning curve. I'm working at it though.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 117
Projects: 4
Exp: 3 years



« Reply #3 - Posted 2013-04-09 07:28:41 »

The "overlaps" part probably isn't necessary if you have those if statements. Anyways, there are multiple problems with your code; for one you should re-check those conditions - shouldn't it be
1  
if (brick.brickRect.x <= ballRect.x)
not
1  
if (brick.brickRect.x <= ballRect.x + ballRect.width)
? Not to mention that all of your conditions should be in one if statement, otherwise it will incorrectly detect collision when only the x is satisfied, and not the y, for example.
Offline Bassex96

Senior Member


Medals: 4



« Reply #4 - Posted 2013-04-09 07:48:55 »

The "overlaps" part probably isn't necessary if you have those if statements. Anyways, there are multiple problems with your code; for one you should re-check those conditions - shouldn't it be
1  
if (brick.brickRect.x <= ballRect.x)
not
1  
if (brick.brickRect.x <= ballRect.x + ballRect.width)
? Not to mention that all of your conditions should be in one if statement, otherwise it will incorrectly detect collision when only the x is satisfied, and not the y, for example.

Yeah, I definitely need to rewrite it. I'm tired so I think it's just getting a little sloppy. The ballrect.x + ballrect.width is to get the right side of the ball.
Offline StumpyStrust
« Reply #5 - Posted 2013-04-09 08:08:43 »

hmmm after you use the intersects method find which side should be trivial.

In order for a object to move there must be a velocity right? Or some value you are incrementing by. Check to see if that value is positive or negative. Do this for both X and Y. Not to correct the collision you would need to find how much these objects intersect and subtract or add to the X and Y. Well look around at some Rectangle2D stuff in java and you will find what you are looking for.

Offline SHC
« Reply #6 - Posted 2013-04-09 08:19:29 »

I simply use this when detected collision.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
public void processCollision(Ball ball, Block block)
{
    int xdist = Math.abs(ball.centerX - block.centerX);
    int ydist = Math.abs(ball.centerY - block.centerY);
    if (xdist>ydist)
    {
        // Collision on top or bottom of block
       ball.vspeed = -ball.vspeed;
    }
    if (ydist>xdist)
    {
        // Collision on left or right of block
       ball.hspeed = -ball.hspeed;
    }
    block.destroy();
}

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (63 views)
2014-04-15 18:08:23

BurntPizza (61 views)
2014-04-15 03:46:01

UprightPath (74 views)
2014-04-14 17:39:50

UprightPath (57 views)
2014-04-14 17:35:47

Porlus (75 views)
2014-04-14 15:48:38

tom_mai78101 (100 views)
2014-04-10 04:04:31

BurntPizza (160 views)
2014-04-08 23:06:04

tom_mai78101 (255 views)
2014-04-05 13:34:39

trollwarrior1 (208 views)
2014-04-04 12:06:45

CJLetsGame (215 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!