xmlgraphics-batik-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Bibek Sahu <scor...@dodds.net>
Subject Re: detecting shape collision?
Date Wed, 04 Feb 2004 21:12:02 GMT

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


Mime
View raw message