Big Problem with the rotate transformation.

:slight_smile: hello!
I’m french, so my english will not be perfect :slight_smile:

I make a game with Java and its API Java2D. With this, I rotate my objects, a bullet _ which must take a direction so as to its head is toward the target_ . I use this way:


                        c.rotate(rt, cent_X, cent_Y); 
			
			c.drawRect(X_MIN, Y_MIN, X_MAX, Y_MAX); // it's a rectangle for checking
			
			c.drawImage(img,
				//possition/placement de l'image:				
				pos_X,pos_Y,
				pos_X+largeur,pos_Y+hauteur,
				
				//choix de la sous-image:
				f_base,0,
				(saut_pixel+f_base),hauteur,null);
			
			
			c.rotate(-rt, cent_X, cent_Y); 

The bullet is rotated in the well direction but, its orthogonal frame is changed too. Thus, when I check the bounce with the screenborders, there is error, “the screenborders of the bullet” have rotated.

In fact, the bullet bounces on the Rectangle(X_MIN,Y_MIN,X_MAX,Y_MAX) that i draw “for checking” during the Graphics2D rotation and not on the true BorderScreen.

I don’t know if i was clear, but this problem is very strange.

If you want, I can give a .jar so as to you see my problem.

Edit: the “c” is a Graphics2D used by my Sprite.

re-Edit: it’s all the more strange that my Object Tank don’t pose problem when I rotate it by the same way. This tank move in four directions only (up, down, right, left), so its rotation’s angle takes 0, 90, 180, 270 degres value. And these rotations don’t pose problem _ visible problem would be more right_.

When i deal with transformation, I don’t apply the inverse transformation to come back to the original transformation (Though, I use Graphics2D) :


  AffineTransform tr = c.getTranform();
  c.rotate(rt, cent_X, cent_Y); 
  c.drawRect(X_MIN, Y_MIN, X_MAX, Y_MAX); // it's a rectangle for checking
			
  c.drawImage(img,
  		       //possition/placement de l'image:				
			pos_X,pos_Y,
			pos_X+largeur,pos_Y+hauteur,
			
			//choix de la sous-image:
			f_base,0,
			(saut_pixel+f_base),hauteur,null);
			
  c.setTranform(tr);

Less mistake and quicker (well i didn’t verify but you only restore a matrix so it should).

It is indeed better not to inverse the transform as you may accumulate an error over few iterations.

Dmitri

:slight_smile: thanks!

I know this technique, and I have tried it: but result is the same. It don’t resolve the rotation of the bullet’s orthogonal frame :s
But, if it’s better at a programing point of view (i can say this in english?), I will re-write my code with it.

So, i believe that my problem come from of a no-update of the cent_X an cent_Y variables (i’m a scatter brain). Despite that this update won’t resolve rotation of the orthogonal frame , in according with my calculation, the bullet’s bouncing will work.

I’m going to try and i come back.

;D yeeeeeeeeeeeeeeeeaaahh!!! it’s a success!! ::slight_smile: i have searched in a long time :s and it was very simple :s i didn’t update the center of my bullet, so when java wanted to rotate my bullet by its center, it rotate by a fixed point whereas my bullet was moving :slight_smile:

thanks for you help!