flip game character round, but game gets players coords wrong?

Hi I’m new to java gaming… as will become obvious
i’m using an AffineTransform to flip my character around when i turn left, but the game then thinks the character is at different coords than it is being drawn at

i drew a white rectangle around the character, which moves with the character, but only when he is facing right
as soon as i turn him left, the rectangle mirrors about its right hand side, and then follows the character from behind, but when i turn him right again, the box flips back to
cover the character!

notice the box and the x coordinates in the images

http://www.geocities.com/petrucciboy20/1.jpg

http://www.geocities.com/petrucciboy20/2.jpg

here are some relevant parts of the 2 classes involved

if (keyCode == KeyEvent.VK_LEFT)
        {
            if (player.getVelocityX() < 0) return;
            if (player.getVelocityX() == 0) player.setAnimation(runninganim);
            if (player.getVelocityX() > 0) player.setX(player.getX()+player.getWidth());
            player.setVelocityX(-0.1f);
        }

    public void drawTransformed(Graphics2D g)
    {
    	if (!render) return;

		AffineTransform transform = new AffineTransform();
		transform.translate(Math.round(x),Math.round(y));
		if (dx == 0.1f) setScale(1.0f);
		if (dx == -0.1f) setScale(-1.0f);
		transform.scale(scale,1);
		transform.rotate(rotation,getImage().getWidth(null)/2,getImage().getHeight(null)/2);
		// Apply transform to the image and draw it
		g.drawImage(getImage(),transform,null);
    }

thanks in advance for any hints… i know this is a big first question!

first I would suggest using only flags in keyPressed and similar methodes, like only doing:

going_left = true;

then later in game engine doing all other stuff, like:


if (going_left) {
            if (player.getVelocityX() < 0) return;
            if (player.getVelocityX() == 0) player.setAnimation(runninganim);
            if (player.getVelocityX() > 0) player.setX(player.getX()+player.getWidth());
            player.setVelocityX(-0.1f);
}

this way you don’t depend on Swings Event Dispach Thread and all your movement code is at one place, it’s more readable

About your problem… you say that “game thinks your character is at different coords”, you need to find out what is exactly wrong. It could be only 2 things

  1. your calculation where character is located is wrong
  2. your image is wrongly rendered
    So what is it? What you are doing when you press left is actually moving character right and rotating it left, which is kind of strange. You can’t actually conclude anything from 2 screen shots you supplied because it all depends how much you move your character to right… image is moved right and white rectangle is also moved right. Question is which of them was moved correct amount (as we don’t know how much is “player.getWidth()” … but I can guess that image rendering is ok, white rectangle is wrongly calculated)
    Print out position of rectangle and position of image. Move your character for 100 pixels, then again print out their positions and see what was moved wrongly.

thanks for your reply, i guess it’s just hard for me to even explain my problem I’m so new to this!
the rectangle is drawn completely from the player sprite’s x,y coords and width and height, so if the sprite is being drawn correctly, I can’t understand why the box would
be drawn in a different position
here is the code to show that

 g.drawString("Player's x coordinate is: "+player.getX(), 10, 300);
        g.drawRect((int)player.getX(), (int)player.getY(), (int)player.getWidth(), (int)player.getHeight());

When the player is moving right, its X velocity is 0.1f, when left is pushed, that velocity is changed to -0.1f and so begins moving left on the screen, and the scale transform is used to flip the image so the player animation is drawn facing left too. I should say that the movement is actually calculated using a translate transform after the scale is applied and not by manipulating the x and y coordinates directly. developing this game is for an assignment, so i’m stuck using this manner of acheiving it, I just can’t get round this one problem.

thanks very much for all your time

I don’t understand this:

if (player.getVelocityX() > 0) player.setX(player.getX()+player.getWidth());

isn’t this moving the player to the right when you’re pressing left? why?

furthermore I don’t understand how you get from first SS to second SS. Does player turn left and then starts moving left, but white rectangle continues to move right? Or is second SS created after single push to left button? In my first reply, I assumed it was after a single push.

what that line was doing was, if the player is travelling right, then you push left, move the players x coordinate so that when the sprite is flipped (by the AffineTransform) it remains on the same spot the turn began

I have got this problem fixed now by my lecturer, turns out all it was was that when i flip the character, the x coordinate is at the top right instead of the top left

thanks for you help though!