Page 1 of 1

Check this!

PostPosted: Wed Aug 30, 2017 10:08 am
by Luezma
I made this function without really understanding what I was doing... Anyway, this function returns the smallest rotation needed to get from one angle to another, instead of using angle1 - angle2, even if they are like -3.06 and 2.5 (Algodoo would normaly go all the way from -3.06 to 2.5, but rotating in clockwise is a better option for something like angvel), this function has a range of -2π to 2π, anything else will fail. It's not finished as I can't input higher values by now.

Mr. Code
Code: Select all
angularDistanceOrWhatever := (an, gle)=>{
    a := 0;
    b := 0;
    an > math.pi ? {
        a = an - math.pi * 2
    } : {
        an <  - math.pi ? {
            a = an + math.pi * 2
        } : {a = an}
    };
    gle > math.pi ? {
        b = gle - math.pi * 2
    } : {
        gle <  - math.pi ? {
            b = gle + math.pi * 2
        } : {b = gle}
    };
    x := a - b;
    y := -(b - a - math.pi * 2);
    z := -(b - a + math.pi * 2);
    (x ^ 2) ^ 0.5 < (y ^ 2) ^ 0.5 ? {
        (x ^ 2) ^ 0.5 < (z ^ 2) ^ 0.5 ? {x} : {z}
    } : {
        (y ^ 2) ^ 0.5 < (z ^ 2) ^ 0.5 ? {y} : {z}
    }
}


You can test it on a circle with with...
Code: Select all
postStep = (e)=>{
    yeah := app.mousepos - pos;
    angvel =  - _dist(angle - math.pi / 2,  - math.aTan2(yeah(0), yeah(1)))
}

Re: Check this!

PostPosted: Wed Aug 30, 2017 10:31 am
by Kilinich
Too complicated.
I wrote whis one for my old AI battel scene:

Code: Select all
angleDiff := (a1, a2)=>{
    tau := (math.pi * 2);
    d := (a1 % tau - a2 % tau) % tau;
    d > math.pi ? {d - tau} : {d <  - math.pi ? {d + tau} : {d}}
}

Re: Check this!

PostPosted: Fri Sep 01, 2017 1:05 am
by Luezma
What does % do?

*edit* Ok I know what it does, and I can't believe how simple you made it...