# How to calculate vector that is perpendicular to the direction of moving in Flash?

In flash i have a class that extends movieclip and i am moving it forward, up and down, and rotating it. How can i calculate the vector that is perpendicular to this movieclip?

## 2 Answers

Well as explained here it is pretty easy to get the perpendicular vector from the movement vector:

Let's say you have a boat that has cannons that fire to the left and right. Given that the boat is facing along the direction vector (2,1), in which directions do the cannons fire? This is easy in 2D: to rotate 90 degrees clockwise, just flip the two vector components, and then switch the sign of the second component. (a,b) becomes (b,-a). So, if the boat is facing along (2,1), the right-facing cannons fire towards (1,-2). The left-facing cannons fire in the opposite direction, so we flip both signs to get: (-1,2).

So you just need to calculate the direction vector and then do what they said. Now how exactly you should calculate the direction vector will depend on your specific implementation; since you haven't provided us any code I'm not sure what you are doing.

However I'll give an explanation based on one common implementation; you simply store the scalar speed of the moving object, and every frame calculate a movement vector based on that speed and the rotation. In that case the movement vector is calculated using simple trigonometry, something like:

x = cos(rot) * speed
y = sin(rot) * speed
movement = [x, y]


In 3D, I'd just take the cross product of the movement vector and the up vector.

• The OP is extending Flashs MovieClip, so this is definitely 2D space. – bummzack Oct 11 '11 at 16:16
• @bummzack: True, I was just pointing things out how I'd do it in 3D. – Tili Oct 12 '11 at 11:28