Jump to content
  • Advertisement
Sign in to follow this  
Phoresis

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.

If you intended to correct an error in the post then please contact us.

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

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

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
Just out of interest, if you set d at the start to be a constant distance, does it work?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
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 this post


Link to post
Share on other sites
Guest 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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
anybody?

Share this post


Link to post
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
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);
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!