Jump to content
  • Advertisement
Sign in to follow this  

Varying a 3D vector by a random amount

This topic is 3160 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi. Im implementing a search function for a character. Im using the method outlined in AI Wisdom 2: Hunting down the player in a convincing manner. You start just getting a direction vector to the players location. The next part is to alter this vector by a random variance within a defined angle Q. Can anyone help me with how i would go about altering the vector by Q? Cheers

Share this post

Link to post
Share on other sites
Here's one way to do it:

1. Compute the vector from the entity position to the player position

2. Compute a random angle in the range [-Q/2, Q/2]

3. Rotate the direction vector about the entity's up vector (or world up vector) by the specified angle

Share this post

Link to post
Share on other sites
Define "random variance". Do you want a cone of possible vectors which are separated by at most Q degrees? Or variation only in one axis? Or something else?

Share this post

Link to post
Share on other sites
Here is the way I randomize a vector in the field of view of a given direction with limits to the min and max angle between the given vector and the randomized vector:
1. Randomize a vector in a unit box.
2. if the vector is outside the unit sphere go back to step 1.
3. Normalize the vector.
4. If the vector is in the requested area then we are done.
5. Transform the vector to the requested area.

Here is my code in C++. It uses a linear transformation instead of a rotation matrix for efficiency.

void RandVecInArea(CVector3f& vRand, CVector3f& vDir,
float fMin, float fMax)
vRand.Set(Rand(-1, 1), Rand(-1, 1), Rand(-1, 1));
}while(vRand.Length2() > 1);
float fCosTi((-1)*(vRand*vDir));
float fTi(acos(fCosTi));
float fTt(0), fTmp(PI - fTi);
if(fMin <= fTmp && fTmp <= fMax) return; //vRand is in the specified area
else if(fTmp < fMin) fTt = fMin + (1 - (PI - fTi)/fMin)*(fMax - fMin);
else fTt = fMin + (fTi/(PI - fMax))*(fMax - fMin);
fTmp = sin(fTt)/sin(fTi);
vRand = (cos(fTt) - fTmp*fCosTi)*vDir - fTmp*vRand;

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!