Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (576)
games submitted by our members
Games in WIP (497)
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  
  Rectangle Collision detection  (Read 3140 times)
0 Members and 1 Guest are viewing this topic.
Offline bitrot

Junior Newbie





« Posted 2010-05-03 21:34:48 »

I'm making a brick breaker game. For collision detection I am using Rectangle.intersects(). I construct a new Rectangle from the Ball object, and Brick extends rectangle. My problem is that the collision points seem to be off set by a few pixles. When the ball collides with the top or left side of a brick it is colliding before it should and from the bottom and right side it is colliding later than it should. The pictures illustrate this. Here is the code for the brick detection method. This method is inside the Ball class. My question is why is this happening.

    /**
     * Check for brick collisions and adjust ball direction.
     */
    public void detectBrick(Level level) {
        Rectangle r1 = new Rectangle(x, y, radius * 2, radius * 2);
        for (Brick b: Level.theBricks) {
                if (r1.intersects(b)) {
                    if (y > b.y && y < b.y + Brick.BHEIGHT)
                        dx = -dx;
                    else if (x > b.x && x < b.x + Brick.BWIDTH)
                        dy = -dy;
                    else {
                        dx = -dx;
                        dy = -dy;

                    }
//                    level.remove(b); // remove the brick after its been hit
                    break;
               }
        }
    }
Offline bitrot

Junior Newbie





« Reply #1 - Posted 2010-05-03 21:48:32 »

I fixed it. I forgot that the x and y coordinates of the Ball are at its center.
Rectangle r1 = new Rectangle(x - radius, y - radius, 2 * radius, 2 * radius);

Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 12


Game Engineer


« Reply #2 - Posted 2010-05-03 23:25:28 »

Instead of using instantiating a new Rectangle every time, it would be much faster to do this:

1  
2  
3  
4  
5  
6  
7  
if (objA.x - objA.width /2 >= objB.x + objB.width /2 &&
    objA.y - objA.height/2 >= objB.y + objB.height/2 &&
    objA.x + objA.width /2 <= objB.x + objB.width /2 &&
    objA.x + objA.height/2 <= objB.y - objB.height/2)
    {
        //collision = true
   }

See my work:
OTC Software
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline bitrot

Junior Newbie





« Reply #3 - Posted 2010-05-04 07:47:09 »

That is how I originally intended to do it. But I thought it would be easier just to have Brick and Paddle extend Rectangle that way I could just use the intersects() method. How much of a difference would it make? Is it worth recoding?

I have another question. The level in the picture is an ArrayList of Bricks. It is populated and drawn in a class called Level. I was wondering how I would go about creating other levels. I looked into Panel's and layout managers, specifically grid layout, but it didn't seem to work out. I wasn't able to draw the same way I would add a component to the panel.

Each Brick knows its position and dimensions. But it seems like a lot of work using a coordinate system to create levels. Any thoughts?
Offline bitrot

Junior Newbie





« Reply #4 - Posted 2010-05-04 19:29:52 »

I was thinking of doing something like this. Im not sure how to go about creating levels without writing each method manually.

public class Level {

    private HashMap<Point, Point> thePoints;
    private ArrayList<Block> theBlocks;
   
    /**
     * Constructor.
     */
    public Level() {
        thePoints = new HashMap<Point, Point>();
        theBlocks = new ArrayList<Block>();
        buildMap();
       
    }
   
    /**
     * Associate a set of coordinates for each block location. Each block
     * location will be numbered by row and column.
     */
    public void buildMap() {
        for (int i = 0; i < 16; i++) {
            for (int j = 0; j < 10; j++) {
                Point p1 = new Point(i, j);
                Point p2 = new Point(i * 20, j * 40);
                thePoints.put(p1, p2);
            }
        }
    }
   
    /**
     * Determine the block coordinates and create the Block.
     * @param i the row
     * @param j the column
     */
    public void buildBlock(int i, int j) {
        Point p1 = new Point(i, j);
        Point p2 = thePoints.get(p1);
        Block b = new Block(p2.x, p2.y);
        theBlocks.add(b);
    }
   
    /**
     * A solid grid of blocks with a 3 space border.
     */
    public void createLevel1() {
        for (int i = 3; i < 13; i++) {
            for (int j = 3; j < 7; j++) {
                buildBlock(i, j);
               
            }
        }
    }
   
    public void createLevel2() {
        }
   
    /**
     * Draw the level to the canvas.
     * @param g the graphics object.
     */
    public void draw(Graphics g) {
        g.setColor(Color.black);
        for (Block b: theBlocks) {
            g.drawRect(b.x, b.y, b.width, b.height);       
        }
    }
}
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 12


Game Engineer


« Reply #5 - Posted 2010-05-04 19:53:22 »

If you're just multiplying by 20 to get the translated point, I really don't recommend using a HashMap to do that. Instead, just use a method.

1  
2  
3  
4  
Point getTransformedPoint(Point p)
{
    return new Point(p.x * 20, p.y * 20);
}


Something like that.

I also wouldn't extend Rectangle. Instead if you want to use Rectangle, store one in the class's fields. It doesn't make sense to extend Rectangle because your classes aren't actually shapes.

You would do something like this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
public class MyClass
{
    private Rectangle bounds;

    public Rectangle getBounds()
    {
        return bounds;
    }

    public boolean intersects(MyClass mc)
    {
        return bounds.intersects(mc.getBounds());
    }
}

See my work:
OTC Software
Offline bitrot

Junior Newbie





« Reply #6 - Posted 2010-05-05 01:09:42 »

Yeah I guess the map was kind of useless. I rewrote the collision detection using math instead of the rectangle intersection. I think I have more control this way as well. Since that was the only purpose of the rectangles I scrapped them as well.

Since im using a mouse motion listener to move the paddle I think im having problems when the paddle is moving faster than the ball. The paddle will intersect the ball before the ball has a chance to check and respond. All my collision detection is handled by the ball.

And I have another issue. The program is starting to get scattered. The more features and things I add the more unorganized it seems to get and sometimes im not sure where things should go and which classes should be handling what. I only have two semesters of java. Any suggestions? Should I read up on design patterns or something?
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 (12 views)
2014-04-15 18:08:23

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

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

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

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

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

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

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

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

CJLetsGame (182 views)
2014-04-01 02:16:10
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

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
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!