Meaningless Drivel is fun!*
The moose likes Java in General and the fly likes Collision detection with rotating shapes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Collision detection with rotating shapes" Watch "Collision detection with rotating shapes" New topic
Author

Collision detection with rotating shapes

Kalle Zander
Greenhorn

Joined: Nov 16, 2010
Posts: 28


I'm trying to perform collision detection with shapes that rotates on the screen using the Graphics2D rotation. The problem is that I need to rotate the sprite.getBounds() Rectangle in some way after the graphics rotation so it fits well so I later on can use the intersect method with decent accuracy. As the above code is now, the sprite.getBounds() Rectangle follows the sprite in x and y, but doesn't rotate with it as can be seen in the images below. Is there some method to rotate the actual sprite.getBounds() Rectangle's coordinates, or how can this problem be approached in best way?



//Kalle


Code first, then think.
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1753
    
    7

You could pair the sprite with a java.awt.geom.Area, rotate the sprite using an AffineTransform and pass that AffineTransform instance to the Area.transform() method. You can then use Area.getBounds() / getBounds2D() to obtain the post-rotation coördinates for your sprite.

Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37907
    
  22
If you use AffineTransforms, apply them to a cloned copy of the Graphics object, obtained with a method of the Graphics class, I think called create().
Kalle Zander
Greenhorn

Joined: Nov 16, 2010
Posts: 28
Jelle Klap wrote:You could pair the sprite with a java.awt.geom.Area, rotate the sprite using an AffineTransform and pass that AffineTransform instance to the Area.transform() method. You can then use Area.getBounds() / getBounds2D() to obtain the post-rotation coördinates for your sprite.


If this was what you meant, it worked fine! Thank you.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Collision detection with rotating shapes
 
Similar Threads
Change text orientation on canvas
Wanted: Java2D example of zooming and panning with scroll bars
Moving and resizing a rectangle around
Drawing a selection box using swing
AffineTransform - please help me out