dcsimg
A Flash Developer Resource Site

Results 1 to 12 of 12

Thread: as3 mathmatic help!

  1. #1
    talk to the hand! Never_land:('s Avatar
    Join Date
    Nov 2007
    Location
    your mother's bedroom
    Posts
    490

    as3 mathmatic help!



    I'm trying to make the red ball(ball_mc) stay in that line between a1 & a2
    and I want is position to be relative to the mouseX,MouseY in a perpendicular way(90 degrees).
    please help!

  2. #2
    Senior Member realMakc's Avatar
    Join Date
    Oct 2002
    Posts
    916
    here you go.
    who is this? a word of friendly advice: FFS stop using AS2

  3. #3
    talk to the hand! Never_land:('s Avatar
    Join Date
    Nov 2007
    Location
    your mother's bedroom
    Posts
    490
    Can you give me some code example.

  4. #4
    Senior Member
    Join Date
    May 2006
    Location
    Manhattan
    Posts
    246
    PHP Code:
    public static function getClosestPointOnLineSegmenta:Pointb:Pointpoint:Point ) : Point
    {
        
        
    //vector pointing from a to b
        //all valid points on the given line segment satisfy a + diff * t for 0 <= t <= 1
        
    var diff:Point b.subtract);
        
    //vector pointing from a to point
        
    var pa:Point point.subtract);
        
    //project the point onto diff- divide by the squared magnitude of diff to solve for t
        
    var t:Number = ( diff.pa.diff.pa.) / ( diff.diff.diff.diff.);
        
    //working with a line segment, ensure t doesn't exceed legal limits
        
    Math.maxMath.min1), );
        
        return new 
    Pointa.diff.ta.diff.);
        

    a and b are the 2 endpoints of the line segment, point is the center of your circle. you'll have to import flash.geom.Point.

    the link realMakc posted should be sufficient to understand the algorithm.

  5. #5
    talk to the hand! Never_land:('s Avatar
    Join Date
    Nov 2007
    Location
    your mother's bedroom
    Posts
    490
    thanks, sorry, I'm new with the package thing.
    I saved your code in ".as" file as linear.as
    ___________________
    1012: The static attribute may be used only on definitions inside a class.

    var th_class:linear= new linear()
    addChild(th_class)

    any help.

  6. #6
    Senior Member
    Join Date
    May 2006
    Location
    Manhattan
    Posts
    246
    if you're just writing the function in Flash, lose public and static (just keep function)

  7. #7
    Senior Member realMakc's Avatar
    Join Date
    Oct 2002
    Posts
    916
    newblack of course you do realize that by giving code you do not help... /unsub
    who is this? a word of friendly advice: FFS stop using AS2

  8. #8
    talk to the hand! Never_land:('s Avatar
    Join Date
    Nov 2007
    Location
    your mother's bedroom
    Posts
    490
    I learn better in actionscript.

  9. #9
    Senior Member
    Join Date
    May 2006
    Location
    Manhattan
    Posts
    246
    Quote Originally Posted by realMakc
    newblack of course you do realize that by giving code you do not help... /unsub
    that's pretty insulting considering i tried my best to comment the code such that it explains why and how it works...
    Last edited by newblack; 04-22-2008 at 06:50 PM.

  10. #10
    talk to the hand! Never_land:('s Avatar
    Join Date
    Nov 2007
    Location
    your mother's bedroom
    Posts
    490
    It took me some time but I understand it now.
    THANKS NEWBLACK!

  11. #11
    Member
    Join Date
    Feb 2010
    Posts
    59
    you want the ball to change as the mouse's x position changes.

    ball_mc.x=mouse.x;

    but you want it in-between a1 and a2 which are have pixel locations, that you can find.

    a1.x=pixel location#
    a1.y=pixel location#

    a2.x=pixel location#
    a2.y= pixel location#

    Your code may have somethig like this.

    if(mouse.x<=a2.x){...}
    else if(mouse.x>=a1.x){...}
    else{}

    Also, you may want to look at slope intercep form of a line:

    y=mx+b

    Google it

  12. #12
    Member
    Join Date
    Feb 2010
    Posts
    59
    You can work out the bugs of this. I did it quickly:
    var p=element ("dot");
    var p1=element ("point1");
    var p2=element ("point2");

    //y=mx+b (equation of a line)

    //slope = rise/run



    stage.addEventListener(Event.ENTER_FRAME, move)

    function move(evt:Event):void
    {
    var slope:Number = ((p2.y)-(p1.y))/((p2.x)-(p1.x))

    p.x=mouseX



    p.y = slope * p.x +400
    }

    the "+400" part should vary
    Last edited by Suthers; 04-29-2010 at 07:16 PM.

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