PDA

View Full Version : Rotating a 2d polygon around a point.



Phoresis
03-08-2007, 09:48 AM
Currently I have code that is repeatedly called which animates a 2d polygon rotating around a point, based on how close to the point it is.

the problem is, my code seems to work fine for a radius of 100 or greater, but the polygon doesn't rotate for a radius shorter than that.

Here is my code:


if (items[0] != NULL)
{
//the center co-ordinates of 2d polygon
int xpos = items[0]->getX();
int ypos = items[0]->getY();

//vector from point to center of polygon
float d = items[0]->getArc();

//current angle between vector and horizontal
float theta = atan2((float)ypos - y, (float)x - xpos);
float angle = float(theta * (180 / 3.14159));
items[0]->rotate(-angle - 90);

float DEG2RAD = (float) 3.14159/180;

angle = -angle +180;
//add one to angle
angle++;
//convert to radians
float degInRad = angle*DEG2RAD;
//set x and y position of polygon
items[0]->setPosition((float)(x + cos(degInRad)*100), (float)(y + sin(degInRad)*100));
}