# Rotating object around circumference of circle

I'm trying to get a lens texture to rotate around the circumference of the circle and although I picked the centre of the circle as the rotating point of the lens it doesn't work.

 private float angle;

//Initialize Method
circlePosition.X = vp.X + vp.Width / 2;
circlePosition.Y = vp.Y + vp.Height / 2;

lensPosition.X = circlePosition.X;
lensPosition.Y = circlePosition.Y + 155;

circleOrigin.X = Circle.Width / 2;
circleOrigin.Y = Circle.Height / 2;

lensOrigin.X = Lens.Width / 2;
lensOrigin.Y = Lens.Height / 2;

//Update Method
angle += 0.005f;

//Draw Method
spriteBatch.Draw(Circle, circlePosition, null, Color.White, 0.0f,
circleOrigin, 1.0f, SpriteEffects.None, 0f);

spriteBatch.Draw(Lens, lensPosition, null, Color.White, angle,
circleOrigin, 1.0f, SpriteEffects.None, 0f);

• You mean lens - lens flare? Is that 3D? Roration is not what lens flare usualy does :-). Mar 31, 2011 at 7:24

You could try

private Vector2 Rotate(float angle, float distance, Vector2 centre)
{
return new Vector2((float)(distance * Math.Cos(angle)), (float)(distance * Math.Sin(angle))) + centre;
}


Keep in mind I'm not at a computer I could test this on but that should be in the right direction

Edit: Checked it, works fine

Edit 2: Something else cool you could do:

public Vector2 Rotate(float angle, Vector2 currentPos, Vector2 centre)
{
double distance = Math.Sqrt(Math.Pow(currentPos.X-centre.X, 2) + Math.Pow(currentPos.Y-centre.Y, 2));
return new Vector2((float)(distance * Math.Cos(angle)), (float)(distance * Math.Sin(angle))) + centre;
}


That way you put in the angle you'd like to be rotated to, the position you're currently at and the the central point you'd like to be rotated around

• Your code works, but why use expensive stuff like Sqrt while you could achieve the very same by simply setting the sprite origin correctly? I don't know how XNA handles memory allocation, but calling new for every update isn't best practice in most programming languages I'm aware of. Mar 31, 2011 at 12:48
• @bummzack Vector2 is a value type (assuming XNA's Vector2 or some other sane implementation), so the new there is just an extremely cheap stack allocation.
– user1430
Mar 31, 2011 at 15:42

I don't know XNA, but usually the rotation of a sprite is around it's origin. So the lens position should be the center of the circle and the origin should be y: lensRadius, x: lensRadius - circleRadius. That would initially position your lens at the 3 o'clock position (0 radians)

Harold's reply should sort you out, but to clarify, I think the source of confusion here is that "rotating around the circumference" is actually 'positioning around the circumference'. Passing the angle directly into the spritebatch draw will simply orientate the lens texture around its own centre. Instead you need to use the angle to find the circumference position and use that in the draw call. Harold's code snippet should do this step for you if you call it each Update (cheers Harold!).

• this should be a comment Mar 31, 2011 at 17:58