Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (475)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (530)
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  
  Still can't seem to get my collision detection right  (Read 427 times)
0 Members and 1 Guest are viewing this topic.
Offline Bassex96

Senior Member


Medals: 4



« Posted 2013-04-14 23:28:29 »

I'm using rectangles and checking bounds when they overlap..Sometimes, a whole row of brick disappears! When the brick is hit is sets isVisible to false and I have a check setup so that when it is not visible it takes 1 away from brickcount. Here is my collision code:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
      if (brick.GetVisible()==true)
      {
         
     
      if (brick.brickRect.overlaps(ballRect))
      {
     
         if (ballRect.x + ballRect.width >= brick.brickRect.x && ballRect.x < brick.brickRect.x) {
            ballSpeedX = -ballSpeedX;
            brick.isVisible = false;
            System.out.println("Left Side");
         }
         if (ballRect.x <= brick.brickRect.x+brick.brickRect.width && ballRect.x > brick.brickRect.x) {
            ballSpeedX = -ballSpeedX;
            brick.isVisible = false;
            System.out.println("Right Side");
         }
         if (ballRect.y + ballRect.height >= brick.brickRect.y && ballRect.y < brick.brickRect.y + brick.brickRect.height) {
            ballSpeedY = -ballSpeedY;
            brick.isVisible = false;
            System.out.println("Bottom Side");
         }
         if (ballRect.y <= brick.brickRect.y+brick.brickRect.height && ballRect.y > brick.brickRect.y + brick.brickRect.height) {
            ballSpeedY = -ballSpeedY;
            brick.isVisible = false;
            System.out.println("Top Side");
         }
Offline blaa_vogn

Junior Member


Projects: 2



« Reply #1 - Posted 2013-04-14 23:39:35 »

Not that it should matter, but you have a duplicated line if your last if.

It is hard to debug other peoples code, but I would guess that your error is in either the overlaps function.

Try this to see how many collisions you register:

1  
2  
3  
4  
5  
6  
      if (brick.brickRect.overlaps(ballRect))
      {
            System.out.println("Collision detected");
           
             //rest of code here
      }


      
Offline Bassex96

Senior Member


Medals: 4



« Reply #2 - Posted 2013-04-14 23:57:07 »

I put message out for each side that is hit, and that is definitely the problem. It's calling them way too much and i cant figure out why.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline blaa_vogn

Junior Member


Projects: 2



« Reply #3 - Posted 2013-04-15 00:11:48 »

Well your problem is most likely in your overlap function then, not this if statement.
Offline Bassex96

Senior Member


Medals: 4



« Reply #4 - Posted 2013-04-15 01:33:15 »

There is no overlap function. It's built in. I'm using LibGDX. I just type if rect1.overlaps(rect2)
Offline philfrei
« Reply #5 - Posted 2013-04-15 01:44:41 »

Could it be due to not testing the x & y at the same time?

For example, let's say you have a row of bricks all at the same X location but different Y locations. When you test only for the X location, ALL the bricks in the same column will go invisible, since the Y is not taken into consideration.

"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
Offline Bassex96

Senior Member


Medals: 4



« Reply #6 - Posted 2013-04-15 02:37:45 »

I don't think so. I've tried this now and it still isn't working. It's calling it too much and taking a row at a time off.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
   int brickLeft = (int)brick.brickRect.x;
      int brickRight = (int)brick.brickRect.x +(int)brick.brickRect.width;
      int brickTop = (int)brick.brickRect.y + (int)brick.brickRect.height;
      int brickBottom = (int)brick.brickRect.x;
     
     
      if (brick.GetVisible()==true)
      {
         
     
     
      //if (brick.brickRect.(ballRect))
     if (ballRect.overlaps(brick.brickRect))
   
     
      {
     
     
         //Left Collision
        if (ballRight > brickLeft && ballLeft < brickLeft && ballTop > brickBottom && ballBottom < brickTop) {
            ballSpeedX = -ballSpeedX;
         brick.isVisible = false;
         System.out.println("Left Side");
         return;
      }
         //Right
        if (ballLeft < brickRight && ballRight > brickRight && ballTop > brickBottom && ballBottom < brickTop) {
            ballSpeedX = -ballSpeedX;
         brick.isVisible = false;
         System.out.println("Right Side");
         return;
      }
           
         //Top
        if (ballBottom < brickTop && ballTop > brickTop && ballRight > brickLeft && ballLeft < brickRight) {
            ballSpeedY = -ballSpeedY;
         brick.isVisible = false;
         System.out.println("Top Side");
         return;
         
      }
         
         //Bottom
        if (ballTop > brickBottom && ballBottom < brickBottom && ballRight > brickLeft && ballLeft < brickRight) {
            ballSpeedY = -ballSpeedY;
         brick.isVisible = false;
         System.out.println("Bottom Side");
         //return;
     }
   
Offline Bassex96

Senior Member


Medals: 4



« Reply #7 - Posted 2013-04-15 03:51:12 »

I got it for the most part. Just set up some Vector variables for the top,left, right, and bottom of the brick and just checked to see which part was in the brick. Here is my code. I still have a problem with the right and left side collisions, but i'm getting it worked out.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
public void checkBrickCollision(Brick brick)
   {
     
     
     
      int ballLeft = (int)ballRect.x;
      int ballRight = (int)ballRect.x + (int)ballRect.width;
      int ballTop = (int)ballRect.y + (int)ballRect.height;
      int ballBottom = (int)ballRect.x;
     
     
     
     
      Vector2 Top = new Vector2(ballLeft + (ballRect.width / 2),ballTop );
      Vector2 Bottom = new Vector2(ballLeft + (ballRect.width/2),ballBottom);
      Vector2 Right = new Vector2(ballRight,ballBottom + ballRect.height/2);
      Vector2 Left = new Vector2(ballLeft,ballBottom + ballRect.height/2);
     
     
      int brickLeft = (int)brick.brickRect.x;
      int brickRight = (int)brick.brickRect.x +(int)brick.brickRect.width;
      int brickTop = (int)brick.brickRect.y + (int)brick.brickRect.height;
      int brickBottom = (int)brick.brickRect.x;
     
     
      if (brick.GetVisible()==true)
      {
         
     
     
      //if (brick.brickRect.(ballRect))
     if (ballRect.overlaps(brick.brickRect))
   
     
      {
     
         
         
         if (brick.brickRect.contains(Bottom.x,Bottom.y)) {
            ballSpeedY = -ballSpeedY;
            brick.isVisible = false;
         }
         if (brick.brickRect.contains(Top.x,Top.y)) {
            ballSpeedY = -ballSpeedY;
            brick.isVisible = false;
         }
         if (brick.brickRect.contains(Left.x,Left.y)) {
            ballSpeedX = -ballSpeedX;
            brick.isVisible = false;
         }
         if (brick.brickRect.contains(Right.x,Right.y)) {
            ballSpeedX = -ballSpeedX;
            brick.isVisible = false;
         }
           
      }
     
      }  
     
   
   }
   
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.

ctomni231 (34 views)
2014-07-18 06:55:21

Zero Volt (30 views)
2014-07-17 23:47:54

danieldean (25 views)
2014-07-17 23:41:23

MustardPeter (27 views)
2014-07-16 23:30:00

Cero (42 views)
2014-07-16 00:42:17

Riven (44 views)
2014-07-14 18:02:53

OpenGLShaders (32 views)
2014-07-14 16:23:47

Riven (34 views)
2014-07-14 11:51:35

quew8 (30 views)
2014-07-13 13:57:52

SHC (66 views)
2014-07-12 17:50:04
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!