A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Motion by angle?

  1. #1
    Junior Member
    Join Date
    Aug 2004
    Posts
    15

    Motion by angle?

    Hello everyone,

    I am having a problem in trying to come up with a code to get a movie clip (in the center of the stage with an instance name of "mTarget") and another movie clip (instance named "mEnemy").

    The purpose of the mEnemy is to appear from a random bearing (0-360 deg from the mTarget) more or less like a missile fired from an aircraft and come right at the mTarget and after passing the mTarget (about 50pixels) make the mEnemy disappear (this can be done with the removeEventListener) but the randomized location of the mEnemy and to take on an angle motion (in relation to the mTarget) directly at the mTarget is what's killing me.

    I have tried different codes I have searched in here and from books but they don't really show on how to calculate the distance between them and use that to create the angle in which the mEnemy needs to take to come right at it. So, instead of posting some non-sense code that I don't even have a clue, I rather leave it to the experts to teach me the right way.

    (new to AS3 but rapidly learning.....well, except for this one headache)

    Any help would be appreciated!
    Last edited by salmerin; 07-09-2008 at 10:38 PM.

  2. #2
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    you need to convert to polar coordinates:

    PHP Code:
    //  calculate relative position from enemy to target
        
    var enemysRelativeX:Number mTarget.mEnemy.x;
        var 
    enemysRelativeY:Number mTarget.mEnemy.y;

    //  convert to polar coordinates
        //  arctan(y / x)
        
    var angleToTarget:Number Math.atan2(enemysRelativeYenemysRelativeX);
        
    //  √(x2 + y2)
        
    var distanceToTarget:Number Math.sqrt(enemysRelativeX enemysRelativeX enemysRelativeY enemysRelativeY);

    //  convert angle from radians to degrees
        
    angleToTarget *= (180 Math.PI); 
    Now you can use the Point.polar method to find the distance you need to move in any direction...so to move halfway toward the target:

    PHP Code:
    var amountToMove:Point Point.polar(distanceToTarget/2angleToTarget);
    mEnemy.+= amountToMove.x;
    mEnemy.+= amountToMove.y
    or for a decaying orbit, 10% closer and 5 degrees offset:

    PHP Code:
    var amountToMove:Point Point.polar(distanceToTarget .9angleToTarget 5); 

  3. #3
    Junior Member
    Join Date
    Aug 2004
    Posts
    15
    I will give this a try once I come back from work today, thank you for your prompt reply Neznein9! I will post back if everything worked fine or if it didn't!

  4. #4
    Junior Member
    Join Date
    Aug 2004
    Posts
    15
    This is what I typed out from what you gave me:
    Code:
    //  calculate relative position from enemy to target
    var enemysRelativeX:Number = mTarget.x - mEnemy.x;
    var enemysRelativeY:Number = mTarget.y - mEnemy.y;
    
    //  convert to polar coordinates
    //  arctan(y / x)
    var angleToTarget:Number = Math.atan2(enemysRelativeY, enemysRelativeX);
    trace(angleToTarget);
    //  √(x2 + y2)
    var distanceToTarget:Number = Math.sqrt(enemysRelativeX * enemysRelativeX + enemysRelativeY * enemysRelativeY);
    
    //  convert angle from radians to degrees
    angleToTarget *= (180 / Math.PI);
    trace(angleToTarget);
    
    this.addEventListener(Event.ENTER_FRAME, moveToTarget);
    
    function moveToTarget(evt:Event):void{
    	
    	var amountToMove:Point = Point.polar(distanceToTarget, angleToTarget);
        //var amountToMove:Point = Point.polar(distanceToTarget * .9 , angleToTarget - 5);
    	mEnemy.x += amountToMove.x;
    	trace(amountToMove.x);
    	mEnemy.y += amountToMove.y;
    	trace(amountToMove.y);
    
    }
    Now I have two movie clips one having an instance name of mEnemy the other mTarget. mTarget is at about 45 degree angle from the mEnemy but the angle to target when executes it does not follow that same angle trajectory, instead it goes the the right (as if the angle of trajectory was 90 degrees) and off the screen.

    Am I messing things up? (dumb question I guess...I know I am....lol)

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