Vector/DistToSqr

From Garry's Mod
Jump to: navigation, search
 Vector:DistToSqr( )

Contents

Description

Returns the squared distance of 2 vectors, this is faster than Vector:Distance as calculating the square root is an expensive process.

Arguments

Vector otherVec

The vector to calculate the distance to.

Returns

number

Squared distance to the vector

Examples

Example

Checks if a player is within `dist` units of another player in the most efficient way possible.

function PlayerWithinBounds(ply,otherPly, dist)
	return ply:GetPos():DistToSqr(otherPly:GetPos()) < (dist*dist)
	-- This is computationally faster than:
	-- ply:GetPos():Distance(otherPly:GetPos()) < dist
end
print(PlayerWithinBounds(Entity(1),Entity(2),500))

Output:

true
Personal tools
Navigation