Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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
share|improve this question
1  
Homework question? –  Jari Komppa Dec 11 '12 at 11:25
1  
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. –  Lars Viklund Dec 11 '12 at 11:39
    
Scaling a vector means multiplying it by a constant. –  Anko Dec 11 '12 at 12:10

3 Answers 3

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.

share|improve this answer

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
share|improve this answer
    
Not really, please have a look at my edit. –  user23837 Dec 11 '12 at 10:50
    
@MichielPater is it ok? –  Ali.S Dec 11 '12 at 11:06
    
@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 –  Sidar Dec 11 '12 at 17:54

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;
share|improve this answer

Your Answer

 
discard

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