A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: 3d vectors - calculating unit vectors?

  1. #1
    Senior Member charlie_says's Avatar
    Join Date
    Feb 2003
    Location
    UK
    Posts
    508

    3d vectors - calculating unit vectors?

    Hi, I got an idea for a game, which requires a rotating cube.
    I managed to get the cube rotation working quite well, until i realised that there was a problem with it (called gimble lock) which to those of you who may have looked into this will know means the order of the rotations will affect the outcome.
    Solution to this problem was to use matrices. Job done, or so I thought until I realised that this rotation wasn't doing quite what I needed either - the rotation being through the world axes, and I needed the rotation through the local axes.
    So, I part code a solution (I'd even say I've done the hard bit) but for the life of me I can't work out the last part.
    UNIT VECTORS
    Can anyone tell me how to calculate a Unit vector from a global x, y and z rotation? (and do I need 3 slightly different iterations for the 3 axes rotations)
    Last edited by charlie_says; 03-26-2005 at 03:39 AM.

  2. #2
    Gross Pecululatarian Ed Mack's Avatar
    Join Date
    Dec 2001
    Location
    UK
    Posts
    2,954
    What do you mean by making a vector out of a rotation? Do you mean the axis of rotation?

    You may also want to look into Quaternions, they are another good solution to the Gimble lock problem

  3. #3
    Senior Member charlie_says's Avatar
    Join Date
    Feb 2003
    Location
    UK
    Posts
    508
    well, as i understand it, and my a level maths was over half a lifetime ago, i need to represent the local axis of rotation by a unit vector. Even though I use the same function for each of the X,Y and Z rotations I imagine that I also have to update the unit vector after each rotation calculation.
    I have this

    code:

    function rotArbAx(deg, X, Y , Z) {
    deg *= (Math.PI/180);
    SIN = Math.sin(deg);
    COS = Math.cos(deg);
    t = 1-Math.cos(deg);
    //
    arb = new Array();
    //
    arb[0] = [t*Math.pow(X,2)+COS, t*X*Y-SIN*Z, t*X*Z+SIN*Y, 0];
    arb[1] = [t*X*Y+SIN*Z, t*Math.pow(Y,2)+COS, t*Y*Z-SIN*X, 0];
    arb[2] = [t*X*Z-SIN*Y, t*Y*Z+SIN*X, t*Math.pow(Z,2)+COS, 0];
    arb[3] = [0, 0, 0, 1];
    //
    return arb;
    }



    and I call it like so

    code:

    transZRot = transform.rotArbAx(z, 0, 0, 1);
    transYRot = transform.rotArbAx(y, 1, 0, 0);
    transXRot = transform.rotArbAx(x, 0, 1, 0);



    in this case where x,y and z are euler rotations (or if that's not the correct description, they rotate from the current position.)
    In the function the rotational axis is represented by X, Y, Z and with the numbers as they stand I get rotation through the global axes, so I'm guessing that if instead of 0s and 1s being pushed to the function I would get rotation thru the local axes. BUt i can't figure it out, I'm pretty sure from the stuff I've read that it should be an iteration of sqrt(x^2+y^2+z^2) = 1
    I do have the global rotations (which I accumulate from the euler figures_ stored in a separate variable, so I do know where the cube has been rotated to in order to calculate the numbers, but I'm just bangin my head against a wall.
    Last edited by charlie_says; 03-26-2005 at 03:38 AM.

  4. #4
    Senior Member charlie_says's Avatar
    Join Date
    Feb 2003
    Location
    UK
    Posts
    508

    ok here's where I'm at

    I've tried, tried again and still not succeded, so here is the fla of where I'm at.
    I'm pretty sure it's not far off the money, but I genuinely can't see what's wrong with this - if anyone cares for a quick look then please do. In case it's not clear I want to rotoate the cube through it's own axes of rotation (i.e through the faces)

    Charlie

    ps. In order to try to understand what was going wrong, in the top left corner you will see a representation of the axes - i think it may indicate (to someone) what is wrong, but I also think there is a problem with the way this is projected (try it you'll see.)

    pps. in order to be honest this example is not completely my own, a lot of it is based on a FriendsOfEd example - i have left a lot of their original code in (i.e. single rotation about an axis) for transparency even though it isn't used, and there is also a good bit of my own added.
    Last edited by charlie_says; 03-28-2005 at 05:11 AM.

  5. #5
    Custom User Title Incrue's Avatar
    Join Date
    Feb 2004
    Posts
    973
    I gess the solution is using quaternions, heard somewhere that with matrices you still have gimbal loop
    There are a article by colin moock somewhere in the macromedia's site about it...is something like 'creating 3d classes' or wharever
    This stuff is complicated as hell...i would like to understand but colin moock dont know how to explain

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