mirror of
https://github.com/vrld/HC.git
synced 2024-11-18 12:54:23 +00:00
Added raycast function (#70)
* Added a raycast function * Now raycast returns hit points instead of ray parameters. * Added documentation for raycast function * Fixed indentention
This commit is contained in:
parent
814061b9a3
commit
eb1f285cb1
@ -254,6 +254,25 @@ You can iterate over the shapes using ``pairs`` (see example).
|
|||||||
game.selectUnit(s)
|
game.selectUnit(s)
|
||||||
end
|
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()
|
.. function:: HC.hash()
|
||||||
|
|
||||||
|
24
init.lua
24
init.lua
@ -118,6 +118,30 @@ function HC:collisions(shape)
|
|||||||
return candidates
|
return candidates
|
||||||
end
|
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 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, rparams)
|
||||||
|
else
|
||||||
|
rawset(candidates, col, nil)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return candidates
|
||||||
|
end
|
||||||
|
|
||||||
function HC:shapesAt(x, y)
|
function HC:shapesAt(x, y)
|
||||||
local candidates = {}
|
local candidates = {}
|
||||||
for c in pairs(self._hash:cellAt(x, y)) do
|
for c in pairs(self._hash:cellAt(x, y)) do
|
||||||
|
Loading…
Reference in New Issue
Block a user