Please make a Vector3.MagnitudeSquared to reduce expensive square roots

Roblox should make a Vector3.MagnitudeSquared function because doing a sqroot is slow
This is a classic game dev optimization.

The Problem

– Vector3.Magnitude does this internally:
math.sqrt(xx + yy + z*z) – sqrt is expensive


## The Workaround Today

-- Roll your own
local function magnitudeSquared(v: Vector3): number
    return v.X*v.X + v.Y*v.Y + v.Z*v.Z
end

-- Compare distances without sqrt
local distSq = magnitudeSquared(a - b)
if distSq < 100*100 then  -- within 100 studs
    -- much faster than (a-b).Magnitude < 100
end

Why It Matters in Roblox

This pattern comes up constantly:

  • NPC aggro range checks (running every heartbeat for many NPCs)
  • Proximity detection
  • Particle/LOD culling

If you’re doing this in a loop with 100+ entities at 60hz, you’re calling sqrt 6000 times/second for no reason when you just need to compare distances.

The Ask Makes Sense

Roblox already has .Magnitude and .UnitMagnitudeSquared (or .Magnitude2 / .LengthSquared like in Unity/Godot/Unreal) is a natural addition and a zero-cost one to implement.

5 Likes

What stops you from doing it manually and declaring the script/function as native?

@native
local function squaredMagnitude(v: Vector3): number
	return v.X*v.X + v.Y*v.Y + v.Z*v.Z
end

(After performing some tests I concluded that this runs 22 times faster than built-in v.Magnitude on my machine)

5 Likes

My theory is the engine written in c++ (probably) should have that MagnitudeSquared, because when it has to run a lanugage like lua in a script it is orders of magnitude slower than baked into the engine library.

1 Like

classic optimization every engine has (Unity’s sqrMagnitude, etc.).

Luau’s native .Magnitude is way faster now, but a built-in Vector3.MagnitudeSquared would still be cleaner + fastest for hot loops.

Roblox should add it. :+1:

2 Likes

I agree with your post but out of curiosity, have you ran into performance issues where doing this sqrt over and over was the cause?

3 Likes

supporting this too, micro optimization where ever possible is always good

This is true. It will be faster. I’m going to write an RFC for this.

dudes be saying micro optimize this micro-optimize that but dont micro-optimize their money :joy: micro opt culture is full of inbreds

You can dot the vector with itself as a shortcut to get this. Should be on the fast path too :slight_smile:

2 Likes

Sure, but this isn’t the appropriate category, move it to Engine Requests.

Good point, unfortunately it isn’t available on clients, probably won’t be for a while as they’d have to support so many different system architectures just to make it work on client devices.

This came up on Unity a while back. What you’re doing is faster. The conclusion was: On a average PC, you probably will not notice. On an average phone, it is up to six times faster. I filed this concept in my good stuff notes.. :rofl: I knew right away what you were talking about due to that.
Phones benefit the most, PCs barely notice.

1 Like