4 Replies Latest reply on Feb 6, 2010 6:12 AM by 843853

    Image rotation offsets

      Ok, well im writing a small 2d game in which you purchase tanks and other weapons etc and then they automatically drive round and fire at enemies.

      Now, i need to draw the tank as 2 parts, since the turret can rotate aswell as the body.

      My question is;

      How can i get the correct position for the tank and the turret after they have been rotated?

      My tank base is;

      50x31 pixels.

      My turret is;

      48x21 pixels.

      Now, i calculated the offset for drawing the turret on at 90 degrees, the same direction as the tank, but whenever i rotate the turret im not sure how to get the position back so that it will draw back onto the tank fine.

      Here is my code (the buffered image is larger to acompany the turret rotations);
      appearance = new int[2];
                appearanceX = new int[2];
                appearanceY = new int[2];
                appearance[0] = 4;
                appearance[1] = 5;
                appearanceX[0] = 0;
                appearanceX[1] = 32;
                appearanceY[0] = 0;
                appearanceY[1] = 20;
      public java.awt.image.BufferedImage getAppearance()
                Sprite mainSprite = Sprite.getSprite(appearance[0]);
                int xOffset = mainSprite.getWidth();
                int yOffset = mainSprite.getHeight();
                java.awt.image.BufferedImage body = new java.awt.image.BufferedImage(xOffset * 2, yOffset * 2, java.awt.image.BufferedImage.TYPE_INT_ARGB);
                java.awt.Graphics2D bodyGraphics = body.createGraphics();
                bodyGraphics.drawImage(Sprite.getSprite(appearance[0]).toImage(), xOffset + appearanceX[0] - (Sprite.getSprite(appearance[0]).getWidth() / 2), yOffset + appearanceY[0] - (Sprite.getSprite(appearance[0]).getHeight() / 2), null);
                java.awt.geom.AffineTransform affineTransform = new java.awt.geom.AffineTransform();      
                affineTransform.translate((double) appearanceX[1], (double) appearanceY[1]);
                affineTransform.rotate(Math.toRadians(rotation), 8, 8);
                bodyGraphics.drawImage(Sprite.getSprite(appearance[1]).toImage(), affineTransform, null);
                return body;
      Could anybody help out or point me in the right direction about getting the correct offsets for when the turret image is rotated?

      You can see the sprites here;


      Thanks for any help.
        • 1. Re: Image rotation offsets
          It seems to me that you need to figure out the distance in pixels from the middle of the tank body image to where the turret should be in that image (in the center of that circular area) which we will call D. Then, when you rotate, you just find the offset away from where the middle of the tank image is located after being rotated and translated by using something like turret_x_offset_from_body = D*cos(Math.toRadians(rotation)) and turret_y_offset_from_body = D*sin(Math.toRadians(rotation)) and add that to the current location of the tank base image though it will depend on where you are measuring your angle from if you need to add minus signs or swap the cos and sin.

          Then you also need to add in for the offset of the turret base within the turret picture. This can be done somewhat similarly to what I described above by finding the distance from the middle of the turret image to the center of the base of the turret and call it D2. Then you do a similar thing to that above to find the other offsets but using D2 and a different angle of rotation for the turret. Then you just take the position of the tank base and add the offsets you just calculated to get the position of the turret image. It will probably take some tinkering to get this to work out just right.

          Edited by: notmuchtotell on Feb 5, 2010 10:38 AM
          • 2. Re: Image rotation offsets
            I tried what you just said but the turret seems to drift back and forward in like a figure of eight.
            • 3. Re: Image rotation offsets
              It might be that you aren't using the right coordinates for the center of the images and for the center of the base of the turret/place on the tank where the turret base goes. For instance, if you have a 3x4 image with 0 indexing for both x and y, the center is at x=2/2=1 (2 is the greatest index in x), and y=3/2=1.5. You would need to use the .5 in there I would think. In other words, you need to get all of these middle points exactly down or it will drift around like that I think.
              • 4. Re: Image rotation offsets
                Also, make when you get the final coordinates that you round them rather than just converting them to ints.