5 Replies Latest reply on Mar 11, 2010 8:11 AM by 795507

    BufferedImage Rotation

    795507
      I'm currently writing a top-down tank shooter for a class project. For the project, I'm using a tiled system with two layers, one for the land tiles, one for the special tiles(starting positions, weapons, power-ups, etc.)

      I'm trying to rotate the tank around its center point(eventually the tank body and the turret will rotate independently, but if I figure out this step, I can get that later), but I'm having a lot of trouble getting the rotation to work. I've tried rotating via Graphics2D.rotate:
      public void turnLeft()
      {
           angle += 0.1;
           while (angle > 360)
           {
                angle -= 360;
           }
           
           buffer.rotate(Math.PI / 180, sprite.getWidth() / 2, sprite[0].getHeight() / 2);
           buffer.drawImage(sprite, 0, 0, null);
           velocity = new SpeedVector(Math.cos(angle) * 5, -Math.sin(angle) * 5);
      }
      That just smears the tank's pixels around in a circular fashion.

      On the other hand, the AffineTransform method:
      public void turnLeft()
      {
           angle += 0.1;
           while (angle > 360)
           {
                angle -= 360;
           }
           
           AffineTransform transform = new AffineTransform();
           transform.rotate(Math.PI / 180, sprite.getWidth() / 2, sprite.getHeight() / 2);
           
           AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BILINEAR);
           sprite = op.filter(sprite, null);
           velocity = new SpeedVector(Math.cos(angle) * 5, -Math.sin(angle) * 5);
      }
      This blurs the crap out of the tank, as well as moves it around in a fashion not befitting of a rotating but otherwise stationary object.

      Is there a different way to rotate the tank, such as rotating the Graphics object itself, rather than the pixels inside it? Or maybe am I doing this wrong?
        • 1. Re: BufferedImage Rotation
          gimbal2
          It seems like you are going out of the realm of pure Java2D with your game, and into the realm of doing a 2D game on a 3D API. Check out the Slick framework, it is built to do exactly what you want.

          [http://slick.cokeandcode.com/|http://slick.cokeandcode.com/]
          • 2. Re: BufferedImage Rotation
            796262
            I think you're using the Graphics2D.rotate() method in the wrong way. You don't want to just rotate the Graphics Object (why are you referencing it outside a painting method anyway?). You want to update a rotation variable, and then use that in your paintComponent() method.

            I found a bunch of tutorials just by googling "java graphics2d rotate". I'm sure one of them will suit your needs.
            • 3. Re: BufferedImage Rotation
              795507
              kevinaworkman wrote:
              I think you're using the Graphics2D.rotate() method in the wrong way. You don't want to just rotate the Graphics Object (why are you referencing it outside a painting method anyway?). You want to update a rotation variable, and then use that in your paintComponent() method.

              I found a bunch of tutorials just by googling "java graphics2d rotate". I'm sure one of them will suit your needs.
              I think I didn't quite understand how the rotate method worked. I moved the rotate into the tank's draw method, which is called by the program's paintComponent method:
              public void draw(Graphics2D g2d)
              {
                   g2d.setColor(Color.white);
                   
                   g2d.rotate(angle);
                   g2d.drawImage(sprite, position.getX(), position.getY(), null);
                   g2d.drawString(name, position.getX(), position.getY());
              }
              Now, the tank is rotating around the origin of the window, so I changed it to show:
              public void draw(Graphics2D g2d)
              {
                   g2d.setColor(Color.white);
                   
                   g2d.rotate(angle, position.getX() + (int)(sprite.getWidth() / 2), position.getY() + (int)(sprite.getHeight() / 2));
                   g2d.drawImage(sprite, position.getX(), position.getY(), null);
                   g2d.drawString(name, position.getX(), position.getY());
              }
              So now the rotating of the tile works, but the converting from the angle to the new velocity is screwed up (see first post code for my velocity computations - just minus the rotating code).
              • 4. Re: BufferedImage Rotation
                795507
                gimbal2 wrote:
                It seems like you are going out of the realm of pure Java2D with your game, and into the realm of doing a 2D game on a 3D API. Check out the Slick framework, it is built to do exactly what you want.

                [http://slick.cokeandcode.com/|http://slick.cokeandcode.com/]
                That library looks sweet, and I will probably experiment with it later, but for this one I kind of want to code the whole thing myself.
                • 5. Re: BufferedImage Rotation
                  795507
                  Alright, I got it to work. Thanks all for the answers.