2
\$\begingroup\$

For any normalized 2D vector, except for ( 0, 0 ), how would I scale the vector to always be the same length?

For example:

int length = 10;

vector v = vector( 0.1, 0.5 );
vector v2 = vector( 0.3, 0.8 );

// Scale v to be length of 10
// Scale v2 to be length of 10
\$\endgroup\$
  • 2
    \$\begingroup\$ Homework question? \$\endgroup\$ – Jari Komppa Dec 11 '12 at 11:25
  • 3
    \$\begingroup\$ You seem to use the term normalized incorrectly. A normalized vector has unit length (1.0), while the ones you have in your example are definitely not of unit length. \$\endgroup\$ – Lars Viklund Dec 11 '12 at 11:39
  • \$\begingroup\$ Scaling a vector means multiplying it by a constant. \$\endgroup\$ – Anko Dec 11 '12 at 12:10
  • 2
    \$\begingroup\$ @JariKomppa Why homework? This is one of primary tasks you bump into when making 2D games. But I, as the OP, I'm here to do games, not math. \$\endgroup\$ – Tomáš Zato - Reinstate Monica Sep 22 '15 at 0:22
8
\$\begingroup\$

You mean like this?

length = sqrt(v.x * v.x + v.y * v.y);

// normalize vector
v.x /= length;
v.y /= length;

// increase vector size
v.x *= 10
v.y *= 10
\$\endgroup\$
  • \$\begingroup\$ Not really, please have a look at my edit. \$\endgroup\$ – user23837 Dec 11 '12 at 10:50
  • \$\begingroup\$ @MichielPater is it ok? \$\endgroup\$ – Ali1S232 Dec 11 '12 at 11:06
  • 1
    \$\begingroup\$ @user23837 Yes that is what you want. That's the way to normalize a vector. You do it for all vectors in question and then multiply by a new length/magnitude \$\endgroup\$ – Sidar Dec 11 '12 at 17:54
6
\$\begingroup\$

A normalized vector, by definition, has length 1. To scale it to length 10, you need to multiply it by 10.

(To multiply a vector by a number, multiply every component of the vector by the number. Depending on the language you use, just writing vector = vector * number may do that automatically, or there may at least be a library function or method to do that.)

By the way, the vectors in your example code are not normalized — their length is not 1. To normalize them, do as Gajoo suggests, i.e. divide them by their length. Again, your language or vector library may already provide a function to do that.

\$\endgroup\$
1
\$\begingroup\$

Normalised vectors have the same length by definition: one. If you want to scale them to an arbitrary length L instead, multiply them (or their components) by L. Note that the examples you have provided are not normalised vectors. To normalise, divide the vectors by their magnitude.

For example (component-wise, as I do not know the extent of vector algebra support in your environment):

int targetlength = 10;
vector v = vector( 0.1, 0.5 );
float vMagnitude = sqrt(v.x*v.x + v.y*v.y);
v.x = targetlength * v.x / vMagnitude;
v.y = targetlength * v.y / vMagnitude;

// v = ( 1.96, 9.81 )

Or with vector algebra:

int targetlength = 10;
vector v = vector( 0.1, 0.5 );
v = targetlength * v / v.length;
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy