Lost with affine transform...

I am brand new to java game programming (started learning about a week ago). So far, I’ve taken someone else’s Space Invader tutorial and started with a skeleton and modified a few things. Right now what I have is a blank screen with the ship in the center. Aliens spawn randomly and move in a random direction (supposed to be toward the player, but that bug is for another day). I’ve figured out how to take mouse clicks and turn it into a shot in the direction of the mouse click, but the shots that come out are always pointing up. I want to apply AffineTransform to rotate the shot on the angle that its traveling. That is where I get lost. Between the AffineTranform API and any sort of tutorial, I can’t figure out how or where to put any of it.

here’s what I have so far:

package shooter;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.geom.AffineTransform;

public class Sprite {
	
	//create the tranform and image used for the sprite
	private AffineTransform transform = new AffineTransform();
	private Image image;
	private double angle;
	
	//creates the sprite with a scale and angle parameter
	public Sprite (Image image, double scale, double angle){
		this.angle = angle;
		//uses the scale to make the sprite the appropriate size
		this.image = image.getScaledInstance((int)(image.getWidth(null) * scale), (int)(image.getHeight(null) * scale), Image.SCALE_DEFAULT);
	}
	
	public int getWidth() {
		return image.getWidth(null);
	}
	
	public int getHeight() {
		return image.getHeight(null);
	}	
	
	public void draw(Graphics g, int x, int y) {
		//not sure if this is how you're supposed to do this, 
		//but I'm trying to move the sprite to the passed x and y
		//then rotate it
		transform.translate(x, y);
		transform.getRotateInstance(Math.toRadians(angle), image.getWidth(null)/2, image.getHeight(null)/2);
		g.drawImage(image, x, y, null);
	}
}

the transform is never actually applied to the image how i have it here because affineTranform doesn’t have any way to pass and image to it… I’m so lost… ???

This is just the sprite part of another class called Entity which is used to create the player, aliens, and shots. Hopefully this makes enough sense that someone can help me. If you do need anything else, let me know because I want to figure this out…

Yup this is how to use AffineTransform for rotation

g2d.setTransform(AffineTransform.getRotateInstance(rotation.x, loc.x, loc.y));

Then just draw the image like normal.

Don’t have it so each sprite as an AffineTransform as you really don’t need it.

I should add this to my java2D guides.

Look at that for other tips on using java2D.

So, I got rid of the instanced AffineTransform and changed the draw method to:

public void draw(Graphics2D g, int x, int y) {
		g.setTransform(AffineTransform.getRotateInstance(Math.toRadians(angle), x, y));
		g.drawImage(image, x, y, null);
	}

it still doesn’t work, could you please put that into context for me? Where exactly does that line need to go? I tried putting it in the game loop right before the entity is drawn and nothing works. Is there anything else that needs to be changed?

It’s not that clear, but
g2d.setTransform(AffineTransform)
should almost never be used, it overwrites the current transform completely.

Use
g2d.transform(AffineTransform)
instead, as that will multiply the current transformation matrix with the specified one.

You can read the javadocs for more information on how both methods behave.

Update: the affine transform was working, it was an issue with my code that always passed 0 degrees to the transform, so it looked like it wasn’t doing anything. You’re explanation was plenty clear, I just messed up some other code.

And I will change that line so I don’t mess with the default transform.

Thanks everyone!

My bad Riven I forgot that.

This is what I do when drawing stuff.

Graphics2D g2d = (Graphics2D)g.create();
        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, (float) (fade)));
        g2d.setTransform(AffineTransform.getRotateInstance(rotation.x, loc.x, loc.y));
		if(image != null)
		{
			if(alive)
			{
				int x1 = (int) (loc.x - (size/2)); 
				int y1 = (int) (loc.y - (size/2));
				g2d.drawImage(image, x1, y1, (int)size, (int)size, null);
			}
		}
		else if(alive)
		{
			int x1 = (int)size >> 1;
            int y1 = (int)size >> 1;
			g2d.setColor(color);
			g2d.fillRect((int)(loc.x - x1), (int)(loc.y - y1), (int)size, (int)size);
		}
		g2d.dispose();

Important part is the g.create();

I do not know if this is a memory leak or if it is slower but in testing it makes no difference for me.