VectorNormalize(vector *v)

Raeven0

Newbie
Joined
Jan 16, 2005
Messages
2,495
Reaction score
0
VectorNormalize(vector& v)

Okay, so we have a function VectorNormalize(vector& v) that returns a single float given a single vector. It's defined in vector.h, line 1508 or so. I can't figure out how it arrives at that float given a single vector, and I don't know any coders. What does it do?
 
It normalizes a vector; the vector is passed by reference and thus is altered itself.

As for the float return value, I'd expect to be the previous magnitude of the vector, but that's a guess (it's typically not used, you can just call vectornormalize without using the return value iinm).
 
Vectors are the weakest part of my programming knowledge. I have no idea what Normalise does, but often stuffing a Vector through it stops crazy firing angles :P

-Angry Lawyer
 
Divides all the components of the vector by the magnitude of the vector, so the vector becomes of unit length, if I remember correctly.
 
My initial assumption, based on limited prior work with vectors, was that it would reduce the vector to a unit vector... but that's not possible because it returns a float, not a unit vector.

One coder I found by a middleman in #valveerc said VectorNormalize would take a vector elephant and a vector banana and return elephant*banana*sin(theta), but that's not possible because A) the function accepts only one argument, and B) the vector we use is a unit vector but the returned float is 182 or 400 or some other high number.

The code I want to understand that uses it is WalkMove() in gamemovement.cpp:
Code:
	VectorCopy(wishvel, wishdir);   // Determine maginitude of speed of move
	wishspeed = VectorNormalize(wishdir);
	Msg("wishdir == %f %f %f --- wishspeed == %f\n",wishdir.x,wishdir.y,wishdir.z,wishspeed); // --Rae
wishvel is a unit vector, with the z part zeroed, containing the direction in which the player desires to move; wishspeed is used later to determine how much to change velocity. But my added-in Msg makes wishspeed out to be the maximum non-directional speed I can attain, completely unrelated to my wish velocity.

wishvel, if we press +forward and +right, is [VectorNormalize(vector &forward) VectorNormalize(vector &right) 0], where forward is a unit vector (with the z zeroed) showing our current forward direction, and right is a unit vector (again with the z zeroed) showing the direction 90 degrees to our right (the .x and .y are changed to 0 or negated depending on the keys we press). The idea is basically that we can move in any of eight directions depending on the current facing angle and the keys we're pressing.

And I want to know why it does that...
 
Your initial idea was correct, it does reduce the vector to a unit vector by dividing all components through the magnitude.

You are thinking that a return value is the only way to produce output, but you're mistaken. When a function parameter is defined with an &, it's called "pass by reference". That means that when you pass variables into the function, they themselves can be altered by the function. The return value can then be either additional information, or absent. In this case, it's additional information, and the only logical piece of information I can think of right away might come out of a normalize is the magnitude of the vector. Other calls I've seen in the code confirm this iinm.

Hi crackhead. Haven't been here before either, was just checking out a post on our mod in a related forum and couldn't help dropping by in the coding forum :)
 
You want to know why it does what?

There's a looot going on in gamemovement.cpp, but I'm pretty much at home in it since I rewrote half of it for our alien movement (and the other half soon to follow). I'll check out the WalkMove next time I'm in windows.
 
Specifically, what I'm looking for is how we arrive at the desired speed (wishspeed) by VectorNormalizing the movement direction (wishvel).

I never knew what that & meant, so that's helpful information. It's difficult to look up some of this stuff, so I end up assuming it's all basically the same :P
 
Well, as I said, the return value is probably the magnitude. It makes sense to take the magnitude of the vector as speed.

The wishvel is created by

Code:
for (i=0 ; i<2 ; i++)       // Determine x and y parts of velocity
wishvel[i] = forward[i]*fmove + right[i]*smove;

which is pretty basic stuff. forward and right are vectors which point at your current forward and right directions, and fmove and smove are values that determine how much you should move in each direction; these are determined by your input.

The code you pasted serves to split up the vector wishvel, representing the velocity, into separate values for a direction (a unit vector, hence the normalize), and a magnitude. This is done to be able to limit the magnitude to a server-defined maximum speed value.
 
My, that suddenly seems very obvious. :P

I had always thought fmove and smove could only ever be 1, 0, or -1, because of a comment I had seen somewhere else in the code...
 
Back
Top