Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (489)
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  
  Can anyone see anything wrong with this?  (Read 716 times)
0 Members and 1 Guest are viewing this topic.
Offline Bassex96

Senior Member


Medals: 4



« Posted 2013-04-17 03:06:53 »

It's my collision script which i've been working on for the past few days...I thought I had it correct, but it still seems all messed up. It's taking away too many bricks. It takes away the brick it hits and takes the last brick left on the board (most bottom right brick)

It's driving me crazy.

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  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
public void checkBrickCollision()
   {
      int ballLeft = (int)ball.ballRect.x;
      int ballRight = (int)ball.ballRect.x + (int)ball.ballRect.width;
      int ballTop = (int)ball.ballRect.y + (int)ball.ballRect.height;
      int ballBottom = (int)ball.ballRect.y;
     
       
     
         for (int i=0;i<level.getBrickCount();i++) {
           
            if (level.bricks[i].GetVisible() == true) {
               
           
      Vector2 Top = new Vector2(ballLeft + (ball.ballRect.width / 2),ballTop );
      Vector2 TopLeft = new Vector2(ballLeft,ballBottom+ball.ballRect.height);
      Vector2 TopRight = new Vector2(ballLeft + ball.ballRect.width,ballBottom+ball.ballRect.height);
     
     
      Vector2 Bottom = new Vector2(ballLeft + (ball.ballRect.width/2),ballBottom);
      Vector2 BottomLeft = new Vector2(ballLeft,ballBottom);
      Vector2 BottomRight = new Vector2(ballLeft + ball.ballRect.width,ballBottom);
     
      Vector2 Right = new Vector2(ballRight,ballBottom + ball.ballRect.height/2);
      Vector2 Left = new Vector2(ballLeft,ballBottom + ball.ballRect.height/2);
     
     
      //int brickLeft = (int)bricks[i].brickRect.x;
     //int brickRight = (int)bricks[i].brickRect.x +(int)bricks[i].brickRect.width;
     //int brickTop = (int)bricks[i].brickRect.y + (int)bricks[i].brickRect.height;
     //int brickBottom = (int)bricks[i].brickRect.x;
     
      if (ball.getRect().overlaps(level.bricks[i].getRect()))
         //System.out.println("Overlapped");
     
      {
     
         
         
         if (level.bricks[i].getRect().contains(Bottom.x,Bottom.y)) {
            ball.ballSpeedY = -ball.ballSpeedY;
            level.bricks[i].isVisible = false;
     
            level.brickCount--;
            System.out.println("Top");
            return;
           
         }
         
         if (level.bricks[i].brickRect.contains(TopLeft.x,TopLeft.y)) {
            ball.ballSpeedX = -ball.ballSpeedX;
            ball.ballSpeedY = -ball.ballSpeedY;
            level.bricks[i].isVisible = false;
     
            level.brickCount--;
            System.out.println("BottomRight");
            return;
         }
         
     
         
         if (level.bricks[i].brickRect.contains(TopRight.x,TopRight.y)) {
            ball.ballSpeedX = -ball.ballSpeedX;
            ball.ballSpeedY = -ball.ballSpeedY;
            level.bricks[i].isVisible = false;
     
            level.brickCount--;
            System.out.println("BottomLeft");
            return;
         }
     
         
         if (level.bricks[i].brickRect.contains(BottomLeft.x,BottomLeft.y)) {
            ball.ballSpeedX = -ball.ballSpeedX;
            ball.ballSpeedY = -ball.ballSpeedY;
            level.bricks[i].isVisible = false;
     
            level.brickCount--;
            System.out.println("TopRight");
            return;
         }
         
         
         if (level.bricks[i].brickRect.contains(BottomRight.x,BottomRight.y)) {
            ball.ballSpeedX = -ball.ballSpeedX;
            ball.ballSpeedY = -ball.ballSpeedY;
            level.bricks[i].isVisible = false;
            System.out.println("TopLeft");
            level.brickCount--;

            return;
         }
         
         
         if (level.bricks[i].brickRect.contains(Top.x,Top.y)) {
           
            ball.ballSpeedY = -ball.ballSpeedY;
           
            level.bricks[i].isVisible = false;
     
            level.brickCount--;
            System.out.println("Bottom");
            return;
           
         }
         if (level.bricks[i].brickRect.contains(Left.x,Left.y)) {
           
           
                       
            ball.ballSpeedX = -ball.ballSpeedX;
            level.bricks[i].isVisible = false;
     
            level.brickCount--;
            System.out.println("Right");
            return;
           
         }
         if (level.bricks[i].brickRect.contains(Right.x,Right.y)) {
           
               
            ball.ballSpeedX = -ball.ballSpeedX;
            level.bricks[i].isVisible = false;
     
            level.brickCount--;
            System.out.println("Left");
            return;
           
         }
         
            System.out.println(level.getBrickCount());
           
      }
     
      }  
     
   
   }
   
   }



*EDIT*

This is my Level class:
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  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
package com.psillicoder.brickbreaker;
import java.util.*;
import com.psillicoder.brickbreaker.Brick;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class Level {
   final int BLANK = 0;
   final int BLUE = 1;
   
   public int brickCount;
   int ROW = 12;
   int COL = 12;
   public Brick[] bricks = new Brick[(ROW)* (COL)];
   int board[][] = new int[ROW][COL];
   
   public Level() {
         board = new int[][] {
            {2,2,2,2,2,2,2,2,2,2,2,2,},
            {3,3,3,3,3,3,3,3,3,3,3,3,},
            {1,1,1,1,1,1,1,1,1,1,1,1,},
            {1,3,1,3,1,1,3,3,3,3,3,1,},
            {1,3,1,3,1,1,1,1,3,1,1,1,},
            {1,3,3,1,1,1,1,1,3,1,1,1,},
            {1,3,1,3,1,1,1,1,3,1,1,1,},
            {1,3,1,3,1,1,1,1,3,1,1,1,},
            {1,1,1,1,1,1,1,1,1,1,1,1,},
            {3,3,3,3,3,3,3,3,3,3,3,3,},
            {2,2,2,2,2,2,2,2,2,2,2,2,},
            {1,1,1,1,1,1,1,1,1,1,1,1,},
      };
   }


   

   public void LoadBricks() {
      this.brickCount = 0;
      for (int x = 0; x < ROW; x++) {
         for (int y = 0; y < COL;y++) {
           
            if (board[x][y] != 0) {
            bricks[this.brickCount] = new Brick(board[x][y],y*32 + 20,-x*16 + 575, true);
            this.brickCount++;
            }
            if (board[x][y] == 0) {
               //this.brickCount++;
              }
           
         }
      }
     
     
   }
   
   public void drawBricks(SpriteBatch batch) {
     
      int intBricks = brickCount;
     
      for (int i=0;i<intBricks;i++) {
         if (bricks[i].GetVisible() == true) {
            bricks[i].draw(batch);
         
         }
         if (bricks[i].GetVisible()==false) {
            //this.brickCount--;
           
         }
         
           
         
         
         
      }
   
   }
   
   

   public int getBrickCount() {
      return bricks.length;
   }
   
   
   
   
}

Offline SHC
« Reply #1 - Posted 2013-04-17 03:41:04 »

Are you following the zetcode.com breakout tutorial? There's a more easier way to bounce of from bricks. Calculate the distances between objects and you can predict the way it collides.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
int xd = Math.abs( (ball.x + ball.width - brick.x - brick.width) /2 );
int yd = Math.abs( (ball.y + ball.height - brick.y - brick.height) /2 );

if (xd > yd)
{
    // Collision on top or bottom, reverse y velocity
   ball.ballSpeedY = -ball.ballSpeedY;
}
if (yd > xd)
{
    // Collision on left or right, reverse x velocity
   ball.ballSpeedX = -ball.ballSpeedX;
}
if (xd == yd)
{
    // Collision on corners, reverse both
   ball.ballSpeedX = -ball.ballSpeedX;
    ball.ballSpeedY = -ball.ballSpeedY;
}

Offline Bassex96

Senior Member


Medals: 4



« Reply #2 - Posted 2013-04-17 21:45:45 »

I originally wrote the code from scratch but kept having trouble with it acting weird, so I implemented the method used on zetcode. Your way seems a lot better and more efficient. I'm going to implement it. I'm not sure if I fully understand the code though. I understand its measuring distance..but how? Sorry, I feel like an idiot. I'm not sure what I'm missing.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Bassex96

Senior Member


Medals: 4



« Reply #3 - Posted 2013-04-18 00:00:58 »

I've got it implemented now, but it still seems to be calling more than one of the side checks. It takes away extra bricks from where I have each removing one from level.brickCount.

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  
   public void checkBrickCollision()
   {
      for (int i=0;i<level.getBrickCount();i++) {
           
            if (level.bricks[i].GetVisible() == true) {
               if (ball.getRect().overlaps(level.bricks[i].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;
             level.bricks[i].isVisible = false;
             level.brickCount--;
             System.out.println("Top/Bottom");
             return;
         }
     
     
         if (yd > xd)
         {
             // Collision on left or right, reverse x velocity
            ball.ballSpeedX = -ball.ballSpeedX;
             level.bricks[i].isVisible = false;
             level.brickCount--;
             System.out.println("Sides");
             return;
         }
         
         if (xd == yd)
         {
             // Collision on corners, reverse both
            ball.ballSpeedX = -ball.ballSpeedX;
             ball.ballSpeedY = -ball.ballSpeedY;
             level.bricks[i].isVisible = false;
             level.brickCount--;
             System.out.println("Corners");
             return;
         }        
      }      
   }
}
}
   



Also, sometimes the ball will go straight through a column when it's going straight up. It is only calling the sides in the console.
Offline SHC
« Reply #4 - Posted 2013-04-18 08:36:50 »

This just determines the side of collision and reverses the direction. Upon collision, before calculating the sides, align the ball outside the brick. So that this appears on the screen like ball touched the brick just at that instant and the side detection will be correct. These are my methods in my engine.

alignWith()

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
public void alignWith(GObject other){
    // Get the intersection rectangle
   Rectangle i = getBounds().intersection(other.getBounds());
    if (i.width > i.height){
        // A vertical collision
       if (getY()<other.getY()){
            // We're on top of other
           setY(other.getY()-getHeight());
        } else if (getY()>other.getY()){
            // We're below other
           setY(other.getY()+other.getHeight());
        }
    } else {
        // A horizontal collision
       if (getX()<other.getX()){
            // We're left to other
           setX(other.getX()-getWidth());
        } else if (getX()>other.getX()){
            // We're right to other
           setX(other.getX()+other.getWidth());
        }
    }
}


bounce()

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
public void bounce(GObject other){
    int xd = (int) ((other.x + other.getWidth() / 2) - (x + getWidth() / 2));
    int yd = (int) ((other.y + other.getHeight() / 2) - (y + getHeight() / 2));
    if (xd < 0) {
        xd = -xd;
    }
    if (yd < 0) {
        yd = -yd;
    }
    if (xd > yd) {
        dx = -dx;
    } else {
        dy = -dy;
    }
}


And in my game, I'll call this as

1  
2  
3  
4  
5  
6  
7  
8  
public void collision(GObject other)
{
    if (other instanceof Brick)
    {
        alignWith(other);
        bounce(other);
    }
}


This is working perfectly in both my engines (GEJ and GECS). Also a screenshot of the problem describes it more.

Offline Bassex96

Senior Member


Medals: 4



« Reply #5 - Posted 2013-04-18 23:28:01 »

I get what you're saying. I'm going to try to implement it. The only thing I don't understand is why you would check if i.width > i.height to find out if its a vertical collision or a horizontal collision? Since my brick is wider than it is tall, then wouldn't it always return a vertical collision?
Offline Bassex96

Senior Member


Medals: 4



« Reply #6 - Posted 2013-04-29 00:15:18 »

I'm trying to implement this but i'm having trouble getting the intersection rectangle...I've tried passing the rectangle and adding .intersects but it doesn't seem to be a defined method..All I have is a boolean overlap. I'm confused.
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 (18 views)
2014-08-28 18:26:30

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

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

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

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

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

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

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

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

BurntPizza (38 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!