FiveM:DrawMarker缩放时玩家距离不准确
我想在一个FiveM GTA V服务器中创建一个圆形区域,但在两个坐标之间计算距离时遇到了问题。我计算的数值似乎从未与DrawMarker使用的半径一致,这使得创建一个正确的区域检查变得困难。
local points = GetEntityCoords(PlayerPedId())
local coords = vector3(110.2, -1951.51, 100)
local radius = 100
我测试了多种方法:
local dx = math.abs(points.x - coords.x)
local dy = math.abs(points.y - coords.y)
local dz = math.abs(points.z - coords.z)
dx <= radius and dy <= radius and dz <= radius
Or,
local dist1 = math.max(dx, dy, dz)
local dist2 = math.sqrt( (dx * dx) + (dy * dy) + (dz * dz))
local dist3 = GetDistanceBetweenCoords(points.x, points.y, points.z, coords.x, coords.y, coords.z, true)
local dist4 = #(points - coords)
dist1 <= radius or dist2 <= radius or dist3 <= radius or dist4 <= radius
但这些方法似乎都无法始终与 DrawMarker 中使用的半径保持一致。
DrawMarker(1, coords.x, coords.y, coords.z - radius * 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, radius * 2.0, radius * 2.0, coords.z + radius * 1.0, 255, 0, 0, 100, false, false, 2, false, nil, nil, false )
我的目标是在玩家处于标记所可视显示的同一半径内时进行检测,但距离检查并没有对齐。
在FiveM中,计算或匹配DrawMarker半径所需的距离的正确方法是什么?
解决方案
Approx. ~1.0 offset is still present when matching the exact zone scale, but this is currently the closest result I’ve found for an accurate cylinder-zone check.
local dx = math.abs(points.x - coords.x)
local dy = math.abs(points.y - coords.y)
local dz = math.abs(points.z - coords.z)
Keep the z coordinate fixed for horizontal (x/y) distance checks, and handle height (z) validation separately.
In practical testing across different radii, I found that most coordinates work well using a consistent empirical offset (~117), though a few cases may require slightly higher or lower values. Since the majority of cases align, I implemented the distance check using the following approach:
local scale = radius + (radius / 117) -- 117 is magic number
math.sqrt( (dx * dx) + (dy * dy)) < scale and dz < scale -- return bool value