aspose file tools
The moose likes Java in General and the fly likes Collision detection with rotating shapes Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply 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: 1409

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: 32708
    
    4
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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Collision detection with rotating shapes
 
Similar Threads
Change text orientation on canvas
Drawing a selection box using swing
Moving and resizing a rectangle around
Wanted: Java2D example of zooming and panning with scroll bars
AffineTransform - please help me out