# Rotating a polygon around a point.

This topic is 4294 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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);

angle = -angle -180;
angle++;
//set x and y position


##### Share on other sites
Just out of interest, if you set d at the start to be a constant distance, does it work?

##### Share on other sites
Quote:
 Original post by Anonymous PosterJust 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.

##### Share on other sites
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).

##### Share on other sites
Yeah that was the point I was making Phoresis. Thanks for answering as I had to leave work and couldnt check back

Anonymous Poster ;)

##### Share on other sites
Quote:
 Original post by ZipsterThe 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?

##### Share on other sites
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]

##### Share on other sites
Quote:
 Original post by ViperGeasier 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.

##### Share on other sites
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);}

1. 1
2. 2
3. 3
Rutin
25
4. 4
5. 5
khawk
14

• 11
• 11
• 23
• 10
• 9
• ### Forum Statistics

• Total Topics
633650
• Total Posts
3013121
×