Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Collision detection with rotating shapes

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 69884
278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 have always wanted to have a neighbor just like you - Fred Rogers. Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic