0

So I want my sprite to rotate at my players position, it rotates, but appears some place to the left of the player. I cannot find anything on the internet that helped me with this.

Here's my code

at.setToRotation(Math.PI / 2, Main.p.x, Main.p.y / 2);

if (!rotate) {
        g.setTransform(at);
        rotate = true;
    }

g.drawImage(item0, Main.p.x + 1, Main.p.y - 15, null); 

Yes I know it's not the best code, I'm still a beginner, don't be too harsh please.

tobi6
  • 8,033
  • 6
  • 26
  • 41
Urgaan
  • 23
  • 5
  • SetToRotation rotate the about the anchor points, because your rotate the Graphics context, you need to be sure that the anchor point is the centre point of where the image will be rendered (x + (width / 2), y + (height / 2)). This can get complex fast, so it might be better to generate a rotated version of your sprite and render that – MadProgrammer May 25 '16 at 09:58
  • And how would I go about doing that? Sorry, I'm still a beginner. I now got this: `at.setToRotation(Math.PI / 2, Main.p.x + Main.blockSize / 2, Main.p.y + Main.blockSize / 2);` It seems to work the way I want it to but I don't know if this is stable or if there's a better way to do this. – Urgaan May 25 '16 at 10:22
  • Also one of my other sprites rotates with this sprite, even if I don't want it to, is there a way to get only one sprite to rotate? – Urgaan May 25 '16 at 10:53
  • I don't know about "better", there are different ways. If you need to position/rotate a number of objects, messing with the `Graphics` context gets complicated, as you need to ensure that you're resting the matrix after each update – MadProgrammer May 25 '16 at 10:53
  • 1
    [This example](http://stackoverflow.com/questions/15779877/rotate-bufferedimage-inside-jpanel/15780090#15780090) does both, it modifes the `Graphics` context and paints the image and it also provides and example of rotating the image itself. If you use `Graphics#create` and `Graphics#dispose`, you can make use of multiple translations (each within a create/dispose context), as it won't change the original context – MadProgrammer May 25 '16 at 10:55

1 Answers1

0

drawImage might use the sprites center to set it. Try adding half of the width of the sprite to x.

EDIT

Or maybe the setToRotation function uses the center point of the picture.

tobi6
  • 8,033
  • 6
  • 26
  • 41