Post Reply  Post Thread 
Sprite rotating?
Author Message
asherwolf
Newbie
*


Posts: 3
Group: Registered
Joined: Jun 2010
Status: Offline
Reputation: 0
Last seen online:
16-07-2010, 03:13 AM
Post: #1
Sprite rotating?

I was thinking of making some modifications to the sprite object to see if I could rotate it around on the screen.

Problem is, the only effective rotation methods I've found in my Java research relies on the rotation method provided by the Graphics2D object you can obtain from a BufferedImage:

BufferedImage dimg = new BufferedImage(w, h, img.getType());
Graphics2D g = dimg.createGraphics();
g.rotate(Math.toRadians(angle), w/2, h/2);
g.drawImage(img, null, 0, 0);

Unfortunately there's only one Graphics2D in the game, obtained at the beginning. I don't quite understand how the Graphics2D object works to be honest. Am I locked in to one view, and one Graphics2D object?

Any help would be appreciated.

23-06-2010 12:10 PM
Find all posts by this user Quote this message in a reply
p132p132
Newbie
*


Posts: 30
Group: Registered
Joined: Jan 2010
Status: Offline
Reputation: 0
Last seen online:
Yesterday, 08:53 AM
Post: #2
RE: Sprite rotating?

In my understanding, an application can own more than one Graphics2D objects, which have different duties during runtime. The one created in the beginning is the back buffer of the game engine, and you can create another one from a BufferedImage instance to manipulate the image. They have similar methods, for they're extended from the same abstract class, but do modification in different objects (the ones which create them).

23-06-2010 05:43 PM
Find all posts by this user Quote this message in a reply
asherwolf
Newbie
*


Posts: 3
Group: Registered
Joined: Jun 2010
Status: Offline
Reputation: 0
Last seen online:
16-07-2010, 03:13 AM
Post: #3
RE: Sprite rotating?

I think I'm understanding better now. I can rotate a sprite, as long as I have the clipping right, by drawing on to its own Graphics2D object [ getGraphics() ]. This alters the BufferedImage which can then be drawn on to the backbuffer Graphics2D object.

If that sounds correct than I'll be on my way Smile

23-06-2010 10:34 PM
Find all posts by this user Quote this message in a reply
MetroidFan2002
Senior Member
****


Posts: 611
Group: Registered
Joined: Jul 2007
Status: Offline
Reputation: 0
Last seen online:
06-09-2010, 11:25 AM
Post: #4
RE: Sprite rotating?

ImageUtil.rotate(image, angle) should give you what you need, if you don't need portions of an angle.

However, keep in mind that images are raster graphics, not vector. Therefore, it's advised to keep your source image, and just calculate what angle you'll need when rotating. Then your rotates won't suffer problems when rotating a rotated image - these problems crop up from raster graphics, and wouldn't occur if it was vector-based.

Here is the code if you're interested:

Code:
public static BufferedImage rotate(BufferedImage src, int angle) {
         int w = src.getWidth(), h = src.getHeight(), transparency = src
                        .getColorModel().getTransparency();
                BufferedImage image = ImageUtil.createImage(w, h, transparency);
                
                Graphics2D g = image.createGraphics();
                g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                        RenderingHints.VALUE_INTERPOLATION_BILINEAR);
                g.rotate(Math.toRadians(angle), w / 2, h / 2);
                g.drawImage(src, 0, 0, null);
                g.dispose();
                
                return image;
        }

24-06-2010 08:08 AM
Find all posts by this user Quote this message in a reply
asherwolf
Newbie
*


Posts: 3
Group: Registered
Joined: Jun 2010
Status: Offline
Reputation: 0
Last seen online:
16-07-2010, 03:13 AM
Post: #5
RE: Sprite rotating?

Thanks for the info MetroidFan2002. Now I just have to work with all the other things rotating can change in a sprite. But for now this part is figured out.

24-06-2010 09:43 AM
Find all posts by this user Quote this message in a reply
Post Reply  Post Thread 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Sprite sets and tile sets anywhere? St3v3 2 1,878 16-08-2010 10:06 AM
Last Post: hongchaguan
  PNG rendering and basic sprite animation Malkavian 5 737 04-08-2010 05:34 PM
Last Post: thewall
  Sprite animation Kell 2 396 30-07-2010 06:29 PM
Last Post: jwright
  Collisions with a non-rectangular sprite thewall 1 229 25-07-2010 08:03 PM
Last Post: MetroidFan2002
Question Keep a sprite inside it's background. gooseisloose 3 452 12-06-2010 03:10 AM
Last Post: gooseisloose

View a Printable Version
Send this Thread to a Friend
Subscribe to this Thread | Add Thread to Favorites

Forum Jump: