View Full Version : Rotation/Mouse
Lord Rahl
August 1st, 2005, 02:17 PM
I'v seen many games, flash based things, etc. One of the coolest effects I find is the ones where there is a little guy or object on the screen, and it rotates so the front is faceing the direction of the mouse. Where the mouse goes, the objects face follows. I could search for a tutorial and I probaly will, but a good explanation on how this type of thing works would be cool. (Love learning new things:cowboy: )
TheCanadian
August 1st, 2005, 02:49 PM
If I had a dime for every time I've explained this . . . I would have around 60 cents (http://www.kirupa.com/forum/showthread.php?t=185092). You should probally also learn trigonometry (if you don't know it already).
Lord Rahl
August 1st, 2005, 03:06 PM
If I had a dime for every time I've explained this . . . I would have around 60 cents (http://www.kirupa.com/forum/showthread.php?t=185092). You should probally also learn trigonometry (if you don't know it already).
6 times eh ;)
TheCanadian
August 1st, 2005, 03:20 PM
Ha ha. I hope it helped.
bandinopla
August 1st, 2005, 03:23 PM
hmmm... mabe like this?
var x=_xmouse, y=_ymouse
var tagetAngle=Math.atan2(this._y-y, this._x-x)*180/Math.PI
if(this._rotation<targetAngle){
this._rotation+=1
}else if ...
...etc
Dauntless
August 1st, 2005, 04:56 PM
Almost...
mc.onMouseMove = function(){
var x:Number = _xmouse - this._x;
var y:Number = _ymouse - this._y;
var angleRad:Number = Math.atan2(y, x);
var angleDeg:Number = angleRad/Math.PI * 180;
this._rotation = angleDeg;
}
:)
TheCanadian
August 1st, 2005, 06:03 PM
hmmm... mabe like this?
var x=_xmouse, y=_ymouse
var tagetAngle=Math.atan2(this._y-y, this._x-x)*180/Math.PI
if(this._rotation<targetAngle){
this._rotation+=1
}else if ...
...etc
Actually this would not work since flash does not use the 360 degrees that the rest of the world does. Instead it goes from 0 to 180. 180 to -180. Then -180 back to 0. I'm sure Macromedia has their reasons. :td:
Dauntless
August 1st, 2005, 06:55 PM
Actually this would not work since flash does not use the 360 degrees that the rest of the world does. Instead it goes from 0 to 180. 180 to -180. Then -180 back to 0. I'm sure Macromedia has their reasons. :td:And the code also wouldnt work because it's not in an onEnterFrame... (or onMouseMove)
TheCanadian
August 1st, 2005, 07:09 PM
And the code also wouldnt work because it's not in an onEnterFrame... (or onMouseMove)
Also true :)
Krilnon
August 1st, 2005, 07:17 PM
Actually this would not work since flash does not use the 360 degrees that the rest of the world does. Instead it goes from 0 to 180. 180 to -180. Then -180 back to 0. I'm sure Macromedia has their reasons. :td:
I know what you mean, the weird angle system has gotten in my way a number of times. :angry:
TheCanadian
August 1st, 2005, 07:56 PM
Yes it is stupid. The code that bandinopla posted would actually be very useful if Macromedia didn't decide to change trigonometry. What are you going to do though.
NiñoScript
August 1st, 2005, 09:16 PM
look look look!!!
i dont really know how to make it know wich way is shorter, couse when it has _rotation 340, and i want to go to 10 the fastest way should be to go up, but it goes down, i'd need some help with that...
but anyway, does this help??
onClipEvent (enterFrame) {
this.adjacent = _root._xmouse-this._x;
this.opposite = this._y-_root._ymouse;
if (this.adjacent<0 && this.opposite>0) {
this.rotation = -90+(360-Math.atan2(this.opposite, this.adjacent)*(180/Math.PI));
} else {
this.rotation = -90-Math.atan2(this.opposite, this.adjacent)*(180/Math.PI);
}
if (this._rotation<this.rotation-6) {
this._rotation += 5;
} else if (this._rotation>this.rotation+6) {
this._rotation -= 5;
}
}
TheCanadian
August 1st, 2005, 09:39 PM
onClipEvent (enterFrame) {
myRadians = Math.atan2(_root._ymouse-this._y, _root._xmouse-this._x);
myDegrees = Math.round((myRadians*180/Math.PI));
if (myDegrees>this._rotation) {
this._rotation++;
}
if (myDegrees<this._rotation) {
this._rotation--;
}
}
That is the code I came up with. Somebody PM'd me a while back with code that did this action perfectly. It was pretty long and he wanted to see if I could make it shorter. I couldn't - all I came up with was that, which doesn't work. And to to top that off, I deleted the message so now I can't even look at/post the code. But i'm going to solve this. It is my everest ;)
NiñoScript
August 1st, 2005, 10:46 PM
uhhm that, wat u just posted is ten times simpler than mine... and does the same :(
but, it has the same problem too though :P
wish someone can make it work
i was thinking something like this:
instead of:
if (myDegrees<this._rotation)
this:
if (myDegrees<this._rotation || myDegrees-180>this._rotation)
or something like that, that should test if the myDegrees var are less than this._rotation, or if the opposite is bigger, i dunno if that would work
PLEASE!!! WE NEED HELP IN HERE!!!!!
bandinopla
August 2nd, 2005, 12:01 PM
TAKE THIS! My code was GOOD and IS GOOD. Cpoy/Paste this code in an MC and crie!
PD: This code need a final touch, but I don't going to put it.
onClipEvent(enterFrame){
x=_root._xmouse
y=_root._ymouse
targetAngle=Math.atan2(y-this._y, x-this._x)
fin=(targetAngle*180)/Math.PI
if(this._rotation<fin){
this._rotation+=5
}else if(this._rotation>fin){
this._rotation-=5
}
}
bandinopla
August 2nd, 2005, 12:03 PM
Yes it is stupid. The code that bandinopla posted would actually be very useful if Macromedia didn't decide to change trigonometry. What are you going to do though.
Thanks for the support!
bytheway, the code work... I posted.
TheCanadian
August 2nd, 2005, 12:18 PM
Well I made this last night. It's exactly the same as the code I posted before but I converts Flash's weird degree system into the proper 360 degrees. It works fine until you pass over from 359 to 0, then it will go all the way back around.
EDIT: Actually bandinopla, your code has the same problem as everyone elses and it is very wobbly when it gets to the target angle.
Templarian
August 2nd, 2005, 12:22 PM
Wow i wish i would of saw this post earlier i could of gave you some of my bro's code it works perfect just like your guys but doesnt have the little glitch like urs. (and theirs a little unit in the middle that fires bullets lol).
TheCanadian
August 2nd, 2005, 12:23 PM
Well if it works perfectly, I would really like to see it :)
Templarian
August 2nd, 2005, 12:25 PM
I think hes playing Planetside ATM. when he goes to get something to eat ill ask it from him.
//edit, yours is seriously glitchy too like it will spin wierd ways.
bandinopla
August 2nd, 2005, 12:28 PM
Well I made this last night. It's exactly the same as the code I posted before but I converts Flash's weird degree system into the proper 360 degrees. It works fine until you pass over from 359 to 0, then it will go all the way back around.
EDIT: Actually bandinopla, your code has the same problem as everyone elses and it is very wobbly when it gets to the target angle.
I know, thas why i SAID, the code need a final touch. But I think that the people ho read this is smart to fix it. I not going to fix it bicouse is so easy that is an insult to my precious time.
Another thing> Wobbly? que carajo es esa puta palabra? Hay algun foro en español? Estoy arto de tener que traducir mentalmente todo lo que leo. Es poco productivo.
Sirisian
August 2nd, 2005, 12:34 PM
wow I didn't even know trig and I made this like a year ago. I've posted this many of times on how to do this.
*Note* When the program adds to the rotation use this
if(unit.rotation==359){
unit.rotation=0;
}else{
unit.rotation++;
}
*Note* When the program subtracts to the rotation use this
if(unit.rotation==0){
unit.rotation=359;
}else{
unit.rotation--;
}
I have a newer version of this code but it's in C++. Other than those two changes the code will work fine. At the top it say while(???<4) if you change the 4 it will move slower and faster.
Templarian
August 2nd, 2005, 12:37 PM
Thx bro, you guys may be able to shorten some of his code down a little further than his is.
TheCanadian
August 2nd, 2005, 01:30 PM
Thank you.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.