PDA

View Full Version : [FMX] Trouble with rotation and elasticity



Baron Ruhstoff
September 9th, 2003, 04:02 AM
Hey folks. I'm new to the forum and in need of some advice. I have a clip that rotates to orient itself towards the mouse when the mouse is over a given area. When the mouse leaves the area, the clip snaps back to its original position.

There are a few problems that I need help with:
1) the clip snaps directly towards the mouse at the moment the mouse enters the clip; I would like it to actually rotate;
2) the clip snaps directly back to its original position when the mouse leaves the clip; I would like it to ease back into position.

The code I am working with is below. I am currently using an easing function supplied by Robert Penner, but would be happy to try something else. Perhaps the real problem I am having is attaching functions to objects...


gearMC.anchor = 90
gearMC.startRotation = gearMC._rotation;

hitAreaMC.onEnterFrame = function() {
if (hitAreaMC.hitTest(_root._xmouse, _root._ymouse)) {
gearMC._rotation = Math.atan2(_ymouse-gearMC._y, _xmouse-gearMC._x)*180/Math.PI;
}
else {
gearMC._rotation = gearMC.anchor;
}
}

//Easing code courtesy of Robert Penner

Math.easeOutElastic = function (t, b, c, d, a, p) {
if (t==0) return b; if ((t/=d)==1) return b+c; if (!p) p=d*.3;
if (a < Math.abs(c)) { a=c; var s=p/4; }
else var s = p/(2*Math.PI) * Math.asin (c/a);
return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
};

grandsp5
September 9th, 2003, 04:06 AM
i will try and make you something...but its 1:10 in the morning so I might be a little slow

grandsp5
September 9th, 2003, 04:14 AM
ok your problem is on the 2 statemes where you say gearMC._rotation = blah. That will snap it instantly. You need an onEnterFrame handler to rotate it for you slowly. You will need to fill in the math because my brain isnt working to well right now.


if (hitAreaMC.hitTest(_root._xmouse, _root._ymouse)) {
gearMC.easeTo();
}

easeTo = function() {
this.onEnterFrame = function(){
if(gearMC._rotation <= Math.atan2(_ymouse-gearMC._y, _xmouse-gearMC._x)*180/Math.PI-5)
this._rotation +=5;
}
}



untested but try it. do the same sort of thing to go back to your starting point. sorry if i did something dumb, it's time for me to crash. and welcome to the forums

Baron Ruhstoff
September 9th, 2003, 04:16 AM
Hoo, that was quick!

Makes sense. I'll give it a go and let you know how it turns out.

Thanks for the help.