From 485335506e09a372a1b8669ef0f97e3f72709d82 Mon Sep 17 00:00:00 2001 From: rzvxa Date: Fri, 18 Aug 2023 21:15:18 +0330 Subject: [PATCH 1/4] Added a raycast function --- init.lua | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/init.lua b/init.lua index 8ec9481..2240602 100644 --- a/init.lua +++ b/init.lua @@ -118,6 +118,27 @@ function HC:collisions(shape) return candidates end +function HC:raycast(x, y, dx, dy, range) + local dxr, dyr = dx * range, dy * range + local bbox = { x + dxr , y + dyr, x, y } + local candidates = self._hash:inSameCells(unpack(bbox)) + + for col in pairs(candidates) do + local intersections = col:intersectionsWithRay(x, y, dx, dy) + if #intersections > 0 then + for i, intersection in pairs(intersections) do + if intersection < 0 or intersection > range then + rawset(intersections, i, nil) + end + end + rawset(candidates, col, intersections) + else + rawset(candidates, col, nil) + end + end + return candidates +end + function HC:shapesAt(x, y) local candidates = {} for c in pairs(self._hash:cellAt(x, y)) do From 2eecdb452ffc49502d9bdd4cf0b489ef0b99c641 Mon Sep 17 00:00:00 2001 From: rzvxa Date: Fri, 18 Aug 2023 21:39:05 +0330 Subject: [PATCH 2/4] Now raycast returns hit points instead of ray parameters. --- init.lua | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/init.lua b/init.lua index 2240602..b89d274 100644 --- a/init.lua +++ b/init.lua @@ -124,14 +124,17 @@ function HC:raycast(x, y, dx, dy, range) local candidates = self._hash:inSameCells(unpack(bbox)) for col in pairs(candidates) do - local intersections = col:intersectionsWithRay(x, y, dx, dy) - if #intersections > 0 then - for i, intersection in pairs(intersections) do - if intersection < 0 or intersection > range then - rawset(intersections, i, nil) + local rparams = col:intersectionsWithRay(x, y, dx, dy) + if #rparams > 0 then + for i, rparam in pairs(rparams) do + if rparam < 0 or rparam > range then + rawset(rparams, i, nil) + else + local hitx, hity = x + (rparam * dx), y + (rparam * dy) + rawset(rparams, i, { x = hitx, y = hity }) end end - rawset(candidates, col, intersections) + rawset(candidates, col, rparams) else rawset(candidates, col, nil) end From c40c8fba05a58bf95e3347fbe3e51a74615cd3c8 Mon Sep 17 00:00:00 2001 From: rzvxa Date: Fri, 18 Aug 2023 21:40:48 +0330 Subject: [PATCH 3/4] Added documentation for raycast function --- docs/MainModule.rst | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/docs/MainModule.rst b/docs/MainModule.rst index 61ff506..fad8b07 100644 --- a/docs/MainModule.rst +++ b/docs/MainModule.rst @@ -254,6 +254,25 @@ You can iterate over the shapes using ``pairs`` (see example). game.selectUnit(s) end +.. function:: HC.raycast(x, y, dx, dy, range) + + :param numbers x,y: Origin point of ray + :param numbers dx,dy: Direction vector of ray(normal vector) + :param number range: Range of raycast + :returns: Table of shapes that got hit and its hit points. + +Gets shapes that got hit by a given ray and the points which that shape intersects with the ray. +The table is a *set*, meaning that the shapes are stored in *keys* of the table. The values are the points of intersection. You can iterate over the shapes using ``pairs`` (see example). + +**Example**:: + + local hits = HC.raycast(originx, originy, directionx, directiony, range) + for shape, points in pairs(hits) do + for _, point in ipairs(points) do + love.graphics.points(point.x, point.y) + end + end + .. function:: HC.hash() From 3ab94cdf31323f172b65041a3712d4402491b45d Mon Sep 17 00:00:00 2001 From: Ali Rezvani <3788964+rzvxa@users.noreply.github.com> Date: Wed, 23 Aug 2023 18:18:09 +0000 Subject: [PATCH 4/4] Fixed indentention --- init.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/init.lua b/init.lua index b89d274..0fa3999 100644 --- a/init.lua +++ b/init.lua @@ -130,7 +130,7 @@ function HC:raycast(x, y, dx, dy, range) if rparam < 0 or rparam > range then rawset(rparams, i, nil) else - local hitx, hity = x + (rparam * dx), y + (rparam * dy) + local hitx, hity = x + (rparam * dx), y + (rparam * dy) rawset(rparams, i, { x = hitx, y = hity }) end end