[quote]Hello
I’m trying to make a 2D space game where each object is built up by an Image.
The problem is that I want the objects to be able to rotate. So I’ll need some way of rotating the object images.
I have understood that since Graphics2D.rotate() is really slow I’ll need to pregenerate the rotated version of the object image.
[/quote]
You can also pre-generate your rotated images within your app. Doing this makes your app more compact (we only need 1 sprite as compared to having dozens of pre-rotated sprites) and lets you easily adjust the rotation stepsize.
You can use AffineTransform ( ie, Graphics2D.rotate() ) to generate your rotations. But take care that your final rotated Image has a large enough size so that your sprite pic doesn’t get clipped.
Well, depending on the size and shape of your actual sprite pic, the bounding box of the sprite should change after each rotation.
For example, imagine a missile sprite positioned horizontally (ie, the tip of the missile facing east). The bounding box for such a sprite would ideally be the smallest rectangle (lying flat on it’s side) encompassing the sprite.
But when the missile is rotated 45 degrees counter-clockwise, facing northeast, how should the bounding box change? You basically have 2 choices:
-
Recalculate the bounding box to be the smallest rectangle (lying flat on it’s side) encompassing the rotated sprite. This is known as an “axis-aligned” bounding box and can be represented by a Rectangle object. Collision detection using such a bounding box is very efficient and is easy to implement, but is not so accurate.
-
Simply rotate the bounding box 45 degrees counter-clockwise as well, so that the bounding box is also facing northeast. This “object-aligned” bounding box can be represented by a Polygon object (since Rectangle objects must be aligned with each axis), and has the benefit of being more accurate and probably easier to implement, but not very efficient to compare collisions with.