How to check in an Entity is in a BlipForArea with rotation

Hello how could I check if an entity is in the AddBlipAreaForArea because I tried to use IsEntityInArea but I can’t because I’m using rotation for the BlipArea so how I could do please ?

My blip creation:

    local coordsQGvagos = vector3(322.73, -2029.84, 20.81)
    blipQG3 = Citizen.InvokeNative(0xCE5D0E5E315DB238, coordsQGvagos.x, coordsQGvagos.y, coordsQGvagos.z, tonumber(275.0), tonumber(195.0))
    SetBlipRotation(blipQG3, math.ceil(tonumber(320.0)))
    SetBlipColour(blipQG3, 0xc7b60098)

You can’t use IsEntityInArea because the area is rotated, but you can write a subsitute to that function. You need to rotate the coordinates of your player.

Here’s the function I’ve written to achieve such a thing :

-- o : origin
-- r : rotation (in rad)
-- c1, c2 : area corners

local function IsPedInRotatedArea(o, r, c1, c2)
    local v = GetEntityCoords(PlayerPedId())
    v = vector3(
        (v.x - o.x) * math.cos(r) + (v.y - o.y) * math.sin(r) + o.x,
        (v.x - o.x) * math.sin(r) - (v.y - o.y) * math.cos(r) + o.y, 
        v.z
    )
    return ((v.x < c1.x and v.x > c2.x) or (v.x > c1.x and v.x < c2.x)) and ((v.y < c1.y and v.y > c2.y) or (v.y > c1.y and v.y < c2.y)) and ((v.z < c1.z and v.z > c2.z) or (v.z > c1.z and v.z < c2.z))

end

Here the origin is the center of you area aka coordsQGvagos and the rotation is 320.0 but in radians so local r = 320.0 * math.pi / 180.

The code I used to test the function
local origin = vector3(322.73, -2029.84, 20.81) -- Origin of the coordinate system
local d = 320.0 -- Degrees to turn the points
local r = d * math.pi / 180 -- Radian to turn the points
local dx = 275.0
local dy = 195.0

-- Calculate the corners
local c1 = vector3(origin.x - dx / 2, origin.y + dy / 2, -100.0)
local c2 = vector3(origin.x + dx / 2, origin.y - dy / 2, 100.0)

local blip = AddBlipForArea(origin, dx, dy) -- Draw the area
SetBlipRotation(blip, math.ceil(d))
SetBlipColour(blip, 5)

-- o : origin
-- r : rotation (in rad)
-- c1, c2 : area corners
local function IsPedInRotatedArea(o, r, c1, c2)
    local v = GetEntityCoords(PlayerPedId())
    v = vector3(
        (v.x - o.x) * math.cos(r) + (v.y - o.y) * math.sin(r) + o.x,
        (v.x - o.x) * math.sin(r) - (v.y - o.y) * math.cos(r) + o.y, 
        v.z
    )
    return ((v.x < c1.x and v.x > c2.x) or (v.x > c1.x and v.x < c2.x)) and ((v.y < c1.y and v.y > c2.y) or (v.y > c1.y and v.y < c2.y)) and ((v.z < c1.z and v.z > c2.z) or (v.z > c1.z and v.z < c2.z))
end

function DrawTextAdvanced(x, y, text, color)
    SetTextFont(0)
    SetTextScale(0.3, 0.3)
    SetTextColour(color.r, color.g, color.b, color.a)
    SetTextDropshadow(0, 0, 0, 0, 255)
    SetTextEdge(2, 0, 0, 0, 150)
    SetTextDropShadow()
    SetTextOutline()
    SetTextEntry("STRING")
    SetTextCentre(1)
    AddTextComponentString(text or "nope")
    DrawText(x, y)
end

Citizen.CreateThread(function()
    while true do
        Wait(0)
        DrawTextAdvanced(0.5, 0.5, IsPedInRotatedArea(origin, r, c2, c1), { r = 255, g = 0, b = 0, a = 200 })
    end
end)

Note to myself : not passing an integer to SetBlipRotation breaks it.

2 Likes

Thank your Elio for your help ! :grin: