A Flash Developer Resource Site

1. ## [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.

2. 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;
}```

3. 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_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

4. reposting this old chestnut
Code:
```// generic re-usable 2d Trig functions

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;
}
}

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){
}
}

function yvel(angle) {
//yvel(180) returns ~1.0
with (Math){
}
}```

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 » Home » Movies » Tutorials » Submissions » Board » Links » Reviews » Feedback » Gallery » Fonts » The Lounge » Sound Loops » Sound FX » About FK » Sitemap