Rotating a polygon around a point.

Started by
18 comments, last by ViperG 17 years, 1 month ago
I'm trying to rotate a 2d polygon around a point by working out it's current angle in relation to the point and then adding one to that angle, and then setting the polygon's x and y position to correspond to the new angle. The trouble is the shape rotates around the point while getting closer and closer until it stops. I don't want it to get any closer.


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

//vector from point to center of polygon	
int d = sqrt((double)((x-xpos)*(x-xpos)) + ((y-ypos)*(y-ypos)));

//current angle between vector and horizontal
float theta = atan2((float)ypos - y, (float)x - xpos);
float angle = theta * (180 / 3.14159);

const float DEG2RAD = 3.14159/180;
		
angle = -angle -180;
//add one to angle
angle++;
float degInRad = angle*DEG2RAD;
//set x and y position
items[0]->setPosition((x + cos(degInRad)*d), (y + sin(degInRad)*d));
Advertisement
Just out of interest, if you set d at the start to be a constant distance, does it work?
Quote:Original post by Anonymous Poster
Just out of interest, if you set d at the start to be a constant distance, does it work?


hrmm yeah that worked. not sure why though.
The biggest problem I see is that you're storing the distance in an integer, which automatically truncates the result of the square root. Combined with floating-point imprecision this could easily cause your values to degenerate. You want to do as much as your mathematics using floating-points as possible, and only convert to integers when it's absolutely necessary (i.e. transforming into screen-space). It's also possible that the floating-point imprecision alone could give you problems, in which case instead of calculating the distance each frame and trying to maintain it, you chose one beforehand (a constant as the AP suggested).
Yeah that was the point I was making Phoresis. Thanks for answering as I had to leave work and couldnt check back

Anonymous Poster ;)
Quote:Original post by Zipster
The biggest problem I see is that you're storing the distance in an integer, which automatically truncates the result of the square root. Combined with floating-point imprecision this could easily cause your values to degenerate. You want to do as much as your mathematics using floating-points as possible, and only convert to integers when it's absolutely necessary (i.e. transforming into screen-space). It's also possible that the floating-point imprecision alone could give you problems, in which case instead of calculating the distance each frame and trying to maintain it, you chose one beforehand (a constant as the AP suggested).


Thanks for your help. I've discovered another problem that I can't explain also. When the radius is particularly short the item seems to stop rotating near the bottom of the circle.
anybody?
easier way to do it.:

int deg=0;float distance=600;const float piover180 = 0.0174532925f;deg++;if (deg > 360)deg-=360;if (deg < 0)deg+=360;polyx=sin(deg*PIover180)*distance;polyy=cos(deg*PIover180)*distance;


easier to understand angle function
float GetAngl(float x1,float x2,float y1,float y2)  //target, source, target, source{	float Angle=0;	Angle=(atan2(x1-x2,y1-y2) * 57.29578049f) //(57....=180overpie)	if (Angle < 0)		Angle+=360.0f;  //Fix Atan lameness (makes -180 and 180 into 0 - -360)	return Angle; }


[Edited by - ViperG on March 8, 2007 1:44:10 PM]
Black Sky A Star Control 2/Elite like game
Quote:Original post by ViperG
easier way to do it.:

*** Source Snippet Removed ***

easier to understand angle function
*** Source Snippet Removed ***


your angle function looks better, but I'm not sure about the first piece of code, as the distance needs to be in relation to the point i'm rotating about and it's also important that rotation starts from the current angle of the polygon with relation to the point, for various reasons.

float GetDis(float x1,float y1,float x2,float y2){		Distance=(float)sqrt(((float)pow((x1-x2),2))+((float)pow((y1-y2),2)));	return (Distance);}
Black Sky A Star Control 2/Elite like game

This topic is closed to new replies.

Advertisement