1

i am working in a game like Geometry dash on java , i got all of the mechanics like , wave , ball , ufo , ship.... anyway i am trying to achieve the effect of the square rotating when it jumps like the original game y try to do this using affineTransform but it doesnt work like the original game i use this code to rotate , but looks weird

public void rotate(){
tx.rotate(Math.toDegrees(degrees),width/2,width/2);
     op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);

}

here is a capture of my game looking right now

here is a link of geometry dash were you can see how the square rotate in middle air(what i am trying to do)

got any ideas? please help me :c

Jesse
  • 75
  • 3
  • 1
    [This example](http://stackoverflow.com/questions/20275424/rotating-image-with-affinetransform/20280225#20280225) shows one possible way to rotate an image using `AffineTransform`, it generates a new image whose size is large enough to accomidate the rotate image. [This example](http://stackoverflow.com/questions/15779877/rotate-bufferedimage-inside-jpanel/15780090#15780090) rotates an image within the `Graphics` context using an `AffineTransform` – MadProgrammer Jan 30 '16 at 04:55
  • Please `edit` your question and provide an [MVCE](https://stackoverflow.com/help/mcve) (a **Minimal**, **Complete**, and **Verifiable** example) that clearly illustrates the problem. You added a link to an image but you don't say what part is wrong with it... Me personally I don't see anything wrong with it. Be as clear as possible. – TT. Jan 30 '16 at 09:38
  • i think my question is really clear , why you cant understand? o.o – Jesse Feb 07 '16 at 07:23

1 Answers1

0

hmmm, I would actually use an AffineTransform but in another way... if you have the two functions

public void tick()
{

}
public void render(Graphics g)
{

}

I would do something like:

AffineTransform at;
rotation = 0;
public Player()
{
  at = AffineTransform.getTranslateInstance(x, y);
}
public void tick()
{
  at = AffineTransform.getTranslateInstance(x, y);
  if(isInAir)
  {
    rotation++;
    at.rotate(Math.toRadians(rotation), width / 2, height / 2);
  }
  else
  {
    rotation = 0;
  }
}
public void render(Graphics g)
{
  Graphics2D g2d = (Graphics2D) g;
  g2d.drawImage(sprite, at, null);
}

I know it's not that practical, always creating another AffineTransform but it worked for me. Another way to do it in the render method, not using any AffineTransform would be:

Graphics2D g2d = (Graphics2D) g;
g2d.rotate(Math.toRadians(rotation));
g.drawImage(sprite, x, y, null);
g2d.rotate(Math.toRadians(-rotation));

I'm not quite sure if you would need Math.toRadians here, but if not just delete it. Hope I could help!

Cactus Coder
  • 63
  • 1
  • 5