rotating a polygon in slick (real quick question)

Okay, I know how to rotate things in the render() method by doing g.rotate(float, float, float), but that only rotates the drawing of the polygon. I want to rotate the actual virtual polygon, not just the drawing of it, so i can deal with collisions and stuff. How do i do that? by the way, I’m trying to make a multiplayer asteroids, hopefully networked.

Real quick answer:


Shape rect = new Rectangle(x, y, w, h);
rect = rect.transform(Transform.createRotateTransform(angle, midX, midY));

wait, when i do that, it rotates the ship, but it also moves the ship. i’m making the x and the y be the center of the triangle, too. but the triangle just goes along this weird path as if i made the point of rotation some far off place.

here’s the code if you want to see what i mean :


import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.Image;
import org.newdawn.slick.Sound;
import org.newdawn.slick.Music;
import org.newdawn.slick.Input;
import javax.imageio.ImageIO;
import java.io.*;
import org.newdawn.slick.geom.Polygon;
import org.newdawn.slick.geom.Shape;
import org.newdawn.slick.geom.Transform;
import org.newdawn.slick.particles.ParticleSystem; 
import org.newdawn.slick.particles.effects.FireEmitter; 
public class Game extends BasicGame
{ 
	static boolean FULLSCREEN = false;
	static AppGameContainer app;
	
	float size = 20;
	float num=17.320508f;
	
	int x=320;
	int y=240;
	int rotate=0;
	int color= 0;
	int state=0;
	int shooting=0;
	
	String info = "";
	
	Shape shape;float[] points = {0,0,(size/2),num,(-size/2),num};
	
	

	
	
	
	public Game() 
	{ 
	 	super("Asteroid War");
	} 
	@Override 
	public void init(GameContainer container) throws SlickException
	{
		shape = new Polygon(points);shape.setLocation(320,240);	
		
	} 
	@Override 
	public void update(GameContainer container, int delta) throws SlickException
	{
		
		System.out.println(shape.getCenterX()+" "+shape.getCenterY());
		shape=shape.transform(Transform.createRotateTransform(.1f,shape.getCenterX(),shape.getCenterY()));
		
	}
	
	public void render(GameContainer container, Graphics g) throws SlickException 
	{	  
       	g.draw(shape);
	} 	
	public void keyPressed(int key, char c)
	{
		//if(c=='d'){rotate++;}	
	}
	public void keyReleased(int key, char c)
	{}
	public static void main(String[] args) 
	{ 
		try 
		{ 
			app = new AppGameContainer(new Game());
			app.setDisplayMode(640,480,false); 
			app.setVSync(true);
			app.setShowFPS(true);
			app.setTargetFrameRate(1);
			app.start(); 	
		} 
		catch (SlickException e) 
		{ 
			e.printStackTrace(); 
		} 
		
	} 
}


in this case, the center of the triangle is 330, 251.547. but it rotates while moving, which it shouldn’t do, it’s supposed to be rotating in place. what’s the problem here?

I think the problem has to do with rounding. The function for calculating the center is dividing by 3 which can easily produce periodic numbers. So the real center is a little bit off the one you are using, which results in a small movement. The problem in your code is, that you are calculating the center over and over again, while the object is moving slightly every time. It’s like amplifying the error.

A solution would be to save the center you get in the beginning and turn around it the whole time.

okay, i used

shape=shape.transform(Transform.createRotateTransform(.1f,330f,251.547f));

but I’m still getting the huge movements. is there any other way to rotate a shape?

You have to rotate around the object local center of your shape, not of your viewport. If your shape has a dimension of 60x60, you have to rotate around x=30 and y=30. Also you can recompute the center on every rotation without any precision problem every frame. A problem would only occur, if you have successive computations on the results of previous ones. If you calculate from the original values every time, you have no problems at all.