# Trig, speed and direction

**URL:** <https://forum.kirupa.com/t/trig-speed-and-direction/314469>\
**Category:** Uncategorized\
**Created:** [September 29, 2010, 2:53pm UTC](https://forum.kirupa.com/t/trig-speed-and-direction/314469 "2010-09-29T14:53:14Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![tbo](https://avatars.discourse-cdn.com/v4/letter/t/7feea3/32.png) [@tbo](https://forum.kirupa.com/u/tbo)\
**Post date:** [September 29, 2010, 2:53pm UTC](https://forum.kirupa.com/t/trig-speed-and-direction/314469/1 "2010-09-29T14:53:14Z")

</div>

using trig, when moving an object based on its current rotation, how does one “detect” if the object is currently moving to\*\* its own\*\* left or right direction?  
example:  
right side up: left moves the car to its left, which is left.  
upside down(180 rotation): left moves it the car to **its** left, which is right.  
based on movement alone, how can i say its moving to its own oriented left or its right?

here is an example:  
paste into frame 1 of a new fla.

```auto

addEventListener(Event.ENTER_FRAME, go);
stage.addEventListener(KeyboardEvent.KEY_DOWN,keysDown);
stage.addEventListener(KeyboardEvent.KEY_UP,keysUp);

var vx:Number=0;
var vy:Number=0;
var radians:Number;
var speed:Number=0;
var left:Boolean;
var right:Boolean;

//create "car"
var car:Sprite = new Sprite();
car.graphics.lineStyle(3,0x00ff00);
car.graphics.beginFill(0x0000FF);
car.graphics.drawRect(0,0,70,25);
car.graphics.endFill();
car.graphics.drawCircle(10, 30, 5);
car.graphics.drawCircle(60, 30, 5);
addChild(car);

//create "instructions"
var txt:TextField = new TextField();  
txt.width = 250;  
txt.x = 25;  
txt.y = 300;  
txt.text = "left arrow : LEFT";
txt.appendText("
"+"Right arrow: RIGHT");
txt.appendText("
"+"Up arrow: stop and rotation+=10");
addChild(txt);

//game Loop
function go(e:Event) {

    if (left==true) {
        speed=-1;
    } else if (right==true) {
        speed=1;
    } else {
         speed=0;
    }

    radians=car.rotation*Math.PI/180;
     vx+=speed*Math.cos(radians);
    vy-=speed*Math.sin(radians);
     car.x+=vx;
    car.y-=vy;

}
function keysDown(e:KeyboardEvent) {
    if (e.keyCode==37) {
         left=true;
        right=false;
    }
    if (e.keyCode==39) {
         right=true;
        left=false;
    }
    if (e.keyCode==38) {
        car.rotation+=10;
         radians=car.rotation*Math.PI/180;
        //var currentSpeed:Number=Math.sqrt(vx*vx+vy*vy);
         //vx=currentSpeed*Math.cos(radians);
        //vy=- currentSpeed*Math.sin(radians);
        vx=0;
        vy=0;
     }
}
function keysUp(e:KeyboardEvent) {

    if (e.keyCode==37) {
        left=false;
    }
    if (e.keyCode==39) {
        right=false;
    }
}

```
