Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

The title probably sucks because this is a really hard question to word properly, so I apologize for that.

Basically I have these two variables: moveSpeed and moveDirection. I'm using these to move an object at that speed in that direction. However, I want to find out how much of that speed is going in another direction.

So, for example

moveSpeed=10
moveDirection=80
otherSpeed=x
otherDirection=70

We have an object moving 80 degrees at 10 pixels per frame. How much of that speed is it moving 70 degrees? Also I realize (and expect) that this number will be negative when the direction is on the opposite side of the circle.

Also, here's a picture to make a little more sense.

Common sense tells us the speed is ~9 since the other direction is very close to the initial direction.

And if I still did too terrible of a job explaining, some static examples:

With 10 moveSpeed in 0 moveDirection, I'd get -10 otherSpeed at 180 otherDirection

With 10 moveSpeed in 0 moveDirection, I'd get 0 otherSpeed at 90 otherDirection

With 10 moveSpeed in 0 moveDirection, I'd get 5 otherSpeed at 45 otherDirection

So, what formula would I plug moveSpeed, moveDirection, and otherDirection into to get otherSpeed?

share|improve this question
    
Your intuition is pretty good, except that you would get otherSpeed of $10\cos45^\circ\approx7.071$ at otherDirection = 45. Imagine walking $10$ units along the diagonal of a $10$ unit square; you get farther than halfway. –  Rahul Jan 9 '13 at 19:21

1 Answer 1

up vote 1 down vote accepted

What you are looking for is called the projection formula. If you have a vector A and you want to find its projection vector on vector B then you use $S= {{A\cdot B}\over {B\cdot B}} B$. Here $A$ is your original velocity vector $A=10(\cos80, \sin80)$. And $B$ is your new direction $B=(\cos70,\sin70)$. And the dot product is defined by $A\cdot B=a_1b_1+a_2b_2=10(\cos80\cos70+\sin80\sin70)$, which using a formula from trig is $10\cos(80-70)=10\cos10$. Your $B \cdot B$ is just $1$, as $B$ is a unit vector. So your velocity in the new direction is $10\cos10(\cos70,\sin70)$. Your speed in that direction is $10\cos10$.

share|improve this answer
    
So, in its simplest form, the formula I'd use would be moveSpeed*cos(moveDirection-otherDirection)? I ask since I'm using this in a program and it'd just be better for everything to use the simplest maths possible. Sorry for taking so long to respond also; I've been trying to wrap my head around your answer since I'm far from skilled in math ^^' –  fay Jan 9 '13 at 19:37
    
@fay Yes. That is the formula for the signed length of the projection. So a negative answer means backing up speed. –  Maesumi Jan 9 '13 at 19:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.