Jav a Image Rotation

Started by
5 comments, last by Gamedave15 14 years, 10 months ago
Hello, Everyone I am working on a java game for a school computer science project, and I have looked EVERYWHERE, but I just cant figure out how to rotate an image in a java applet. I am making a tank game so I need to rotate the tank body when a key is pressed and the gun(a separate image ) based on the mouse position. I already have two normalized vectors describing both of there orientations, but I cant figure out how to rotate the actual image. and I am having one last problem, do any of you guys have any ideas how I would solve collisions when they happen? right now I have a bounding rect for the tank and a map of 32 by 32 collision tiles, and when they intersect I just do this: while(tile intersects map tile) { Move the tank backwards along its directional vector (just multiplying the Velocity by the negative direction vector += the position) } for the most part this works except if the directional vector is almost parallel to the tile the tank is colliding with it pushes the tank all the way down until its not colliding anymore, sometimes this could be all the way across the entire level. I know why this is happening, but I cant think of a solution any help is appreciated Thanks, David
Advertisement
I think what you are after is one of the following methods:
Graphics2D.drawImage(Image img, AffineTransform xform, ImageObserver obs)
Graphics2D.setTransform(AffineTransform Tx)
where you supply an AffineTransform instance containing your rotation.

You might also want to check out what other parts of the java api that uses the AffineTransform class.

As for your collision, I would probably check for collisions before moving the tank and determine exact where the collision occurs and adjust the movement vector so the tank stops before the collision occurs. It's hard to give a more specific solution though without knowing more about the rest of your system.
-LuctusIn the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move - Douglas Adams
Thanks for the help, But I cant get it to work at all...
I got everything in my game, AI collision networking menus ect. , working and the only thing I need is to just get an image to rotate and I cant figure out how to use what you showed me. Is there an example somewhere online of how to use this? or a function that I can just pass in an image and an angle and it returns an image? I realy need this to finish up my game.

Thanks,
David
Here's a simple example of using AffineTransform:
import javax.swing.*;import java.awt.*;import java.lang.Math;import java.awt.geom.AffineTransform;import java.util.Date;import java.net.URL;class Rotater	extends JPanel{	Image img;	public Rotater( Image img ) {		this.img = img;	}		public void paint(Graphics g) {		super.paint(g);		long now = new Date().getTime();		double angle = 0.20 * 2*Math.PI*now/1000.0; // 1/5 rotation per second		double cx = getWidth() / 2.0;		double cy = getHeight() / 2.0;		double r = Math.min(getWidth(), getHeight()) * (3.0/8.0);		AffineTransform m = new AffineTransform();		m.translate(cx, cy); // Move to center		m.rotate(angle); // Rotate around origin		m.translate(r, 0); // Move out a distance r from the origin.		((Graphics2D)g).drawImage(img, m, this);	}}public class Javarot 	extends JApplet	implements Runnable{	private Thread thread;	private static long fps = 30;	public void init() {		setBackground(Color.WHITE);		URL url = this.getClass().getResource("sprite.gif");		Image img = getImage(url);		Rotater r = new Rotater(img);		getContentPane().add(r);	}	public void start() {		thread = new Thread(this);		thread.start();	}	public void stop() {		thread = null;	}	public void run() {		while(true) {			repaint();			try {				Thread.sleep(1000/fps);			} catch (InterruptedException e) {				e.printStackTrace();			}		}	}}


This is the resulting applet.

[Edited by - Luctus on May 21, 2009 8:55:25 PM]
-LuctusIn the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move - Douglas Adams
Thanks!! this works perfect, but is there a way to get the image to rotate about its origin? (not rotate the image around the images center point Like an orbit) but a a tanks turret rotating on top of a tank. I am trying to figure out right now how to do it, but I am running out of time so I was wondering if you knew a simple way to do it, Thanks for all the help

Thanks again,
David
Quote:Original post by Gamedave15
Thanks!! this works perfect, but is there a way to get the image to rotate about its origin? (not rotate the image around the images center point Like an orbit) but a a tanks turret rotating on top of a tank. I am trying to figure out right now how to do it, but I am running out of time so I was wondering if you knew a simple way to do it, Thanks for all the help

Thanks again,
David


Translate so that the image is centered on the point that you want to rotate about, rotate about that point, and then make the opposite translation to the first one.
Thanks Man, you are a life saver! I got everything working right and im presenting it Tomorrow :)

Thanks
David

This topic is closed to new replies.

Advertisement