|
-
Senior Member
is this impossible?
I have been messing about with this for some time now, and I can't get it to work. The effect I want is an mc that rotates to point towards a point on the stage. This is easy using Math.atan2 like this:
Code:
xd = _root._xmouse-_x;
yd = _root._ymouse-_y;
_rotation= Math.atan2(yd, xd)/toRad;
The effect I want is for the mc to rotate slowly (at constant speed) towards that point, taking the shortest route. You might think this is easy, but when the mc (mouse in this case) is in a certain direction the mc rotates the longer way towards it, instead of the shortest.
Has anyone got code that can make the effect? I'm almost giving up.
-
Senior Member
Try the idea Arne The Turtle posted in the end of this thread:
http://www.flashkit.com/board/showthread.php?t=509004
-
Senior Member
Well, that doesen't work either. It will rotate right when the mouse is below, to the right and to the left, but when you move it up it doesen't work.
-
Senior Member
Im only new at flash but im gona give it ago, i also wanted to do this
92.7 Fresh FM for all your South Aussies - Doof Doof music FTW people!
-
Senior Member
Im sorry but tony's example was correct and i got it working like this.
Create an MC stick in on da stage give it a instance called mc1 and stick this code on the main timeline. It will rotate towards the mouse traverling the shortests possable distance. Its quite nice realy.
Code:
onEnterFrame = function () {
var rotation_speed = 10;
var xd = mc1._x-_xmouse;
var yd = mc1._y-_ymouse;
var an = Math.atan2(yd, xd);
var an = an*180/Math.PI;
if (mc1._rotation>an) {
if (mc1._rotation-an<=180) {
mc1._rotation -= rotation_speed;
} else {
mc1._rotation += rotation_speed;
}
} else if (mc1._rotation != an) {
if (an-mc1._rotation<=180) {
mc1._rotation += rotation_speed;
} else {
mc1._rotation -= rotation_speed;
}
}
};
There was only one problem with that, when it had aligned it self with the target (the mouse in this case) you get some jerking because it cant go dircelty %100 in that direction.
Thanks tony for that link.
92.7 Fresh FM for all your South Aussies - Doof Doof music FTW people!
-
Senior Member
Well, it was quite old thread so I decided to write new code based on vectors:
Code:
//declare mc
mcname=_root.mc;
//reset angle
mcname.angle=0;
//rotation speed
mcname.rotSpeed=2;
//set function to enterframe event
mcname.onEnterFrame=rotate;
function rotate(){
this._rotation=this.angle;
var rAng=this.angle/180*Math.PI;
//find current vector
var vx1=Math.cos(rAng);
var vy1=Math.sin(rAng);
//find vector to mouse coordinates
var vx2=this._x-_root._xmouse;
var vy2=this._y-_root._ymouse;
var dist=Math.sqrt(vx2*vx2+vy2*vy2);
//normalise
vx2=vx2/dist;
vy2=vy2/dist;
//find dot product between left normal and vector to mouse
var dp = vy1*vx2 - vx1*vy2;
if(dp<-this.rotSpeed/90){
//rotate counter-clockwise
this.angle-=this.rotSpeed;
}else if(dp>this.rotSpeed/90){
//rotate clockwise
this.angle+=this.rotSpeed;
}
}
this.rotSpeed/90 is to avoid jerking, I dont have theory for that value, but this looked about right.
The code uses dot product of 2 vectors which show how both vectors are directed.
Last edited by tonypa; 03-04-2006 at 08:38 AM.
-
Senior Member
if (_rotation<0) {
_rotation = 359;
}
if (_rotation>359) {
_rotation = 0;
}
was causing the problem. Don't know why it is there.
The vector one works perfectly 
Thanks alot!
-
if ( MCNAME._rotation<0) {
( MCNAME._rotation = 359;
}
if (( MCNAME._rotation>359) {
( MCNAME._rotation = 0;
}
Hope nobody knows I am still on Flash 5 
______________________________________
All artists are prepared to suffer for their work
but why are so few prepared to learn to draw?(Banksy)
-
Senior Member
yeah, I know that, but I didn't do it from root, so I don't need the mc name there
-
Senior Member
Hey tony thats some nice code there, and it was preaty simple to follow as well.
92.7 Fresh FM for all your South Aussies - Doof Doof music FTW people!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|