Command in range of a coord

How could I make a. Script so the command and events are only triggered of within a 15 metere range of a coordinate?

2 Likes

Assuming you want 15m in a radius:

(x - cx)² + (y + cy)² + (z - cz)² <= r²

Where x, y, z are the components of the coordinate you want to test it is inside the circle.
Where cx, cy, cz are the components of the circle’s center position.
Where r is the radius of the circle.

In lua:

function IsPointInSphere(x, y, z, cx, cy, cz, radius)
  
    local tx = math.pow(x - cx, 2)
    local ty = math.pow(y - cy, 2)
    local tz = math.pow(z - cz, 2)

    return (tx + ty + tz) <= math.pow(radius, 2)
end

Function will return true or false, True if it’s the point is indeed in the sphere, else false.

Note that this function is for spheres (so 3d). You could eliminate the y component, then it would be more like a 2D circle.

1 Like

Couple questions. So where is the 15 metere radius located in that (is it the (radius, 2))?

Also in my code Would I have

RegisterCommand (“example”, Functions())
function IsPointInShere (…
…,…
…

So with the register command and then the Sphere function below? Thanks for all the help

When the command gets invoked/called you would need two things:

  • Player location (GetEntityCoords…)
  • Location of the center of the circle.

x, y, x are the player coordinates and cx, cy, cz are the circle center point coordinated.
You can pass those coordinates to the function, the last argument (radius) specifies how big of a radius/area it should consider. So to set the radius, just use the argument of the function.

You could have something like this:


RegisterCommand (“example”, function()
   //Player position
    local x,y,z= GetEntityCoords(GetPlayerPed(-1))

    //Circle coordinates
    local cx, cy, cz = 100, 50, 100

    //Radius of the circle
    local radius = 15

    //Check if inside circle.
    local insideCircle = IsPointInSphere(x, y, z, cx, cy, cz, radius)

    if(insideRadius)
        TriggerClientEvent("chat", source, "You are inside the circle.")
    else
        TriggerClientEvent("chat", source, "You are outside the circle.")        
    end

end)

function IsPointInSphere(x, y, z, cx, cy, cz, radius)
  
    local tx = math.pow(x - cx, 2)
    local ty = math.pow(y - cy, 2)
    local tz = math.pow(z - cz, 2)

    return (tx + ty + tz) <= math.pow(radius, 2)
end

1 Like

So if I wanted more that 1 circle location would I just add

 //Circle coordinates
    local cx, cy, cz = 100, 50, 100
    local cx, cy, cz = NEW, COORD, POINTS
    local etc.

Just run the test for all the circle coordinates.

Trying to fix this now that math.pow is deprecated, getting this error
attempt to preform arithmetic on a nil value y

function IsPointInSphere(x, y, z, cx, cy, cz, radius)

    local tx = (x - cx)^2
    > local ty = (y - cy)^2 -- This Line
    local tz = (z - cz)^2

    return (tx + ty + tz) <= radius^2
end

Any ideas?

just changed of how i go about my problem using now

local areas= { {x = -2226.0, y = 3240.0, z = 32.0} }

	local x,y,z = table.unpack(GetEntityCoords(GetPlayerPed(-1), true))

	for k, coords in pairs(areas) do
			distance = GetDistanceBetweenCoords(x, y, z, coords.x, coords.y, coords.z, true)
			ShowNotification(distance)
		end