views:

201

answers:

3

Math escapes me today.

How do I find the X speed and the Y speed of an object if it is going at a defined speed (say, 5 pixels/second) at a 45 degree angle?

+1  A: 

At a 45 degree angle, an object is going sqrt(2)/2 of the speed along each axis. Generally, you can do it with sin and cosine, but for specific angles like this you can do it just by knowing pythagorean triangles.

In a right triangle, the square of the hypotenuse is equal to the sum of the squares of the other two sides. You know the hypotenuse is V. You also know that the other two sides equal each other. That means that V^2 = Vx^2 * 2. This means that Vx = sqrt(V^2/2), which equals V * sqrt(1/2).

Paul Tomblin
+1  A: 

It will be Vx=VCos@
Vy=Vsin@
So in your case it will be Vx=5*cos45 and Vy=5*sin45
At 45 angle value of Cos & Sin is same i.e 1/root 2.

Note: If you are doing any math stuff in programming then have a look at Vecmath lib.

Khangharoth
+11  A: 

So always 5 pixels/sec and always 45 degrees?

The general case is

 velx=cos(a)*vel;
 vely=sin(a)*vel;

a is angle, usually in radians, so convert from degrees, and the signs (positive/negative) will depend on your coordinate system.

Crazy fact from the 1980s: In the old days, we used lookup tables for sin and cos!


Edited: Made my axes more conventional thanks to comment below. x is positive to your right. y is positive up. 45 degrees is to the northeast. If you have something else, let me know.

Nosredna
+1 for he most general case
Billy ONeal
Be careful about your angle convention here -- this answer is only right if the angle is measured clockwise from the Y-axis, which is unconventional. With the more usual convention of measuring the angle counterclockwise from the X-axis, you need to switch your sin and cos.
Adam Rosenfield
Thanks Adam, fixed.
Nosredna
I think some 3D engines or libraries are STILL using lookup for sin and cos, since they are Really calculation intensive to look for the answer in real time. And compare to old days, the cost to store the whole table is much lower today.
xandy
Well, our circles had angles in the range 0-255 so we could wrap them around with an "and" (or actually just a byte add where the overflow was lost). And of course, we used the same table for sin and cos, just with an offset. Or sometimes we returned a scaled sin(x) in the low byte and a scaled cos(x) in the low byte and just did one lookup.
Nosredna