xmlgraphics-batik-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From don undeen <donund...@yahoo.com>
Subject Re: detecting shape collision?
Date Wed, 04 Feb 2004 21:38:02 GMT
So you're saying that while there isn't code in batik to see if to SVG shapes intersect, I
can turn them into java.awt.Shapes, and use code like jPong.PongObject.collides(shape1, shape2)
to see if they collide?

that makes sense; I wonder if that will ulitimately be practical for my system, where I intend
to to have LOTs of shapes moving around. For N objects, I have to do (n+(n-1)+(n-2)+...+2+1
(i forget, is that what they'd call "O(log(n))" ?) comparisons every iteration to see if there
are any new intersections. I've been able trying to avoid loops like that where possible.
What I really wanted to have was a way to register a "collisionEvent" with a shape, so that
collisions are just "announced" when they occur. Would something like that ever be in the
works for SVG?


Bibek Sahu <scorpio@dodds.net> wrote:

Howdy,

On Wed, 4 Feb 2004, don undeen wrote:

> Is there any way with batik/java/svg to detect if two svg shapes have
> "collided" or overlap? For example, if you have two circles of some
> radius, and you move them closer together, I'd like an alert to go up
> when the edges collide. Or perhaps I'd like to know if two lines
> overlap.

In principle, in SVG, the SVGSVGElement methods 'getEnclosureList()',
'getIntersectionList()', 'checkEnclosure()', and 'checkIntersection()' might
be useful (though I don't know if they'd be enough)... but in practice I
don't think they're implemented in Batik yet (at least, that the impression
I got from what I've seen on this list).

In practice with Batik, you can get the GraphicsNode associated with an
SVGElement, and then use GraphicsNode.getOutline() (or if it's a
ShapeNode, use ShapeNode.getShape()) for both nodes, then check if the
returned Shape objects intersect:

BridgeContext bc = svgComponent.getUpdateManager().getBridgeContext();

SVGElement circle1=...., circle2=....;
GraphicsNode g1 = bc.getGraphicsNode(circle1);
GraphicsNode g2 = bc.getGraphicsNode(circle2);

Shape outline1 = g1.getGlobalTransform().createTransformedShape( g1.getOutline() );
Shape outline2 = g2.getGlobalTransform().createTransformedShape( g2.getOutline() );

if(outline1.intersects(outline2.getBounds2D()))
// the first shape intersects the bounding-box of the second
else
// blah blah blah


Note that it doesn't actually check if the two /shapes/ intersect, and
especially with circles this could get to be a problem, but at least you now
have Shape objects to work with. :-)


If you need a method to see if two Shapes collide, Mr. Google is your
friend. (e.g., http://www.risrani.com/programs/pong/doc-Pong/jPong/PongObject.htmlhttp://www.risrani.com/programs/pong/doc-Pong/jPong/PongObject.html
;-)

Good luck!
- Bibek


> Is there a method to do this that doesn't involve explicitly calculating
> the areas covered by each of my shapes?
>
> I'm guessing the answer is "no," but I'd sure like it to be "yes"!
>
>
>
> thanks for everything,
>
> Don Undeen
>
>
>
>
> ---------------------------------
> Do you Yahoo!?
> Yahoo! SiteBuilder - Free web site building tool. Try it!


---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org


---------------------------------
Do you Yahoo!?
Yahoo! SiteBuilder - Free web site building tool. Try it!
Mime
View raw message