Sign in to follow this  
acidwillburnyou

[java] Sprite Rotation using Java2D

Recommended Posts

I'm trying to rotate a sprite in my game. The problem is, the entire buffer rotates (including the fps string). Not only this, but the rotation isn't happening from the center of the sprite, but rather from the top left of the screen. I'm using the Graphics2D class in the java api. Here's the relevant code snippets: method that's called, eg: mech.rotate(-1, 45);
	// direction is either 1 (cc) or -1 (c)
	// angle is in deg
	public void rotate(int direction, double deg) {
		AffineTransform transform = new AffineTransform();
		angleInRad = Math.toRadians(direction * deg);

		//transform.setToTranslation(x, y);
		transform.rotate(angleInRad * direction);
		super.rotate(transform);

	}

super class's rotate method (it has reference to the sprite Image variable):
	public void rotate(AffineTransform transform) {
		g2d.drawImage(image, transform, null);
		System.out.println("rotate: " + transform);
	}

Thanks, Karan

Share this post


Link to post
Share on other sites
use
transform.rotate(angle,x,y);

where angle is yo angle and (x,y) is coordinate of the pivot.
For the entire buffer rotating, that buffer isnt called "image" in yo code is it?

Share this post


Link to post
Share on other sites
Hmm, still no cigar. I've consolidated the two methods into one:


public void rotate(int direction, double deg, Graphics2D g2d) {
AffineTransform transform = new AffineTransform();
angleInRad = Math.toRadians(direction * deg);

//transform.setToTranslation(x, y);
int hwidth = super.image.getWidth(null) / 2;
int hheight = super.image.getWidth(null) / 2;
transform.rotate(angleInRad * direction, currentX + hwidth, currentY - hheight);
g2d.drawImage(super.image, transform, null);
}



The values of hheight/width and currentX/Y are correct. I'm wondering why it's still not working.

Thanks in advance.

Share this post


Link to post
Share on other sites
the x and y in rotate(angle,x,y) are relative to the image origin (0,0) not to the absolute origin.
So if u have a 50x40 sprite and you want to draw it rotated around it's center by 30 degrees at position 300,400 u do this:

void paint(Graphics g){
...
Graphics2D g2d=(Graphics2D)g;
AffineTransform at1=new AffineTransform();
at1.translate(300-25,400-20);
at1.rotate(Math.PI/180*30,25,20);
g2d.drawImage(image,at1,null);
...
}

Share this post


Link to post
Share on other sites
I particularly like to use Graphics.create don't know whether it is a decent way of creating separate graphics contexts but it works except with it you have to worry about clipping area.


public void rotate(int direction, double deg, Graphics2D g2d) {
int width = super.image.getWidth(null);
int height = super.image.getHeight(null);

//create new context
Graphics2D imgG2D = (Graphics2D)(g.create(currentX, currentY, width, height));

//rotate from the center using radians
imgG2D.rotate(Math.toRadians(deg), width/2, height/2);

imgG2D.drawImage(super.image, 0, 0, null);
}



There are a couple things with this however the new Graphics object imgG2D will clip everything drawn outside of 0,0,width,height so you will get part of your images being cutoff if they are square meaning their width and height aren't the same. Also, the new imgG2D renders everything to the same screen as the original but its translated to the currentX and currentY coordinates which is why I used 0,0 in the drawImage function. Play around with it a bit its a nice way of drawing things because its completely independent of the settings from the original graphics object.

Hope this helps!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this