Sunday, April 4, 2010

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%26gt;currentAngle rotate+, but this doesn't care for the 180/-180 passage.?any ideas??thank you in advance,?fabec.

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;

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

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.

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%26lt;190) {

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

midO = false;

} else {

var wheelCurrentRotation:Number = rotatingClip._rotation;

var targetRotation:Number = arrayWithTargetRotations[menuItemSelected];

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%26gt;=targetRotation %26amp;%26amp; wheelCurrentRotationModulo-10%26lt;=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%26lt;180 %26amp;%26amp; midO == false) :

rotatingClip._rotation -= incr/2;

break;

?case (targetRotation-wheelCurrentRotationModulo%26gt;180 %26amp;%26amp; midO == false) :

rotatingClip._rotation += incr/2;

break;

?}

}

mid = false;

double post...

Message was edited by: fabec

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%26lt;180){
?inc=-3;
?} else {
?inc = 3;
?}
}

hi kglad,
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...)

%26amp; nbsp; case (r%26lt;=180 %26amp;%26amp; r%26gt;=0 %26amp;%26amp; midO == false) :
%26amp; nbsp; menuSP._rotation += incr/2;
%26amp; nbsp; break;

%26amp; nbsp; case ((r%26gt;180 || r%26lt;0) %26amp;%26amp; midO == false) :
%26amp; nbsp; menuSP._rotation -= incr/2;
%26amp; nbsp; break;

(...)

anyway, your idea was the correct one.?it is allways a warm pleasure to find a helping mind with time to spare on the other side of the wire.?thank you very much,?fabec

you're welcome.

No comments:

Post a Comment