Fixed tangent mis calculation (#42)

* Fixed tangent mis calculation
This commit is contained in:
2025-05-26 14:29:51 +02:00
committed by GitHub
parent fbcff08a54
commit cae2c7edb3
3 changed files with 84 additions and 25 deletions
Binary file not shown.
+40 -22
View File
@@ -382,37 +382,37 @@ end
---@param origin Vec2
---@return Array<Vec2>
function UTIL.GetTangentHullPointsFromOrigin(hull, origin)
local function cross(o, a, b)
return (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x)
local function orientation(a, b, c)
-- Returns >0 if c is to the left of ab, <0 if to the right, 0 if colinear
return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x)
end
local n = #hull
if n == 0 then return {} end
if n == 1 then return { hull[1] } end
local left, right = nil, nil
for i = 1, n do
local prev = hull[(i - 2 + n) % n + 1]
local curr = hull[i]
local next = hull[(i % n) + 1]
local cp1 = cross(origin, curr, prev)
local cp2 = cross(origin, curr, next)
-- Tangent if the cross products have different signs or one is zero
if cp1 * cp2 <= 0 then
if not left then
left = curr
else
right = curr
-- Find left tangent: the hull point where all other points are to the right of the line from origin to that point
local function findTangent(isLeft)
local best = 1
for i = 2, n do
local o = orientation(origin, hull[best], hull[i])
if (isLeft and o < 0) or (not isLeft and o > 0) then
best = i
end
end
return hull[best]
end
local result = {}
if left then table.insert(result, left) end
if right and right ~= left then table.insert(result, right) end
return result
local leftTangent = findTangent(true)
local rightTangent = findTangent(false)
-- If tangents are the same (can happen if origin is colinear), only return one
if leftTangent.x == rightTangent.x and leftTangent.y == rightTangent.y then
return { leftTangent }
else
return { leftTangent, rightTangent }
end
end
end
@@ -1089,7 +1089,6 @@ do -- INIT DCS_UTIL
end
functionString = functionString .. "{0,1,0,1}, {0,1,0,1}, " .. lineStyle .. ")"
env.info(functionString)
---@diagnostic disable-next-line: deprecated
local f, err = loadstring(functionString)
if f then
@@ -1118,6 +1117,25 @@ do -- INIT DCS_UTIL
return drawID
end
---@param start Vec3
---@param finish Vec3
---@param lineColor DrawColor
---@param lineStyle LineType
function DCS_UTIL.DrawLine(start, finish, lineColor, lineStyle)
if lineStyle == nil then lineStyle = 4 end
drawID = drawID + 1
local lineColorMapped = {
lineColor.r or 0,
lineColor.g or 0,
lineColor.b or 0,
lineColor.a or 1
}
trigger.action.lineToAll(-1, drawID, start, finish, lineColorMapped, lineStyle)
return drawID
end
local _markID = 200
+44 -3
View File
@@ -92,6 +92,8 @@ function BattleManager:Update()
return math.random(4, 10) -- Return a random interval between 5 and 10 seconds for the next update
end
local drawID = 8633
---@private
---@param groups Array<SpearheadGroup>
---@param targetGroups Array<SpearheadGroup>
@@ -124,11 +126,22 @@ function BattleManager:LetUnitsShoot(groups, targetGroups)
radius = 1,
expendQty = qty,
weaponType = ammo,
expendQtyEnabled = true,
counterbattaryRadius = math.random(5, 10)
expendQtyEnabled = true
}
}
---- DEBUG ONLY!!
--- DRAWS THE SHOOTING LINE
-- do
-- local color = {r = 1, g = 0, b = 0, a = 1}
-- if unit:getCoalition() == 2 then
-- color = {r = 0, g = 0, b = 1, a = 1}
-- end
-- Spearhead.DcsUtil.DrawLine(unitPos, {x = point.x, y = 0, z = point.y}, color, 1)
-- end
self._logger:debug("Red unit " .. unit:getName() .. " will shoot " .. qty .. " rounds at point: " .. tostring(point))
local controller = unit:getController()
@@ -211,7 +224,35 @@ function BattleManager:GetRandomPoint(origin, groups)
local hull = Spearhead.Util.randomFromList(hulls) --[[@as Array<Vec2>]]
local enlargedHull = Spearhead.Util.enlargeConvexHull(hull, 25)
local shootPoints = Spearhead.Util.GetTangentHullPointsFromOrigin(enlargedHull, origin)
---- DEBUG ONLY!!
---- DRAWS the hulls of the units
-- for _, drawHull in pairs(hulls) do
-- ---@type SpearheadTriggerZone
-- local zone = {
-- name = "temp",
-- zone_type = "Polygon",
-- radius = 0,
-- verts = drawHull,
-- location = { x=drawHull[1].x, y=drawHull[1].y },
-- }
-- Spearhead.DcsUtil.DrawZone(zone, {r =0, g= 1, b =0, a = 0.5} ,{r =0, g= 1, b =0, a = 0}, 1)
-- local enlarged = Spearhead.Util.enlargeConvexHull(drawHull, 25)
-- local enlargedZone = {
-- name = "temp_enlarged",
-- zone_type = "Polygon",
-- radius = 0,
-- verts = enlarged,
-- location = { x=enlarged[1].x, y=enlarged[1].y },
-- }
-- Spearhead.DcsUtil.DrawZone(enlargedZone, {r =0, g= 0, b =1, a = 0.5} ,{r =0, g= 1, b =0, a = 0}, 1)
-- end
return Spearhead.Util.randomFromList(shootPoints) --[[@as Vec2]]
end