Author Topic: Picture Rotating  (Read 2778 times)

0 Members and 1 Guest are viewing this topic.

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Picture Rotating
« on: September 12, 2011, 06:14:18 am »
This is a general programming question. I chose to put it here, because I'm using Java.

I need to display an image, and that image can rotate with the mouse. Basically, it's like Builderboy's game Nightmare, where the player has an orientation and he is always looking at the location of the mouse.

I'm not asking for Java code, I'm asking about how to make it.

Code: [Select]
int orientation;
int x;
int y;

These are the three main variables of the player object. My problem is how to display an image with a certain rotation.

If the player were a rectangle, I would display it like this:

Code: [Select]
/* g is the graphics component */
g.fillRect(x, y, 20, 20);

Now, how can I rotate the rectangle? My only obvious answer is using trigonometry, it's not very hard, but I'm wondering if there are any other ways. Thanks a lot!
« Last Edit: September 12, 2011, 06:14:25 am by ephan »

Offline calc84maniac

  • eZ80 Guru
  • Coder Of Tomorrow
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2912
  • Rating: +471/-17
    • View Profile
    • TI-Boy CE
Re: Picture Rotating
« Reply #1 on: September 12, 2011, 12:12:30 pm »
Well, I think you'd want to use the fillPolygon method. For getting the location of the vertices, I'm pretty sure you'll need trigonometry. If you're always rotating it by a constant angle though, you could keep a vector that points from the center of the square to one of the vertices and then rotate that vector using a precalculated sine/cosine of your rotation angle.

Edit: Oh wait, you're displaying an image, not a rectangle. Hmm...
« Last Edit: September 12, 2011, 12:13:27 pm by calc84maniac »
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman

Offline JL235

  • LV3 Member (Next: 100)
  • ***
  • Posts: 57
  • Rating: +13/-0
    • View Profile
    • Play My Code
Re: Picture Rotating
« Reply #2 on: September 12, 2011, 02:57:49 pm »
I am presuming you are using Java2D for this (the default graphics library which comes with Java). For this when rotating an image, no, you don't need to know trigonometry. Instead you can use the AffineTransform class in the Java API. You create one, set the rotation, and then pass it in to the Graphics2D when you draw an image. There is a tutorial on this here.

Java2D can do rotations in real time, but it's pretty slow. At most, your talking 10s or 100s of rotations, but it depends on the size of the image (and a bunch of other factors, such as JVM version, start up flags, and your OS). You can speed this up by generating a rotated image, and storing it, but this in turn creates a new set of issues. Creating lots of images requires lots of memory, which you can quickly run out of. To fix this you can use HashMaps and WeakReferences to allow you to create as many images as you want, which are automatically removed if you run out of memory. If they are removed then you just remake the image.

For these reasons, performance and memory, is why most Java game developers use hardware accelerated libraries instead. For 2D graphics these include Slick, LWJGL or JOGL. With them you can trivially draw 10,000s to 10 millions of rotated images, at 60 frames per second. From the sounds of your post, you are mostly interested in getting underneath the bonnet, building engines, and working this stuff out. These libraries will hold you hand less often, especially LWJGL and JOGL which are just wrappers for OpenGL. For them you would need to know either how matrix operations work, trigonometry, or both, to do things like rotating an image.

One final thing to note is that with your draw rectangle example, x and y refer to the top left corner. When rotating, you need to work from the centre. This is because a rotation needs a centre of origin, the point it is being rotated around, which will be the centre of the image.
Build games in the browser at PlayMyCode.com, @playmycode

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Picture Rotating
« Reply #3 on: September 13, 2011, 02:19:12 pm »
AffineTransform, that's just what I need. Thanks! Also +1 for the detailed answer JL235.

Offline JL235

  • LV3 Member (Next: 100)
  • ***
  • Posts: 57
  • Rating: +13/-0
    • View Profile
    • Play My Code
Re: Picture Rotating
« Reply #4 on: September 13, 2011, 06:04:53 pm »
AffineTransform, that's just what I need. Thanks! Also +1 for the detailed answer JL235.
Not a problem. I've used both Java2D and JOGL quite heavily in the past and have a lot of experience with them. Java2D is a very sophisticated graphics library, but these days I personally think it's quite dated. For drawing custom GUI elements, and especially for games, I think there are lots of better alternatives out there. But it ultimately depends on what you do with it.

Just to throw it out there as something you might want to play with, Oracle are currently developing JavaFX 2. It's far better then JavaFX 1, and it's hardware accelerated, but it's only out in beta.
Build games in the browser at PlayMyCode.com, @playmycode