Search code examples
javaaffinetransform

How to use AffineTransform and also a cordinate based system in Java?


I have been working on a game in Java, that has rotational based movement (meaning you rotate to turn, and can only move forwards and backwards in the direction you are rotated towards). I have it almost working, but ran into a problem at the very end. In order to use AffineTransform, you use g2d.drawImage([image], [name of AffineTransform], null); This does not allow for the input of coordinates of where to place my character, meaning that I cannot move my character on the screen, only rotate it.

How can I transform my player image (to rotate it), AND then place it correctly on my screen through the xPos and yPos variables I have set on my screen?

If it is any help, here is my code.

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;

public class Player 
{
    imageLoader loader = new imageLoader();
    private BufferedImage player = loader.loadImage("/player.png");
    private BufferedImage rotater = loader.loadImage("/player.png");
    AffineTransform transform = new AffineTransform();

    public int xPos = 200;
    public int yPos = 200;
    public double degrees = 90.0;

    public Input input;

    public void render(Graphics g)  //SOMEWHERE IN THIS RENDER METHOD I HAVE TO ROTATE AND DRAW THE PLAYER TO THE SCREEN.
    {
        transform.setToIdentity();
        transform.translate(player.getWidth() / 2, player.getHeight() / 2);
        transform.rotate(Math.toRadians(degrees));
        transform.translate(-player.getWidth()/2, -player.getHeight()/2);

        Graphics2D g2d = (Graphics2D) g;
        g2d.drawImage(player, transform, null);

        System.out.println("PLAYER_X = "+xPos+" | " + "PLAYER_Y " + yPos + " | " + "ROTATION " + degrees);
    }

    public void moveForward()
    {
        xPos += (int) (Math.sin(degrees * (Math.PI/180)) * 4);
        yPos += (int) (Math.cos(degrees * (Math.PI/180)) * -4);
    }
    public void moveBackward()
    {
        xPos -= (int) (Math.sin(degrees * (Math.PI/180)) * 4);
        yPos -= (int) (Math.cos(degrees * (Math.PI/180)) * -4);
    }
    public void rotateLeft()
    {
        degrees = degrees - 4;
    }
    public void rotateRight()
    {
        degrees = degrees + 4;
    }
}

Thank you so much, I hope one of you can help me.

Ryan Corkery


Solution

  • You already have the code to rotate and translate (draw at a certain position) in your method. Your original code was already drawing the image at coordinate -width/2, -height/2 (because your image is rotate about its center, not about it's top-left corner)

    So, it's a matter of adding the extra translation that you need to draw it at xPos, yPos:

        transform.translate(-xPos + player.getWidth() / 2, -yPos + player.getHeight() / 2);
        transform.rotate(Math.toRadians(degrees));
        transform.translate(xPos - player.getWidth()/2, yPos - player.getHeight()/2);