A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: rotational maths

  1. #1
    Junior Member
    Join Date
    Nov 2001
    Posts
    10

    rotational maths

    Hey guys

    I've been using flash for a while now but ive never looked into using maths/code to make stuff move around so i come beggin your concil

    What I have is a symbol and i want it to be rotating when the page loads and then if the user puts thier mouse over it for it to change direction/angular velocity in order of how fast the pointer/symbol interaction was

    That wasnt very clear so let me give u an analogy, imagine a record spinning on a turntable with the motor turn of. If you put ur hand across the turntable and span it (in either direction) it would spin for a time in that direction at a speed relative to how fast u push the turn table and then come to a stop.

    That is the kind of thing i want to do, but instead of coming to a total stop...just spin very slowy.

    Any advise or information and guidance you can offer greatly appreciated

    thanks
    AJ

  2. #2
    Bacon-wrapped closures Nialsh's Avatar
    Join Date
    Dec 2003
    Location
    Houston!
    Posts
    338
    Pretty interesting problem.

    See the attachment for a visual. Whenever the mouse is over the turntable, you should save the angle (green lines) between the mouse (blue dot) and the center of the turntable (black dot) in a variable. Once you have two angles, find the difference between them (short black arcs) and you have the angular velocity of your turntable. Save that in a variable and then every onEnterFrame you should A. increase the turntable's rotation by the angular velocity and B. multiply the angular velocity by a "friction", probably somewhere between .9 and .95

    To leave it spinning slowly after the user plays with it instead of coasting to a stop, put a test around the friction multiplier:
    if(Math.abs(aV) > MinSpeed){ aV *= Friction; }

    If your turntable is especially small or you just want a lot of accuracy, you'll probably want to use the mouse position just before it touches the turntable and the one just after it leaves.

    I've pretty much given you a general overview of how it should work, so I can understand if you need help implementing it. It uses some trig for the angle stuff. Feel free to ask me for further explanation of anything or everything.

    Neal
    Attached Images Attached Images

  3. #3
    Junior Member
    Join Date
    Nov 2001
    Posts
    10
    woah man thats cool - uba helpful. I am going to need masses of help with the implmentation as my AS skillz are next to nill... i can just about do timeline control lol. Accuracy isnt a major issue this is just something to look nice.

    www.thefieldofchampions.com/home.html [NB: best to use firefox if you have it...my flash design and IE are not liking each other atm and it may cause some problems i.e. movie not loaded]

    in the top left of the page is the icon that i have been given to work with. I have done a simple motion tween on it for now jst to see what its like (feel free to ignor the crapy template of a webpage underneath i intend to change it shortly...)

    Well ive done/am doing A level physics/maths (high school i think is equivalent over the pond) so dont be afraid to hit it with the maths and trig etc.

    So ive got my little symbol on the stage...what now?

    Thanks again

    AJ

  4. #4
    Bacon-wrapped closures Nialsh's Avatar
    Join Date
    Dec 2003
    Location
    Houston!
    Posts
    338
    I couldn't get the very top movie to load with IE or firefox. Anyway, since you seem to not know very much about AS at all, it's easier to just code it for you and let you see how it should go than to try to walk you through coding it yourself.
    code:
    //radius of record
    radius = record_mc._width/2;
    Friction = .95;
    MinSpeed = 5;
    //rotational speed
    rSpeed = 0;
    //tells if the mouse is over the record or not
    contact = false;

    this.onEnterFrame = function(){
    //find distance between mouse and record
    xDiff = record_mc._x-_xmouse;
    yDiff = record_mc._y-_ymouse;
    dist = Math.sqrt(xDiff*xDiff+yDiff*yDiff);
    //set contact variable based on if the record is being touched or not
    if(dist <= radius){
    contact = true;
    }else{
    contact = false;
    //set sentinel value
    oldAngle = undefined;
    }
    if(contact){
    //get current angle with arc tangent convert from radians to degrees
    angle = Math.atan2(yDiff, xDiff) *180/Math.PI;
    //see if more than one angle has been found; set new rSpeed if there are two
    if(oldAngle != undefined){
    rSpeed = angle-oldAngle;
    }
    //send angle back to oldAngle
    oldAngle = angle;
    }
    //manipulate record based on speed
    record_mc._rotation += rSpeed;
    //decrease speed if it is fast enough
    if(Math.abs(rSpeed) > MinSpeed)
    rSpeed *= Friction;
    }

    This code would go in the timeline, and it assumes that the instance name of your record is record_mc. I tried to make it obvious what I was doing with comments but you're bound to have a lot of questions anyway since you don't know the language.

    Neal

  5. #5
    Junior Member
    Join Date
    Nov 2001
    Posts
    10
    how odd... i was messing round with files on the server earlier so that mite have been y it wasnt loading

    Anyway i will have a bash with this when i retrun home, tho looking thru the code it all seems fairly straight forward - as in i know wt ur doing...i couldn;t have written that, my programing knowledge extends as far as VB and thats about it. Thanks alot man ill let u know how it comes out

  6. #6
    Junior Member
    Join Date
    Nov 2001
    Posts
    10
    man that rocks! thank u so much only thing is... the rotation seems a bit jerky...ne ideas as to how to smooth it out? (will upload later)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center