Garry's Mod Wiki

Vector:Dot

number Vector:Dot( Vector otherVector )

Description

Returns the dot product of this vector and the passed one.

The dot product of two vectors is the product of their magnitudes (lengths), and the cosine of the angle between them:

a · b = |a| |b| cos(θ)

where a and b are vectors.

See Vector:Length for obtaining magnitudes.

A dot product returns just the cosine of the angle if both vectors are normalized, and zero if the vectors are at right angles to each other.

Arguments

1 Vector otherVector
The vector to calculate the dot product with

Returns

1 number
The dot product between the two vectors

Example

Get the angle of two opposite normalized vectors.

local a = Vector(0, 1, 0) local b = Vector(0, -1, 0) local dot = a:Dot(b) -- returns the cos(ang) of the two vectors because they're both of length 1 print("Radians", math.acos(dot)) -- the inverse of the cosine to get the angle print("Degrees", math.deg(math.acos(dot))) -- Convert radians to degrees
Output: Radians 3.1415926535898
Degrees 180

Example

Calculates whether the player is looking in the direction of an entity. This is often faster than traces, but it produces a slightly different result.

The player is looking in the direction of the entity if the angle between the aimvector and the vector from the player to the entity is less than 45 degrees (or pi / 8 radians).

local directionAng = math.pi / 8 local aimvector = ply:GetAimVector() -- The vector that goes from the player's shoot pos to the entity's position local entVector = ent:GetPos() - ply:GetShootPos() local dot = aimvector:Dot(entVector) / entVector:Length() print(dot < directionAng)
Output: This script will say if the player is looking in the direction of the entity.

Example

A function to make sure the player is looking somewhere.

function IsLookingAt( ply, targetVec ) return ply:GetAimVector():Dot( ( targetVec - ply:GetPos() + Vector(70) ):GetNormalized() ) < 0.95 end
Output: Returns true if ply is looking at (or close to) the target.

Page Links


Special Pages


Wikis

?

Render Time: 136ms

DB GetPage 33
Generate Html 11
SaveChanges (1) 78
Render Body 0
Render Sidebar 12