A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: 3D Box trigonometry question

  1. #1
    Bob (the singing sock)
    Join Date
    Aug 2000
    Location
    Århus, Denmark
    Posts
    472

    3D Box trigonometry question

    Hey

    I just checked out an excellent 3D tut here
    http://www.codylindley.com/Tutorials/trigonometry/

    I'm trying to point out the 8 x and y postitions in a box ...
    Perspective depth is not important in my case.
    I do need to incorporate a viewing angle (see attached trig.gif)

    any help out there?

    Kind Regards
    Poden

    ******** AS *********
    Code:
    // you'll need to make a linked clip named 'dot'
    function attBox(){
     var distance = 200;  // perspective
     var width = 150;  // box width
     var height= 200;  // box height
     var depth = 50;   // box depth
     // it takes 8 (x,y,x points) to make a box
     arrX = [width,width,width,width,0,0,0,0]  // xpos
     arrY = [height,height,0,0,height,height,0,0] // ypos
     arrZ = [depth,0,depth,0,depth,0,depth,0]  // zpos
     for(var i=0;i<arrX.length;i++){     // loop
      _root.attachMovie("dot",["dot"+i],i) // attaching mc 'dot'
      _mc = _root["dot"+i]     // variable mc name
      var x = arrX[i];
      var y = arrY[i];
      var z = arrZ[i];
      var perspective_ratio = distance/(distance+z)
      var perspective_x = x * perspective_ratio;
      var perspective_y = y * perspective_ratio;
      _mc._x = perspective_x;
      _mc._y = perspective_y;
     }
    }
    attBox()
    ******** AS *********
    Attached Images Attached Images

  2. #2
    Senior Member tigersbite's Avatar
    Join Date
    Apr 2002
    Posts
    314
    Here's a work-in-progress that may help you out.
    I have movie clip in the library containing a 5x5 black circle and a dynamic text field named "mytext".


    var width = 100;
    var height = 100;
    var depth = 100;

    var angle = {x: 0, y: 0};

    var tempMouse = {x:0, y:0};

    var dragFlag = false;

    initialize();

    function initialize() {

    corner = new Array(8);
    corner[0] = new point(-width/2, height/2, depth/2);
    corner[1] = new point(-width/2, height/2, -depth/2);
    corner[2] = new point(width/2, height/2, -depth/2);
    corner[3] = new point(width/2, height/2, depth/2);
    corner[4] = new point(-width/2, -height/2, depth/2);
    corner[5] = new point(-width/2, -height/2, -depth/2);
    corner[6] = new point(width/2, -height/2, -depth/2);
    corner[7] = new point(width/2, -height/2, depth/2);


    _root.createEmptyMovieClip("box",1);
    box._x = 75;
    box._y = 75;

    for (x=0; x< corner.length; x++) {
    tempDot = box.attachMovie("dot","dot"+x,x+8);
    tempDot.myText = x;
    }

    setDots();
    }


    _root.onMouseMove = function() {
    if (dragFlag) {
    angle.y = _xmouse-tempMouse.x;
    angle.x = tempMouse.y-_ymouse;
    tempMouse = {x:_xmouse, y:_ymouse};
    rotatePoints();
    setDots();
    updateAfterEvent();
    }

    }


    _root.onMouseDown = function() {
    dragFlag = true;
    tempMouse = {x:_root._xmouse, y:_root._ymouse};
    }


    _root.onMouseUp = function() {
    dragFlag = false;
    }


    function point(x, y, z) {
    this.x = x;
    this.y = y;
    this.z = z;
    }


    function rotatePoints() {
    for (x=0; x<corner.length; x++) {
    tempRotate = rotate(corner[x].x,corner[x].z,angle.y);
    corner[x].x = tempRotate.x;
    corner[x].z = tempRotate.y;
    tempRotate = rotate(corner[x].z,corner[x].y,angle.x);
    corner[x].z = tempRotate.x;
    corner[x].y = tempRotate.y;
    }
    }

    function rotate(x,y,ang) {
    tempAngle = Math.atan2(y,x) -ang/180*Math.PI;
    tempRadius = Math.sqrt((x*x)+(y*y));
    return {x:tempRadius*Math.cos(tempAngle), y:tempRadius*Math.sin(tempAngle)};
    }

    function setDots() {
    for (x=0;x < corner.length; x++) {
    box["dot"+x]._x = corner[x].x;
    box["dot"+x]._y = corner[x].y;
    }
    }

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