Rotation in a 2d array

Started by
8 comments, last by LessThanJoe 22 years, 3 months ago
Any idea how I can do rotation in a 2d array. Say I have 2 global variables that store the x and y of the point I want to rotate around. someone gave me the formula: // newx = oldx * cos(t) - oldy * sin(t); // newy = oldx * sin(t) + oldy * cos(t); but it doesn't seem to work when I put it in code.
      
void Rotate(int Angle)
{
	// newx = oldx * cos(t) - oldy * sin(t);

	// newy = oldx * sin(t) + oldy * cos(t);

	
	if (state != 0)
	{
		int oldx = 0;
		int oldy = 0;

		for (int counter = 0; counter < 4; counter++)
		{
			oldx = shape[0][counter] - xpoint;
			oldy = shape[1][counter] - ypoint;
			
			if (oldx != 0 || oldy != 0)
			{
				shape[0][counter] =int(shape[0][counter] + (oldx * cos(Angle) - oldy * sin(Angle)));
				shape[1][counter] =int(shape[1][counter] + (oldx * sin(Angle) + oldy * cos(Angle)));
			}
		}
	}
}
      
help? and thanks in advance to the people who don't tell me to go look it up. (edit: took out some code I had in there for debugging purposes) Edited by - lessthanjoe on January 5, 2002 1:23:41 AM Edited by - lessthanjoe on January 5, 2002 2:06:35 AM
Advertisement
sin() and cos() take angles in radians, not degrees. pi radians equals 180 degrees. There are constants in math.h for pi, but you will have to look them up. pi itself is M_PI. So to convert Angle*M_PI/180.
Keys to success: Ability, ambition and opportunity.
gah! first my calculator screwed me with the degrees and radians mode for about 2 months, now this..

Thanks man!
err, I retract my prior statement (Minus the thanks, that''s still there)

I just did this on paper and it worked out fine with degrees.
I think the problem has something to do with these two line sof code:
oldx = shape[0][counter] - xpoint;
oldy = shape[1][counter] - ypoint;

Now, xpoint and ypoint are set to the same cords of the block I want to shape to rotate around. When It goes down I add one to ypoint. When it moves left I subtract one from xpoint, right i add. The numbers those 2 lines returns are always right except for the posotive and negative. But for those it's strange. One shape may have the x being negative when it should be posotive, another will do the opposite. Obviously you would think that means I am doing something wrong elsewhere, but I have checked all over and have found nothing.
"It's not that my code sucks, it's that the computer sucks" - a quote heard daily (CSAP class) from a friend whom is a bad programmer.

I need to sleep now. Any help is appreciated. Here is a link to the .zip (with the .cpp in it) for those of you feeling board:
(sorry about the copy and paste, new to these boards)
www.geocities.com/azriel182/tetris.zip

(edit: comment Output() and un-comment Debug() in GameMain to make this easier to see. I plan on porting this to directx when I get all the game mechanics working so I didn't really take the time to go back to make it easier to see. (It's not that it's hard to see, it's just not 100% clear))

Edited by - lessthanjoe on January 5, 2002 2:20:02 AM
I'm not sure exactly what worked out on paper means, but the function takes radians. You are rotating around the point (xpoint,ypoint) more or less. You need to add the values back after the rotation. You should replace shape[0][counter] and shape[1][counter] with xpoint and ypoint in the right hand side of the assignments for the rotation.

Edited by - LilBudyWizer on January 5, 2002 4:34:48 AM

Edited by - LilBudyWizer on January 5, 2002 4:35:29 AM
Keys to success: Ability, ambition and opportunity.
Yeah use radians...also, off the top of my head(i have rotated things in 2d before) I don''t remember useing oldy * sin(angle) to find the x coord, or useing oldx to find the y coord...

Also I remeber useing atan or something helped...
In my mario kart game I did rotation like this:
x=xpos+(sin(angle)*hypotenuse);
y=ypos+(cos(angle)*hypotenuse);

where the hypotenuse is the distance from the center of rotation to the positon of rotation. But because of typecasting & converting radians to degrees & visa versa little gaps appeared unless the angle was divisble by 90. The biggest gaps were caused by angles divisble by 45. So in the end I scrambled that idea & instead of rotating the screen around the level I rotated the level around the screen. Where you take into account every piexel on the screen see which coloud it should be , there is a really gd tutorial on 2D rotation here:

http://www.allegro.cc/pixelate/issues/5/articles/circle/sincos.htm

hope that helped
WizHarD
who is it that keeps on nicking WizHarD name !! :P
Striker222,

  //Angle addition formulascos(theta+angle)=cos(theta)*cos(angle)-sin(theta)*sin(angle)sin(theta+angle)=sin(theta)*cos(angle)+cos(theta)*sin(angle)//Polar coordinates of a point(r, theta)//Cartesian coordinates of same point(r*cos(theta), r*sin(theta))//Polar coordinates of rotated point(r, theta+angle)//Cartesian coordinates of rotated point(r*cos(theta+angle), r*sin(theta+angle))//Now the derivationP''.x=r*cos(theta)*cos(angle)-r*sin(theta)*sin(angle)P''.y=r*sin(theta)*cos(angle)+r*cos(theta)*sin(angle)//ButP.x=r*cos(theta)P.y=r*sin(theta)//SoP''.x=P.x*cos(angle)-P.y*sin(angle)P''.y=P.y*cos(angle)+P.x*sin(angle)//Or reorderingP''.x=P.x*cos(angle)-P.y*sin(angle)P''.y=P.x*sin(angle)+P.y*cos(angle)  


Now you can use atan to explicitly find theta, but you don''t have to. If you do it that way you also have to explicitly find r. Also since the tangent is y/x you have a problem if x=0. One other problem is that atan returns angles between -pi/2 and pi/2. You can get around those problems by using atan2. It is perhaps a bit easier to remember and visualize though it takes longer than just using the angle addition formulas.
Keys to success: Ability, ambition and opportunity.
Yes well it was a quick assingment But yeah.

This topic is closed to new replies.

Advertisement