6 Replies - 498 Views - Last Post: 22 June 2012 - 04:41 AM Rate Topic: -----

#1 podypodpod  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 21-January 11

Oval .getBounds collison detection

Posted 19 June 2012 - 06:44 PM

Im making a simple collision detection game, using a circle as the users charicter
I need to find the edges of the circle rather than the center point for collision
I've been presuming i need to make an object of an oval so i can the .getBounds and see if it intersects
something like this which i found online
                Rectangle r1 = rect1.getBounds();
                Rectangle r2 = rect2.getBounds();
 
                if (r1.intersects(r2))
                        collision = true;
                else
                        collision = false;


but im not quite sure how to go about making an oval object for starters (do i need to make my own class for this?), im sorry if i havent explained it too well as im fairly new with java.
Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Oval .getBounds collison detection

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9029
  • View blog
  • Posts: 33,490
  • Joined: 27-December 08

Re: Oval .getBounds collison detection

Posted 19 June 2012 - 06:46 PM

Take a look at using an Ellipse2D object.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8019
  • View blog
  • Posts: 31,126
  • Joined: 06-March 08

Re: Oval .getBounds collison detection

Posted 20 June 2012 - 08:41 PM

Rectangle r1 = rect1.getBounds();
Rectangle r2 = rect2.getBounds();

if (r1.intersects(r2))
        collision = true;
else
        collision = false;


why not simply ?

collision = rect1.intersects(rect2);



When coding: KISS
Was This Post Helpful? 0
  • +
  • -

#4 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1500
  • View blog
  • Posts: 3,211
  • Joined: 11-December 07

Re: Oval .getBounds collison detection

Posted 21 June 2012 - 06:19 AM

getBounds() will return the smallest rectangle that encompasses the shape so by using the strategy you describe above, you will be treating everything as a rectangle for the purposes of collision detection.

This might be OK for your purposes but if it is not, I would consider a different strategy.

To see if a rectangle is colliding with another, the above strategy is perfect.

If you are testing two circles, see if you can make something work with Pythagoras equations.

The tricky case is testing if a circle is intersecting a rectangle. It can be easily broken down by checking if the circle is intersecting any of the lines (or is wholly contained within the rectangle).
Was This Post Helpful? 0
  • +
  • -

#5 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 993
  • Posts: 2,203
  • Joined: 05-April 11

Re: Oval .getBounds collison detection

Posted 21 June 2012 - 07:47 AM

You can simply calculate the distance between the two circles center and subtract their radius :)
Was This Post Helpful? 1
  • +
  • -

#6 Duta  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 43
  • Joined: 21-June 12

Re: Oval .getBounds collison detection

Posted 21 June 2012 - 09:20 AM

View PostCasiOo, on 21 June 2012 - 07:47 AM, said:

You can simply calculate the distance between the two circles center and subtract their radius :)

That only works if they are both circular - if they are ovals, their radius isn't constant
Was This Post Helpful? 0
  • +
  • -

#7 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1500
  • View blog
  • Posts: 3,211
  • Joined: 11-December 07

Re: Oval .getBounds collison detection

Posted 22 June 2012 - 04:41 AM

That's true but the OP says he is working with circles.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1