Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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;

//Load Content Method  
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);
share|improve this question
    
You mean lens - lens flare? Is that 3D? Roration is not what lens flare usualy does :-). –  Notabene Mar 31 '11 at 7:24

3 Answers 3

up vote 5 down vote accepted

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

share|improve this answer
    
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. –  bummzack Mar 31 '11 at 12:48
2  
@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. –  Josh Petrie Mar 31 '11 at 15:42
    
Thanks so much for your help man much appreciated !! –  dbomb101 Mar 31 '11 at 17:52

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)

share|improve this answer

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!).

share|improve this answer
    
this should be a comment –  bummzack Mar 31 '11 at 17:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.