# Help explaining vector:Normalize()

I remember using the vector normalize function a lot back when I was scripting, but I’ve seemed to forget how to use it.

I know it has to do with direction, like ( vector1 - vector2 ):Normalize() would give you the direction of those two vector positions but I don’t remember how to use it and what it returns.

it returns the vector specified but with length of 1 unit. So, print(Vector(512,51,200):Normalize()) returns 0.9275 0.0924 0.3623. You use it for specifying angles. (ie aim)

It’s a vector that points a direction (angle) with a magnitude one 1. Multiply it for use with velocities etc. in order to declare the magnitude (strength) of the normal vector.

Ohhh ok I get it. I was playing around with it and launched a prop in the air depending on the magnitude. haha Ok thanks guys!

The formula behind a normalized vector:

where |v| is the length of the vector (vector:Length() = math.sqrt(v.x^2+v.y2+v.z^2) ).

Handy piece of related code:
[lua]
– shared.lua
function ENT:Think() – You could do this in physics simulate, if you wanted. However, PhysicsSimulate isn’t shared, so the client can’t predict the motion
self:SetVelocity(self:GetVelocity()+(self.Target:GetPos()-self:GetPos()):Normalize()*self.HomingSpeed
end
[/lua]

I used something similar to that to create the homing (red) snowballs for ExiledServers.net’s Snowball fight gamemode, though I did use self:GetPhysicsObject():ApplyForceCenter(blah) instead.

Yea I understand that. Thanks for the example, cleared up a few things.

Joke:
So if normalizing is technically a velocity, what if we took the derivative of normalize to get acceleration?

A normalized vector

``````
isn't a velocity

``````

, it’s a vector without length and only direction.

:seriousface:

Normalize, Norm of 1 = Length of 1

Your sarcasm meter must be broken.

I was thinking of phys:SetVelocity() lol So then what’s a logical example of using normalize()?

:irony:

Some meter is broken alright.

Here is an example of how I’m using it in my Sent:
[lua]function ENT:Think()

``````for _,ply in ipairs(player.GetAll()) do

local coinPos =  self:LocalToWorld(self:OBBCenter())
local plyPos = ply:LocalToWorld(ply:OBBCenter())

local trd = {}
trd.start = coinPos
trd.endpos = coinPos + (plyPos - coinPos):GetNormal()*60
trd.filter = {self}
local tr = util.TraceLine(trd)

debugoverlay.Line(trd.start, trd.endpos)

end
``````

end[/lua]
What this does is send out a trace that is 60 units long and is always pointing to a player.

Ahhhhhhhhhhhhhhhhhhhh that makes much more sense now. I knew it had to do something ( vec1 - vec2 ):Normalize(). So that gives the vector direction of those two positions and you multiply it to extend it. Interesting, thanks!

[editline]08:54PM[/editline]

For example in my punch swep, I used Normalize() to find the direction to punch the player getting punched.

[lua]
local trace = {}
trace.start = self.Owner:EyePos()
trace.endpos = trace.start + self.Owner:GetAimVector() * 60
trace.filter = self.Owner
local tr = util.TraceLine( trace )

local norm = ( tr.Entity:GetPos() - self.Owner:GetPos() ):Normalize()
local push = 2000 * norm
tr.Entity:GetPhysicsObject():ApplyForceOffset( push, tr.HitPos )
[/lua]

Wait! Important note here.

Although Normalize() and GetNormal() both return the normal of the vector it was performed upon, it’s very important to know the difference between the two. Normalize() changes the vector it was performed upon. GetNormal returns a normalized copy of the vector it was performed upon.

If you’re going to be using the same vector over and over, and need to get it’s normal occasionally, use GetNormal().
If the vector is temporary (like line 7 of what PC Camp posted) then use Normalize(). That way an additional vector doesn’t have to be created, it just modifies and then returns itself.