Java-Gaming.org Hi !
 Featured games (83) games approved by the League of Dukes Games in Showcase (581) Games in Android Showcase (163) games submitted by our members Games in WIP (632) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Fastest way to see if a shape is fully contained inside another shape  (Read 1687 times) 0 Members and 1 Guest are viewing this topic.
Sanzeinga

Senior Newbie

 « Posted 2009-04-08 22:02:32 »

(im using slick)
Im considering using a circular boundary for the playing field, as such im wondering if there is a way to check if a shape is fully contained within another shape.
1. But before that, is there a better way to keep shapes(hitboxes) within a circular boundary?
2. Is there a better method than taking the 4 corners of a rectangle and seeing if theyre all within a gigantic circle that is the playing field.  This is my current idea but it involves having to make a rectangular "bounding hitbox" for every entity (probably bigger than the sprite ), including things that otherwise have circular hitboxes.
appel

JGO Wizard

Medals: 69
Projects: 4

I always win!

 « Reply #1 - Posted 2009-04-08 22:22:15 »

I believe that if you have a:

hugeCircle

and objectShape within that hugeCircle, and assuming all objectShapes start within that hugeCircle, you could do:

 1  2  3  4  5 `if(hugeCircle.intersects(objectShape)) {  // objectShape is going out of hugeCircle} else {  // objectShape is still within hugeCircle}`

I believe Shape.intersects(Shape) does return false if a shape is fully within another shape. Only when the lines of the shapes intersect does it return true.

Check out the 4K competition @ www.java4k.com
DzzD
 « Reply #2 - Posted 2009-04-08 22:32:57 »

if you try to test if a shape is within a circle ? if so just verify the following

for all point p

if true for all point the shape made by all p point is inside circle with the center cx,cy

but not sure it is what you want ?

Sanzeinga

Senior Newbie

 « Reply #3 - Posted 2009-04-08 22:36:28 »

appel is that true?  Wouldn't that mean a very fast moving small object ( a bullet with a tiny hitbox ) might move past the boundary in one instant and never "collide"
dzzd how does that work for a circle within a circle
DzzD
 « Reply #4 - Posted 2009-04-08 22:48:25 »

if you have c1 and c2 with their respective radius r1 & r2

you just have to compute the distance between the two center (and compare to a circle centered on c2 with a smaller radius depending on the c1 radius r1*0.5)

if
(c1.x-c2.x)²+(c1.y-c2.y)²<(r2-r1*0.5)²
then
c1 is inside c2

how it works => it compute the distance between the two center c1 & c2 and see if it is greater then a circle with radius r2-r1/2 centered on c2

ps: verify this a little cause I may have do errors on the formula...

appel

JGO Wizard

Medals: 69
Projects: 4

I always win!

 « Reply #5 - Posted 2009-04-08 22:57:32 »

appel is that true?  Wouldn't that mean a very fast moving small object ( a bullet with a tiny hitbox ) might move past the boundary in one instant and never "collide"
dzzd how does that work for a circle within a circle

You could fix (somewhat) with AppGameContainer.setMaximumLogicUpdateInterval(int).

However, if the game bullet of size 10x10 moves super-fast, say 400 pixels per 1 delta, then it's not possible to detect collision this way. You'll need to find another way. One would be to check if the line composing of previous location and new location intersect any of the lines of the target shape.

Check out the 4K competition @ www.java4k.com