Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (553)
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 having trouble determining which side collided..  (Read 657 times)
0 Members and 1 Guest are viewing this topic.
Offline Bassex96

Senior Member


Medals: 4



« Posted 2013-05-18 19:26:21 »

Hey guys, i've been working on this for some time now. I got a new job and haven't been able to put as much time in lately, but I can't get it figured out.

I posted another time on the issue, and someone told me to align the ball with the brick..I tried but cannot get that to work either. I feel like this shouldn't be as hard as it is, and it's very disheartening..if I can't get this figured out, how the hell am I going to program anything of use? ya know?

Anyways, it's my brickbreaker/breakout game. I'm using LibGDX and i'm using rectangles for the ball and the brick..I feel like I should have used a circle for the ball instead, but oh well. I just want to get this game completely finished so I can move on to something else. This is just a "learner" game.

Anyways, collision detection on the top and bottom seem to be working just fine..But when it hits the side, it acts weird and bounces incorrectly..The corners act a little odd as well...

Here is my collision detection 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  
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  
63  
public void checkBrickCollision()
   {
      for (int i=0;i<level.brickCount;i++) {
           
            if (level.bricks[i].GetVisible() == true) {
               if (level.bricks[i].getRect().overlaps(ball.getRect()))
               {
     
         
         
         int xd = (int) Math.abs( ((ball.ballRect.x + ball.ballRect.width) - (level.bricks[i].brickRect.x + level.bricks[i].getRect().width)) /2 );
         int yd = (int) Math.abs( ((ball.ballRect.y + ball.ballRect.height) - (level.bricks[i].brickRect.y + level.bricks[i].getRect().height)) /2 );
         
         if (xd > yd)
         {
             // Collision on top or bottom, reverse y velocity
           
             ball.ballSpeedY = -ball.ballSpeedY;
             Score score = new Score(level.bricks[i].getScore(),(int)level.bricks[i].brickRect.x,(int)level.bricks[i].brickRect.y);
             scoreList.add(score);
             level.bricks[i].Destroy();
             //level.brickCount--;
            System.out.println("Top/Bottom X: " + xd + " Y: " + yd);
             
             return;
         }
     
     
         if (yd > xd)
         {
             // Collision on left or right, reverse x velocity
           
            ball.ballSpeedX = -ball.ballSpeedX;
             
             Score score = new Score(level.bricks[i].getScore(),(int)level.bricks[i].brickRect.x,(int)level.bricks[i].brickRect.y);
             scoreList.add(score);
             level.bricks[i].Destroy();
             //level.brickCount--;
            System.out.println("Left/Right X: " + xd + " Y: " + yd);
             
             return;
         }
         
         if (xd == yd)
         {
             // Collision on corners, reverse both
            ball.ballSpeedX = -ball.ballSpeedX;
             ball.ballSpeedY = -ball.ballSpeedY;
             Score score = new Score(level.bricks[i].getScore(),(int)level.bricks[i].brickRect.x,(int)level.bricks[i].brickRect.y);
             scoreList.add(score);
             level.bricks[i].Destroy();
             //level.brickCount--;
            System.out.println("Corners");
             
             return;
         }        
     
               //level.brickCount--;
       
               }      
   }
}
}


I just want to get past this issue so I can continue learning other things. I haven't done any new work to the game in a while because of this..

The ball is so unrealistic..It only bounces at a 45 degree angle.
Online HeroesGraveDev

JGO Kernel


Medals: 246
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #1 - Posted 2013-05-18 19:45:09 »

1  
2  
3  
4  
5  
6  
// collides on x axis
boolean cx = Math.abs(x1 - x2) <= (w1 + w2);
// collides on y axis
boolean cy = Math.abs(y1 - y2) <= (h1 + h2);

//then change the if statements to use the booleans.

Offline Bassex96

Senior Member


Medals: 4



« Reply #2 - Posted 2013-05-18 19:50:55 »

Excuse my stupidity, but I don't get that? I don't see how that will fix the problem as i'm doing

1  
2  
   int xd = (int) Math.abs( ((ball.ballRect.x + ball.ballRect.width) - (level.bricks[i].brickRect.x + level.bricks[i].getRect().width)) /2 );
         int yd = (int) Math.abs( ((ball.ballRect.y + ball.ballRect.height) - (level.bricks[i].brickRect.y + level.bricks[i].getRect().height)) /2 );


Which is about the same thing..The title of this is a bit misleading I guess..My collision detection works, just not correctly all the time.

So you're taking the x values of the ball and the brick and subtracting them and then seeing if the width of both added together is greater than that value?

I don't want to just take that code and copy and paste it, i'm trying to figure out how this works to do it for myself. Like I said, this is a learner game..Everything else i've done has taught me a lot and I haven't had as much of a problem..But this is bothering me pretty badly.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online HeroesGraveDev

JGO Kernel


Medals: 246
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #3 - Posted 2013-05-18 19:57:16 »

The code I provided takes the gap between the centres and checks if it's less than the sum of the bounds.

Your code is a bit weird. You're adding the width to the pos with one, and doing the same to the other and halving it. Huh I think that explains the problem well enough.

Offline matheus23

JGO Kernel


Medals: 106
Projects: 3


You think about my Avatar right now!


« Reply #4 - Posted 2013-05-18 20:00:59 »

I just jumped into the discussion, but...

There is some very, very, simple trick:
1. Move object on x axis by the amount it wants to move
2. check whether collides, if true => collision on x axis

3. Move object on y axis by the amount it wants to move
4. check whether collides, if true => collision on y axis

That's it Smiley
Hope it helps. Not got that much time to read atm...  Undecided

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline Bassex96

Senior Member


Medals: 4



« Reply #5 - Posted 2013-05-18 20:07:28 »

I just jumped into the discussion, but...

There is some very, very, simple trick:
1. Move object on x axis by the amount it wants to move
2. check whether collides, if true => collision on x axis

3. Move object on y axis by the amount it wants to move
4. check whether collides, if true => collision on y axis

That's it Smiley
Hope it helps. Not got that much time to read atm...  Undecided

Dude...That makes perfect sense and I think that will solve my problem. I didn't even think about that. It seems the issue is that it detects more than one collision so when it's moving, it changes direction twice, which puts it going the same way it was. I'm going to try that and let you know. Thanks!
Offline Bassex96

Senior Member


Medals: 4



« Reply #6 - Posted 2013-05-18 20:08:58 »

The code I provided takes the gap between the centres and checks if it's less than the sum of the bounds.

Your code is a bit weird. You're adding the width to the pos with one, and doing the same to the other and halving it. Huh I think that explains the problem well enough.

That code was mostly from someone who helped me on here..I just copied and pasted it so I didn't understand it. I was going to come back to it and break it down..But it caused the problem i'm having now, so I haven't been able to get past it because I didn't understand it.
Offline Bassex96

Senior Member


Medals: 4



« Reply #7 - Posted 2013-05-18 21:09:25 »

I'm still getting the same issue.

I took a screen record of it, forgive my crappy recording software, very choppy.

Fast forward to around :39 and youll see part of what i'm talking about.

http://www.youtube.com/watch?v=Ttqe6OfC32w&feature=youtu.be
Online HeroesGraveDev

JGO Kernel


Medals: 246
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #8 - Posted 2013-05-18 21:11:55 »

Can we see the new code?

Offline Bassex96

Senior Member


Medals: 4



« Reply #9 - Posted 2013-05-18 21:17:23 »

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  
   
   public void checkBrickCollision()
   {
      for (int i=0;i<level.brickCount;i++) {
           
            if (level.bricks[i].GetVisible() == true) {
               if (level.bricks[i].getRect().overlaps(ball.getRect()))
               {
     
         
         
         
         
         boolean cx = Math.abs(ball.getRect().x - level.bricks[i].getRect().x) <= Math.abs(ball.getRect().width + level.bricks[i].getRect().width);
               
         boolean cy = Math.abs(ball.getRect().y - level.bricks[i].getRect().y) <= Math.abs(ball.getRect().height + level.bricks[i].getRect().height);
                 
         if (cx)
         {
             // Collision on top or bottom, reverse y velocity
           
             ball.ballSpeedY = -ball.ballSpeedY;
             Score score = new Score(level.bricks[i].getScore(),(int)level.bricks[i].brickRect.x,(int)level.bricks[i].brickRect.y);
             scoreList.add(score);
             level.bricks[i].Destroy();
             //level.brickCount--;
            System.out.println("Top/Bottom X: " + cx + " Y: " + cy);
             
             return;
         }
     
     
         if (cy)
         {
             // Collision on left or right, reverse x velocity
           
            ball.ballSpeedX = -ball.ballSpeedX;
             
             Score score = new Score(level.bricks[i].getScore(),(int)level.bricks[i].brickRect.x,(int)level.bricks[i].brickRect.y);
             scoreList.add(score);
             level.bricks[i].Destroy();
             //level.brickCount--;
            System.out.println("Left/Right X: " + cx + " Y: " + cy);
             
             return;
         }
         
               //level.brickCount--;
       
               }      
   }
}
}


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.

TehJavaDev (12 views)
2014-08-28 18:26:30

CopyableCougar4 (24 views)
2014-08-22 19:31:30

atombrot (37 views)
2014-08-19 09:29:53

Tekkerue (30 views)
2014-08-16 06:45:27

Tekkerue (29 views)
2014-08-16 06:22:17

Tekkerue (18 views)
2014-08-16 06:20:21

Tekkerue (27 views)
2014-08-16 06:12:11

Rayexar (65 views)
2014-08-11 02:49:23

BurntPizza (41 views)
2014-08-09 21:09:32

BurntPizza (33 views)
2014-08-08 02:01:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!