mapping 3d object onto jit.boids3d


    Jun 07 2007 | 7:27 am
    Hi all,
    I've been messing with mapping of a 3d object onto the trajectory of
    jit.boids3d object. Obviously mapping object to the right position is
    trivial. However, even though I am calculating angles using old xyz vs. new
    xyz and then calculating actual angles using atan, something fishy is going
    on.
    The algorithm for parsing data out is contained within javascript file which
    looks as follows:
    var x1=0;
    var y1=0;
    var z1=0;
    var x2=0;
    var y2=0;
    var z2=0;
    var xvec=0;
    var yvec=0;
    var zvec=0;
    var xrot=0;
    var yrot=0;
    var zrot=0;
    var PI=3.1415927;
    function calculate()
    {
    xvec=x2-x1;
    yvec=y2-y1;
    zvec=z2-z1;
    /////////////////////
    zrot=-Math.atan(xvec/yvec)/PI*180-90;
    if(yvec < 0) {
    if (xvec < 0) {
    //post("cn");
    zrot = 180 - zrot;
    }
    else {
    //post("dn");
    zrot = 180 + zrot;
    }
    }
    else if (xvec < 0) {
    //post("bn");
    }
    else {
    //post("an");
    zrot = -zrot;
    }
    /////////////////////
    yrot=Math.atan(xvec/zvec)/PI*180-90;
    if(zvec < 0) {
    if (xvec < 0) {
    //post("cn");
    yrot = 180 + yrot;
    }
    else {
    //post("dn");
    yrot = -yrot;
    }
    }
    else if (xvec < 0) {
    //post("bn");
    }
    else {
    //post("an");
    yrot = 180 - yrot;
    }
    /////////////////////
    xrot=Math.atan(yvec/zvec)/PI*180+90;
    if(zvec < 0) {
    if (yvec < 0) {
    //post("cn");
    xrot = 180 - xrot;
    }
    else {
    //post("dn");
    xrot = 180 - xrot;
    }
    }
    else if (yvec < 0) {
    //post("bn");
    xrot = 180 + xrot;
    }
    else {
    //post("an");
    xrot = -xrot;
    }
    output();
    }
    function output()
    {
    outlet(5,zrot);
    outlet(4,yrot);
    outlet(3,xrot);
    outlet(2,z2);
    outlet(1,y2);
    outlet(0,x2);
    }
    function bang()
    {
    output();
    }
    function list()
    {
    var tmp = arrayfromargs(arguments);
    if (tmp.length != 9)
    {
    post ("zrot says: Invalid number of arguments (9
    needed).n");
    }
    else
    {
    x1=tmp[0];
    y1=tmp[1];
    z1=tmp[2];
    x2=tmp[3];
    y2=tmp[4];
    z2=tmp[5];
    calculate();
    }
    }
    Now, some of these had to be for some reason adjusted manually (namely four
    quadrants of a circle and their behavior using atan()). Why? I have no idea.
    At any rate, I was wondering whether there is a more "elegant" way to
    achieve the same without delving into javascript. If not, I would greatly
    appreciate some help at deciphering this odd behavior.
    Many thanks!
    Ivica Ico Bukvic, D.M.A.
    Composition, Music Technology, CCTAD, CHCI
    Virginia Tech
    Dept. of Music - 0240
    Blacksburg, VA 24061
    (540) 231-1137
    (540) 231-5034 (fax)
    ico@vt.edu
    http://www.music.vt.edu/people/faculty/bukvic/