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() {
        addKeyListener(new ListenKeys());

    public void initGame() {


    public void paint(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        g2d.translate(tx, ty);

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

    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;

Re: Collision detection shapes 80 80

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

Re: Collision detection shapes 80 80

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);


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

Re: Collision detection shapes 80 80

what's supposed to happen when the code executes? All I get are lots of unlabelled messages on the console:

Re: Collision detection shapes 80 80

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.

Re: Collision detection shapes 80 80

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...

Re: Collision detection shapes 80 80

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.

Re: Collision detection shapes 80 80

I have tried intersects and contains

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

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]

Re: Collision detection shapes 80 80

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.

Re: Collision detection shapes 80 80

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.