How do I calculate speed given two xy vectors?

I have some code that returns the x and y linear velocities of a moving space ship. How can I combine these to give me total speed?

x = self.player._box2dBody.GetLinearVelocity().x;
y = self.player._box2dBody.GetLinearVelocity().y;

So if:
x = 1.99
y = -1.63

How do I calculate the total speed?

• Look up magnitude. It's likely part of the vector class already, so you can probably use something like .magnitude instead of .x and .y.
– House
Aug 1 '13 at 2:13
• The actual formula is v = sqrt(x^2 + y^2), but follow Byte 56's advice and learn some basic linear algebra. Atan2(...) is also an extremely useful function in gamedev that you won't learn in most purely academic classes.
– mobo
Aug 1 '13 at 5:43

In this situation, the length of the vector would be considered the speed, since the vector itself represents the velocity (speed and direction). This is also referred to as the magnitude. The vector class you're getting .x and .y from likely already has a built in implementation for getting the magnitude or length. Where is the magnitude of the vector.

I suggest you read up on linear algebra. Wolfire has put together a nice series of blog posts on the topic that should get you up to speed on the basics.

Do you remember this from early high school math? The length of the blue side is 3.
The length of the green side is 4,
And thus the length of the red side is SQRT( 32 + 42 )

Imagine that the green line is the X-speed and the blue line is the Y-speed.

Having said that, yes, so read up on some math. It will come in quite handy and Byte56's answer does cover that. I just posted this to add an easier version.