A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: [RESOLVED] position a textbox a certain distance from an object at a particular angle

  1. #1
    Senior Member
    Join Date
    Apr 2000
    Location
    Minneapolis
    Posts
    2,127

    resolved [RESOLVED] position a textbox a certain distance from an object at a particular angle

    Hi,
    I have some texboxes which I need to stay a fixed distance away from another object at an angle.

    so I need to say textbox_mc position is 10 pixels away from my_Object at an angle of 35 degrees.

    The angle is changing and the object is moving, I think I can handle that if I know how to position by angle and distance.

    thanks
    mark

  2. #2
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    Keep your position in polar (radius, angle) - then just convert to cartesian when you're ready to position on screen...

    PHP Code:
    var boxPosition:Object = {theta:35radius:10};
    setPosition(myTextboxboxPosition);

    function 
    setPosition(tx:TextFieldpolar:Object):void{
        
    tx.polar.radius Math.cos(polar.theta);
        
    tx.polar.radius Math.sin(polar.theta);

    Please use [php] or [code] tags, and mark your threads resolved 8)

  3. #3
    Senior Member
    Join Date
    Apr 2000
    Location
    Minneapolis
    Posts
    2,127
    Hi,
    thanks for the reply.
    I tried implementing that. It sort of works except my textbox orbits my object as it moves.
    I am getting really desperate, this project has taken WAY too long.
    I have come so far, but I am failing near the finish.
    could you find time to look at my code?
    Each textbox should stay inside the angle it represents as the dots are dragged.
    The code is below, but I am also attaching a fla
    my attempt at positioning is at line 245
    I really am begging for help at this point!!

    you can see the project here
    http://gasolicious.com/flashstuff/ki...%20parent.html
    A graphic guide to the terms I use is here, some comments are below the image
    http://gasolicious.com/flashstuff/layout.html

    please help
    Mark
    PHP Code:
    var item:MovieClip;
    var 
    dragOffsetX:int
    var 
    dragOffsetY:int
    var 
    itemOffsetXY:int;
    var 
    stageW:int 800;
    var 
    stageH:int 650;
    var 
    dotHalf:Number kite_mc.dot1_mc.width/2;
    var 
    bSpace:int 1;
    var 
    tSpace:int 2;
    var 
    angleTextOffset:Number kite_mc.dot1_mc.width;


    //add event listeners to all dots
    kite_mc.dot1_mc.addEventListener(MouseEvent.MOUSE_DOWNonItemDragStart);
    kite_mc.dot2_mc.addEventListener(MouseEvent.MOUSE_DOWNonItemDragStart);
    kite_mc.dot3_mc.addEventListener(MouseEvent.MOUSE_DOWNonItemDragStart);
    kite_mc.dot4_mc.addEventListener(MouseEvent.MOUSE_DOWNonItemDragStart);
    kite_mc.dot5_mc.addEventListener(MouseEvent.MOUSE_DOWNonItemDragStart);

    function 
    onItemDragStarte:MouseEvent ):void 
        
    item e.target.parent as MovieClip;
        
        
    dragOffsetX item.item.parent.mouseX
        
    dragOffsetY item.item.parent.mouseY
        
    itemOffsetXY item.width 2;
        
            switch (
    item.name) {
        case 
    "dot1_mc" :
            
    trace("case 1");
            
    stage.addEventListenerMouseEvent.MOUSE_MOVEdragItem1); 
            break; 
        case 
    "dot2_mc" :
            
    trace("Case 2");
            
    stage.addEventListenerMouseEvent.MOUSE_MOVEdragItem24);
            break;
        case 
    "dot3_mc" :
            
    trace("Case 3");
            
    stage.addEventListenerMouseEvent.MOUSE_MOVEdragItem3);
            break;
        case 
    "dot4_mc" :
            
    trace("Case 4");
            
    stage.addEventListenerMouseEvent.MOUSE_MOVEdragItem24);
            break;
        case 
    "dot5_mc" :
            
    trace("Case 5");
            
    stage.addEventListenerMouseEvent.MOUSE_MOVEdragItem5);
            break;
        }
          
        
    stage.addEventListenerMouseEvent.MOUSE_UPonItemDragStop );

    }

    //drag 1 mirror movement to 3 and x movement to 5
    function dragItem1(e:MouseEvent):void{
        var 
    topLeft:Point item.parent.globalToLocal( new Point0) );
        var 
    bottomRight:Point item.parent.globalToLocal( new PointstageWstageH ) );
        
    item.Math.minMath.maxitem.parent.mouseX dragOffsetXtopLeft.itemOffsetXY ), bottomRight.itemOffsetXY );
        
    item.Math.minMath.maxitem.parent.mouseY dragOffsetYtopLeft.itemOffsetXY ), bottomRight.itemOffsetXY );
        
    kite_mc.dot3_mc.item.x;
        
    kite_mc.dot3_mc.item.* -1;
        
    kite_mc.dot5_mc.item.x;
        
    loop();
    }

    //drag 2 or 4 restrict movement to X plane
    function dragItem24(e:MouseEvent):void{
        var 
    topLeft:Point item.parent.globalToLocal( new Point0) );
        var 
    bottomRight:Point item.parent.globalToLocal( new PointstageWstageH ) );
        
    item.Math.minMath.maxitem.parent.mouseX dragOffsetXtopLeft.itemOffsetXY ), bottomRight.itemOffsetXY );
        
    loop();
    }

    //drag 3 mirror movement to 1 and x movement to 5
    function dragItem3(e:MouseEvent):void{
        var 
    topLeft:Point item.parent.globalToLocal( new Point0) );
        var 
    bottomRight:Point item.parent.globalToLocal( new PointstageWstageH ) );
        
    item.Math.minMath.maxitem.parent.mouseX dragOffsetXtopLeft.itemOffsetXY ), bottomRight.itemOffsetXY );
        
    item.Math.minMath.maxitem.parent.mouseY dragOffsetYtopLeft.itemOffsetXY ), bottomRight.itemOffsetXY );
        
    kite_mc.dot1_mc.item.x;
        
    kite_mc.dot1_mc.item.* -1;
        
    kite_mc.dot5_mc.item.x;
        
    loop();
    }
    //****************
    //drag 5 move the whole parent kite? not working
    function dragItem5(e:MouseEvent):void {
        var 
    topLeft:Point=item.parent.globalToLocal(new Point(0,0));
        var 
    bottomRight:Point=item.parent.globalToLocal(new Point(stageWstageH));
        
    item.x=Math.min(Math.max(item.parent.mouseX+dragOffsetX,topLeft.x+itemOffsetXY),bottomRight.x-itemOffsetXY);
        
    item.y=Math.min(Math.max(item.parent.mouseY+dragOffsetY,topLeft.y+itemOffsetXY),bottomRight.y-itemOffsetXY);
        
    //
        
    var newPoint:Point=item.localToGlobal(new Point(item.xitem.y));
        
    kite_mc.newPoint.x;
        
    kite_mc.newPoint.y;
        
        
    loop();
    }

    //draw kite one last time just in case fast mouse movement gets dot ahead of line
    //remove all possible move move listeners
    function onItemDragStope:MouseEvent ):void {
        
    loop();
        
    stage.removeEventListenerMouseEvent.MOUSE_MOVEdragItem1 );
        
    stage.removeEventListenerMouseEvent.MOUSE_MOVEdragItem24 );
        
    stage.removeEventListenerMouseEvent.MOUSE_MOVEdragItem3 );
        
    stage.removeEventListenerMouseEvent.MOUSE_MOVEdragItem5 );
        
    stage.removeEventListenerMouseEvent.MOUSE_UPonItemDragStop ); 
    }

    function 
    radToDeg(n:Number):Number {
          return 
    Math.PI 180;
        }
    function 
    degToRad(n:Number):Number {
          return 
    Math.PI 180;
        }
    function 
    rd(n:Number):Number {
          return 
    Math.round(100) / 100;
        }

    //draw and fill the kite
    //calculate all distances and angles (eventually)
    function loop():void{
    //clear lines and fill
        
    kite_mc.graphics.clear();
    //create and define point variables
        
    var a:Point = new Point(kite_mc.dot1_mc.xkite_mc.dot1_mc.y);
        var 
    b:Point = new Point(kite_mc.dot2_mc.xkite_mc.dot2_mc.y);
        var 
    c:Point = new Point(kite_mc.dot3_mc.xkite_mc.dot3_mc.y);
        var 
    d:Point = new Point(kite_mc.dot4_mc.xkite_mc.dot4_mc.y);
        var 
    f:Point = new Point(kite_mc.dot5_mc.xkite_mc.dot5_mc.y);
        
    //var a1:Point = new Point(
        
        
    var vector1:Object = {xb.a.xy:  b.a.y};
        var 
    vector2:Object = {xc.b.xy:  c.b.y};
        var 
    vector3:Object = {xd.c.xy:  d.c.y};
        var 
    vector4:Object = {xa.d.xy:  a.d.y};
        var 
    vector5:Object = {xa.f.xy:  a.f.y};
        var 
    vector6:Object = {xc.f.xy:  c.f.y};
        var 
    vector7:Object = {xb.f.xy:  b.f.y};
        var 
    vector8:Object = {xd.f.xy:  d.f.y};
        
        var 
    dotProductBAF:Number vector1.vector5.vector1.vector5.y;//angle1
        
    var dotProductABF:Number vector1.vector7.vector1.vector7.y;//angle2
        
    var dotProductCBF:Number vector2.vector7.vector2.vector7.y;//3
        
    var dotProductFCB:Number vector6.vector2.vector6.vector2.y;//4
          
        
    var dotProductDCF:Number vector3.vector6.vector3.vector6.y;//5
        
    var dotProductCDF:Number vector3.vector8.vector3.vector8.y;//6
        
    var dotProductADF:Number vector4.vector8.vector8.vector8.y;//7
        
    var dotProductDAF:Number vector4.vector5.vector4.vector5.y;//8
        
        
    var dotProductAFD:Number vector5.vector8.vector5.vector8.y;//9

    //create angle variables
        
    var angle1:Number;
        var 
    angle2:Number;
        
          var 
    magBA Point.distance(ba);//v1
          
    var magBC Point.distance(bc);//v2
          
    var magCD Point.distance(cd);//v3
          
    var magDA Point.distance(da);//v4
          
    var magAF Point.distance(af);//v5
          
    var magCF Point.distance(cf);//v6
          
    var magBF Point.distance(bf);//v7
          
    var magDF Point.distance(df);//v8
          
          
    var angleBAF Math.acos(dotProductBAF / (magBA magAF));//angle1, v1 v5
          
    var angleABF Math.acos(dotProductABF / (magBA magBF));//2, v1 v7
          
    var angleCBF Math.acos(dotProductCBF / (magBC magBF));//3, v2 v7
          
    var angleFCB Math.acos(dotProductFCB / (magBC magCF));//4, v2 v6
          
          
    var angleDCF Math.acos(dotProductDCF / (magCD magCF));//5, v3 v6
          
    var angleCDF Math.acos(dotProductCDF / (magCD magDF));//6, v3 v8
          
    var angleADF Math.acos(dotProductADF / (magDA magDF));//7, v4 v8
          
    var angleDAF Math.acos(dotProductDAF / (magDA magAF));//8 , v4 v5
          
          
    var angleAFD Math.acos(dotProductAFD / (magAF magDF));//8, v5 v8
    ///////

          
    var angle_BAF int(180-rd(radToDeg(angleBAF)));//1
          
    var angle_ABF int(rd(radToDeg(angleABF)));//2
          
    var angle_CBF int(rd(radToDeg(angleCBF)));//3
          
    var angle_FCB int(rd(radToDeg(angleFCB)));//4
          
          
    var angle_DCF int(180-rd(radToDeg(angleDCF)));//5
          
    var angle_CDF int(rd(radToDeg(angleCDF)));//6
          
    var angle_ADF int(rd(radToDeg(angleADF)));//7
          
    var angle_DAF int(rd(radToDeg(angleDAF)));//8
          
          
    var angle_AFD int(rd(radToDeg(angleAFD)));//9

        
    //draw the lines and fill
        
    kite_mc.graphics.lineStyle(0);
        
    kite_mc.graphics.moveTo(a.x,a.y); ///This is where we start drawing
        
    kite_mc.graphics.beginFill(0x9DA8D0.3);
        
    kite_mc.graphics.lineTo(b.xb.y);
        
    kite_mc.graphics.lineTo(c.xc.y);
        
    kite_mc.graphics.lineTo(d.xd.y);
        
    kite_mc.graphics.lineTo(a.xa.y);
        
    kite_mc.graphics.endFill()
        
    kite_mc.graphics.lineTo(f.xf.y);
        
    kite_mc.graphics.lineTo(c.xc.y);
        
    kite_mc.graphics.moveTo(d.x,d.y);
        
    kite_mc.graphics.lineTo(f.xf.y);
        
    kite_mc.graphics.lineTo(b.xb.y);

    //write angles to textboxes and position
    //1
        
    kite_mc.angle1Text_mc.angle_txt.text angle_BAF.toString();
        
    //kite_mc.angle1Text_mc.x = kite_mc.dot1_mc.x -(dotHalf + tSpace);
        //kite_mc.angle1Text_mc.y = kite_mc.dot1_mc.y + (dotHalf - tSpace);
        
        
    //2 and 3
        
    kite_mc.angle2Text_mc.angle_txt.text kite_mc.angle3Text_mc.angle_txt.text angle_ABF.toString();
        
    kite_mc.angle2Text_mc.kite_mc.dot2_mc.dotHalf;
        
    kite_mc.angle2Text_mc.kite_mc.dot2_mc.-(dotHalf bSpace);
        
    kite_mc.angle3Text_mc.kite_mc.dot2_mc.dotHalf;
        
    kite_mc.angle3Text_mc.kite_mc.dot2_mc.tSpace;
    //4
        
    kite_mc.angle4Text_mc.angle_txt.text angle_FCB.toString();
        
    kite_mc.angle4Text_mc.kite_mc.dot3_mc.-(dotHalf tSpace);
        
    kite_mc.angle4Text_mc.kite_mc.dot3_mc.- (dotHalf tSpace);
    //5
        
    kite_mc.angle5Text_mc.angle_txt.text angle_DCF.toString();
        
    kite_mc.angle5Text_mc.kite_mc.dot3_mc.+dotHalf tSpace;
        
    kite_mc.angle5Text_mc.kite_mc.dot3_mc.- (dotHalf tSpace);
    //6 and 7
        
    kite_mc.angle6Text_mc.angle_txt.text kite_mc.angle7Text_mc.angle_txt.text angle_CDF.toString();
        
    kite_mc.angle6Text_mc.kite_mc.dot4_mc.-(dotHalf tSpace);
        
    kite_mc.angle6Text_mc.kite_mc.dot4_mc.dotHalf tSpace;
        
    kite_mc.angle7Text_mc.kite_mc.dot4_mc.-(dotHalf tSpace);
        
    kite_mc.angle7Text_mc.kite_mc.dot4_mc.- (dotHalf tSpace);
    //8
        
    kite_mc.angle8Text_mc.angle_txt.text angle_DAF.toString();
        
    kite_mc.angle8Text_mc.kite_mc.dot1_mc.+dotHalf tSpace;
        
    kite_mc.angle8Text_mc.kite_mc.dot1_mc.dotHalf tSpace;
    //9
        
    kite_mc.angle9Text_mc.angle_txt.text angle_AFD.toString();
        
    kite_mc.angle9Text_mc.kite_mc.dot5_mc.+dotHalf tSpace;
        
    kite_mc.angle9Text_mc.kite_mc.dot5_mc.- (dotHalf tSpace);
        
    //heres me trying to set the first angle text in place    
        
    var boxPosition1:Object = {theta:angle_BAF/2radius:angleTextOffset};
        
    setPosition(kite_mc.angle1Text_mcboxPosition1);

        function 
    setPosition(tx:MovieClippolar:Object):void{
           
    tx.kite_mc.dot1_mc.polar.radius Math.cos(polar.theta);
           
    tx.kite_mc.dot1_mc.polar.radius Math.sin(polar.theta);
    }



    //layout and enter values in distance textfields
        
    kite_mc.dist1_txt.text Math.round(magBA).toString();
        
    kite_mc.dist1_txt.= (a.b.x) / 2;
        
    kite_mc.dist1_txt.= (a.b.y) / 2;
        
        
    kite_mc.dist2_txt.text Math.round(magBC).toString();
        
    kite_mc.dist2_txt.= (b.c.x) / 2;
        
    kite_mc.dist2_txt.= (b.c.y) / 2;
        
        
    kite_mc.dist3_txt.text Math.round(magCD).toString();
        
    kite_mc.dist3_txt.= (c.d.x) / 2;
        
    kite_mc.dist3_txt.= (c.d.y) / 2;
        
        
    kite_mc.dist4_txt.text Math.round(magDA).toString();
        
    kite_mc.dist4_txt.= (d.a.x) / 2;
        
    kite_mc.dist4_txt.= (d.a.y) / 2;
        
        
    kite_mc.dist5_txt.text Math.round(magAF).toString();
        
    kite_mc.dist5_txt.= (a.f.x) / 2;
        
    kite_mc.dist5_txt.= (a.f.y) / 2;
        
        
    kite_mc.dist6_txt.text Math.round(magCF).toString();
        
    kite_mc.dist6_txt.= (c.f.x) / 2;
        
    kite_mc.dist6_txt.= (c.f.y) / 2;
        
        
    kite_mc.dist7_txt.text Math.round(magBF).toString();
        
    kite_mc.dist7_txt.= (b.f.x) / 2;
        
    kite_mc.dist7_txt.= (b.f.y) / 2;
        
        
    kite_mc.dist8_txt.text Math.round(magDF).toString();
        
    kite_mc.dist8_txt.= (d.f.x) / 2;
        
    kite_mc.dist8_txt.= (d.f.y) / 2;
    }

    //draw lines and fill first time.
    loop(); 
    Attached Files Attached Files

  4. #4
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    Whoops - try converting theta to radians before you take it's sin/cos.
    Please use [php] or [code] tags, and mark your threads resolved 8)

  5. #5
    Senior Member
    Join Date
    Apr 2000
    Location
    Minneapolis
    Posts
    2,127
    if you try it with this I get something vaguely right at first glance.
    as you move the top dot it drifts out of the desired location.
    I am way out of my math depth at this point.

    I am converting angle_BAF to radians
    is the angle angleBAF of use? about line 168.
    I don't want to be converting to degrees and then back again.

    Any advice on how to get the angle text to stay in the right spot inside the angle it refers to would be great, maybe my approach is all wrong
    here is where I am trying to get to, last item on the page "exploring kites interactiive"
    http://www.hotmaths.com.au/FreeActivities/
    PHP Code:
    function degToRad(n:Number):Number {
          return 
    Math.PI 180;
        }

    //heres me trying to set the first angle text in place
    //no idea why its +90 that seems vaguely right for a while, just guessed
        
    var rad1:Number degToRad(angle_BAF 90 /2);//see function line 114
        
    var boxPosition1:Object = {theta:rad1radius:angleTextOffset};
        
    setPosition(kite_mc.angle1Text_mcboxPosition1);

        function 
    setPosition(tx:MovieClippolar:Object):void{
           
    tx.kite_mc.dot1_mc.polar.radius Math.cos(polar.theta);
           
    tx.kite_mc.dot1_mc.polar.radius Math.sin(polar.theta);

    Last edited by mgason; 12-03-2009 at 06:05 PM.

  6. #6
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    Ok, take a look at the attached diagram - you have the size of the BAF angle (red arc). You want your text in the center of that (orange dot) so divide it in half.

    This is the angle you need, but it's not in the correct relation to Flash's own rotation system (counter-clockwise from the x axis). To get there, add 90° (or .5 radians - see the yellow arrow) to the angle - you'll need to tweak this for each angle, otherwise you'll always be below-left of each vertex.

    Now - convert that to cartesian to get your relative x,y position (note - this is relative to 0,0 - the top left of the screen). So you'll want to add the point you just generated to the tip of the kite (it looks like you're doing this already). Now your textfield's top-left corner should be right on the orange dot.

    Finally, correct for the size of the textfield by subtracting half of the textWidth from .x and half of the textHeight from .y - now your textfield should be perfectly centered on the orange dot.
    Attached Images Attached Images
    Please use [php] or [code] tags, and mark your threads resolved 8)

  7. #7
    Senior Member
    Join Date
    Apr 2000
    Location
    Minneapolis
    Posts
    2,127
    thanks very much for your help.
    I have to take a break, time to go to my flash user group
    is that a break :-)

    anyway been at it for almost 10 hours
    I will try this stuff tomorrow morning

    thanks again
    Mark

  8. #8
    Senior Member
    Join Date
    Apr 2000
    Location
    Minneapolis
    Posts
    2,127
    Hi,
    wanted to say thanks for giving me some geometry info and making me dig in myself and solve it.
    I did as you suggested and divided the angle and added compensation, then turned it back into radians.
    This was as you said different for every angle. It worked.

    I went to bed and couldn't sleep, my brain kept telling me that the fact you previously explained to me that the midpoint between 2 point is always
    Mymc.x = (a.x + b.x) / 2;
    Mymc.y = (a.y + b.y) / 2;
    was important.

    I realized that a new point at the midpoint between the 2 could give the angle in flash co-ordinate system without any adjustments being needed.

    finally I got up at 2am and wrote this, worked first time!
    1 function, works for every angle, would work in any triangle. Maybe someone else will find it useful
    Thanks again
    PHP Code:

    //I already have all points declared and have angles in degrees.

    /*function to locate the angles display text.
    accepts 3 points already declared in loop function that define displayed angle,
    these are the dot locations, p1 is the point where the angle will be displayed
    next, the movieclip that displays the angle text(kite_mc.angle1Text_mc etc..)
    last, the angle value to display calculated in loop function (var angle_BAC etc)*/
    function displayAngle(p1:Pointp2:Pointp3:Pointt:MovieClipda:int) {
        var 
    angle:Number;// angle to place text at from dot
        
    var textAdjust:int;//distance to place text at from dot

        //for clarity adjust distance from dot, when angle gets small move text further out
        
    if (da<textPos) {
            
    textAdjust textPos + (textPos 2) - (da 2);
        } else {
            
    textAdjust=textPos;
        }

        
    //get a point midway between 2 outer dots
        
    var hp:Point = new Point((p2.p3.x)/2, (p2.p3.y)/2);
        
    //calculate angle from point 1 to that mid point, this is center of displayed angle
        
    angle Math.atan2(p1.hp.yp1.hp.x) + (Math.PI);
        
    //use the angle and textAdjust distance to place the textbox
        
    var myPolar:Object={theta:angle,radius:textAdjust};
        
    t.x=p1.x+myPolar.radius*Math.cos(myPolar.theta);
        
    t.y=p1.y+myPolar.radius*Math.sin(myPolar.theta);

        
    //display the angle text in the textfield
        
    t.angle_txt.text=da.toString();
    }

    displayAngle(abckite_mc.angle1Text_mcangle_BAC); 

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