Setting a vector's length         When you need to arbitrarily set the length of a Vector3

## Overview of the problem

You are writing some code that uses OGRE's great Ogre::Vector2, Ogre::Vector3 and Ogre::Vector4 classes and then you need to arbitrarily set the length of the vector you are using.

You look at OGRE's documentation, but then you scream out loud when you realize that OGRE does not define any function to set the length of a vector. As you are not a math genius (just as me) and/or you are never sure about yourself (just as mee too), you are desperate as you see that looking for some code in the forums gives you everything except what you are looking for.

## The solution

The solution is really simple, just create a small function in your program, that takes a Ogre::Vector3 pointer as the first parameter and a Ogre::Real as the second parameter, and you are done!

```void setVectorLength(Ogre::Vector3 *vector, const Ogre::Real newLength)
{
vector->normalise();
(*vector) *= newLength;
}```

You could write overloaded functions for Ogre::Vector2 and Ogre::Vector4 as well.

```void setVectorLength(Ogre::Vector2 *vector, const Ogre::Real newLength)
{
vector->normalise();
(*vector) *= newLength;
}

void setVectorLength(Ogre::Vector4 *vector, const Ogre::Real newLength)
{
vector->normalise();
(*vector) *= newLength;
}```

79 online users