Hi,

I can't figure how to detect collision between Ellipse2D and Rectangle2D. I have tried intersects and contains methods with and without getBounds and getBounds2D methods but nothing works.

Here is some code, ball and bat are overlapped.

package net.viped;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.Ellipse2D.Double;
import java.awt.image.BufferedImage;

import javax.swing.JPanel;

public class Board2d extends JPanel {
    private boolean left = false;
    private boolean right = false;
    private boolean up = false;
    private boolean down = false;

    private boolean gameOn = true;

    double batx = 10.0;
    double baty = 0.0;
    double ballx = 10.0;
    double bally = 0.0;
    int tx = 0;
    int ty = 0;

    Ellipse2D ball = new Ellipse2D.Double(ballx, bally, 10, 10);
    Rectangle2D playerBat = new Rectangle2D.Double(batx, baty, 10.0, 50.0);

    AffineTransform at = new AffineTransform();

    gameThread gt = new gameThread();

    public Board2d() {
        setFocusable(true);
        addKeyListener(new ListenKeys());
        setBackground(Color.black);
        initGame();
        gt.start();
    }

    public void initGame() {

    }

    public void paint(Graphics g) {
        super.paint(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setColor(Color.green);
        g2d.setTransform(at);
        g2d.fill(ball);
        g2d.setTransform(at);
        g2d.translate(tx, ty);
        g2d.fill(playerBat);
    }

    public class gameThread extends Thread {
        public void run() {
            while (gameOn) {
                try {
                    sleep(30);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                if (down) {
                    ty++;
                }
                if (up) {
                    ty--;
                }
                if (ball.getBounds2D().contains(playerBat.getBounds2D())) {
                    System.out.println("osuma");
                }
                System.out.println(ball.getBounds2D());
                System.out.println(playerBat.getBounds2D());
//              System.out.println(batx + ", " +baty);
                repaint();
            }
        }
    }

    public class ListenKeys extends KeyAdapter {
        public void keyPressed(KeyEvent e) {
            int key = e.getKeyCode();
            if (key == e.VK_LEFT) {
                left = true;
            }
            if (key == e.VK_RIGHT) {
                right = true;
            }
            if (key == e.VK_UP) {
                up = true;
            }
            if (key == e.VK_DOWN) {
                down = true;
            }
        }

        public void keyReleased(KeyEvent e) {
            up = false;
            down = false;
        }
    }

}

Do you have code for testing the problem? The posted code does not have a main() method.

Yeh sure, here

package net.viped;

import javax.swing.JFrame;

public class Launch extends JFrame{

    /**
     * @param args
     */
    public Launch() {
        add (new Board2d());

        setSize(600, 400);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new Launch();
    }

}

what's supposed to happen when the code executes? All I get are lots of unlabelled messages on the console:
java.awt.geom.Rectangle2D$Double[x=10.0,y=0.0,w=10.0,h=10.0]

Yep those are boundaries both bat and ball and if it detects collision it should print "osuma". Thats the main problem I dont know how to detect collision correctly. I have tried different ways without solution.

So you are asking for an algorithm to detect it? Hmm... On the top of my head, the way I would do is to find whether there is an intersection between those 4 line segments of the rectangle and the ellipse. If there is an intersection (or even touch), it is collided... Have not done any research about that yet.

Edit: Here is a link for some equations to find intersection points of line & ellipse...

Edited 4 Years Ago by Taywin

Actually I am asking what method I should use for it. With between two normal Rectangles, not Rectangle2D like here, I would use intersect method, but now with Rectangle2D I can't get it work.

I have tried intersects and contains

Here's the output I get when I use intersects()

osuma
ball bnds=java.awt.geom.Rectangle2D$Double[x=10.0,y=0.0,w=10.0,h=10.0]
bat bnds=java.awt.geom.Rectangle2D$Double[x=10.0,y=0.0,w=10.0,h=50.0]
osuma

Edited 4 Years Ago by NormR1

Hmm, Works with me too now. I have been modifying code alot and got lost somewhere i guess. Must had have problem somewhere else too cause I am pretty sure I tested that already. Weird. Thanks again.

OK, you just want an approximate of the collision, not a real collision... If you use getBound() from both ellipse and rectangle, you could use intersects() to check it. It is not a real collision but might be good enough.

This question has already been answered. Start a new discussion instead.