A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: [help] direction calculation

  1. #1
    Professional Bodger
    Join Date
    Aug 2003
    Location
    behind you!
    Posts
    89

    [help] direction calculation

    hi,

    ive searched abit for this and havent been successful in finding results...

    im trying to figure out the code for making a MC rotate and face another MC, this is for racing game AI. im going to use the waypoint system.

    well firstly what pops up in my head of how to do this is as follows

    Code:
    acc_x = Math.cos((Math.PI*this._rotation)/180);
    acc_y = Math.sin((Math.PI*this._rotation)/180);
    i seriously have no hope from that, but im guessing that you have to firstly find the x and y of both MC's then ya gotta find the angle between them with the code i have shown. and then somehow make the mc rotate at a certain speed until its facing the other MC.

    could someone please help me, just explain how i would go about it, perhaps not hand it to me on a silver platter with ALL the code, more like explain to me how it works with lil samples on the hard bits, cos beleive it or not im actually getting good at AS lol...
    i hope im not asking alot.

    thanx in advance
    I reject your reality and Substitute my own!

  2. #2
    n00b LeechmasterB's Avatar
    Join Date
    May 2004
    Location
    Switzerland
    Posts
    1,067
    Self explaining (I hope):
    Code:
    function rotateAtoB(objectA, objectB) {
    	var directionVectorY = objectA._y-objectB.y;
    	var directionVectorX = objectA._x-objectB.x;
    	var rotation_angle = Math.round(Math.atan2(-directionVectorX, directionVectorY)/Math.PI*180);
    	_rotation = rotation_angle;
    }
    I do stuff that does stuff...

    J-Force

  3. #3
    Professional Bodger
    Join Date
    Aug 2003
    Location
    behind you!
    Posts
    89
    oh wow, nice one thanks.

    just for anyone that don't know (like i used to not! hehe) i'll post the code which also makes this MC move in direction of the second one, also integrating LeechMasterB's code (with anotations)(this is all in the movieclip of "objectA"):

    Code:
        onClipEvent(load){
            dirX = this._x - _root.objectB._x;
    	dirY = this._y - _root.objectB._y;
    	acc_x = 0;
    	acc_y = 0;
    	vel_x = 0;
    	vel_y = 0;
        }
    Code:
        onClipEvent(enterFrame){
            //using math the find where object should be pointing. using the dirX, dirY in the load.
            var rotation_angle = Math.round(Math.atan2(-dirX, dirY)/Math.PI*180);
    	_rotation = rotation_angle-90; //pointing object in that direction.
    	acc_x = Math.cos((Math.PI*this._rotation)/180);//defining two values to
    	acc_y = Math.sin((Math.PI*this._rotation)/180);//add a speed to, to make it move.
    	vel_x += acc_x/10;//adding speed
     	vel_y += acc_y/10;//to it
    	
    	this._x += vel_x;//finally making
    	this._y += vel_y;//the object move.
    }

    if there's any better ways to do this, please feel free to post other ways

    thankyou LeechMasterB
    I reject your reality and Substitute my own!

  4. #4
    Junior Senior
    Join Date
    Nov 2000
    Location
    sydney
    Posts
    565
    reposting this old chestnut
    Code:
    // generic re-usable 2d Trig functions
    
    Math.DEG2RAD=0.01745329252;
    Math.RAD2DEG=57.295779513;
    
    function ang2D(xDistance,yDistance){
    	//
    	// used as: ang2D(targetX-thisX, targetY-thisY)
    	// returns degrees with this range and orientation -
    	//
    	//           0
    	//
    	// -90/270   +   90
    	//
    	//          180
    	//
    	with(Math){
    		flip=0;
    		if(yDistance>=0) flip=PI;
    		return RAD2DEG*(flip-atan(xDistance/yDistance));
    	}
    }
    
    function turn(currentAngle,preferredAngle,maxTurnSpeed)
    {
    	//
    	// prevents an object turning 'the long way' between angles such as
    	// 10 to 350 or 170 to -170 and clips the result at +/- maxTurnSpeed
    	//
    	t=preferredAngle-currentAngle;
    	while(t>180)t-=360;
    //    with the ranges of the ang2D() function in preferredAngle and _rotation in currentAngle
    //    (-90 to 270 versus -180 to 180) we can omit the line below	
    //	while(t<-180)t+=360;
    //
    	if (t < -maxTurnSpeed)return -maxTurnSpeed;
    	if (t > maxTurnSpeed)return maxTurnSpeed;
    	return t;
    }
    
    
    function xvel(angle) {
    	//xvel(90) returns ~1.0
    	with (Math){
    		return sin(angle*DEG2RAD);
    	}
    }
    
    function yvel(angle) {
    	//yvel(180) returns ~1.0
    	with (Math){
    		return 0-cos(angle*DEG2RAD);
    	}
    }
    Signature

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