8 Replies Latest reply on Oct 16, 2009 6:51 AM by kglad

# how to correctly rotate a wheel menu

hi,

i have a ring shaped menu with 7 entries on it (see attached image).  when the mous is over it it rotates depending on mouse position.  and when 1 entry is seleceted, i want it to rotate back to a certain angle so that such selection appears on top of it (12:00 .on a clock).   i have an array with the corresponding rotations for each entry.  but i would like it to rotate through the shortest way and i simply can't find it!.  logic says: if destinationAngle>currentAngle rotate+, but this doesn't care for the 180/-180 passage.  any ideas?  thank you in advance,  fabec.

• ###### 1. Re: how to correctly rotate a wheel menu

instead of dealing with angles from -180 to +180, convert all angles to 0 to 360 by using:

ang = (ang+360)%360;

• ###### 2. Re: how to correctly rotate a wheel menu

sorry i pressed the wrong button...

hi,

thanks, kglad, again, for your answer.  i've tried the modulo option (which i saw in one of your answers to someone else on this forum) but it isn't enough (though it helps) to solve my case.

what i am looking for is the shortest way to rotate my wheel, not to rotate it at once, and the problem is that the reference angle changes permanently:  let's say the angle of the item i clicked is 250 (or -110 before % operation).  when i trigger the rotation process (+/- 1 pix/fram @25fps) my wheel is at 0 degrees.  allthough 250 is bigger than 0 my rotation has to be negative.  but if the angle i am looking for is 110, still bigger than 0, my rotation has to be positive.  basicaly, if i don't do the modulo operation, it works for certain angles, and for others if i apply it...  what i need to find is...  the shortest way.

thanks, fabec

• ###### 3. Re: how to correctly rotate a wheel menu

for angles less than 180, rotate to 0.  for angles greater than 180, rotate to 360.

in your example, rotate from 250 up to 360.

• ###### 4. Re: how to correctly rotate a wheel menu

it was actually the other way round: going from 0 to 250, not 250 to 0...

but, never mind here is the code concerned, hope it will helps.  sorry i just don't know how to format it properly in this forum:

// if the distance between the wheel and the mouse is under 190, the wheel rotates with mouse position

if (distance<190) {

rotatingClip._rotation -= (rotatingClip._parent._xmouse-rotatingClipCenterX)/100;

midO = false;

} else {

var wheelCurrentRotation:Number = rotatingClip._rotation;

var wheelCurrentRotationModulo:Number = (wheelCurrentRotation+360)%360;

var targetRotationModulo:Number = (targetRotation+360)%360;

switch (true) {

// if current rotation is within 20 degrees of the target, the target rotation is appplied and rotation stopped

case (wheelCurrentRotationModulo+10>=targetRotation && wheelCurrentRotationModulo-10<=targetRotation) :

rotatingClip._rotation = targetRotation;

midO = true;

break;

//THIS IS THE PART WHERE I HAVE THE PROBLEM:  WHAT IS THE SHORTTEST WAY FROM MY CURRENT POSITION TO MY TARGET

//WHAT DIRECTION TO SPIN??

case (targetRotation-wheelCurrentRotationModulo<180 && midO == false) :

rotatingClip._rotation -= incr/2;

break;

case (targetRotation-wheelCurrentRotationModulo>180 && midO == false) :

rotatingClip._rotation += incr/2;

break;

}

}

mid = false;

• ###### 5. Re: how to correctly rotate a wheel menu

double post...

Message was edited by: fabec

• ###### 6. Re: how to correctly rotate a wheel menu

that should be:  (180+bar._rotation)%360;

so, for example, rotateF() will cause bar towards rotate north:

bar.onEnterFrame=function(){
this._rotation+=inc;
}

function rotateF(){
var r:Number = (180+bar._rotation)%360;
if(r<180){
inc=-3;
} else {
inc = 3;
}
}

• ###### 7. Re: how to correctly rotate a wheel menu

it worked!  i just had to do a little tweening: it is on the difference between present angle and destination angle that i applied your magic modulo operator, and the 'if' on your function now checks for a difference between 0 and 180, for positive rotation, or 181 and over or negative, for negative rotation:

//distance between angles

var r:Number = (180+(presentRotation-targetRotation))%360.

(...)

//positive or negative rotation..  ('incr' is defined elsewhere...)

case (r<=180 && r>=0 && midO == false) :
break;

case ((r>180 || r<0) && midO == false) :