vrld / HC

General purpose collision detection library for the use with LÖVE.
http://hc.readthedocs.org/
404 stars 48 forks source link

Intersections with segment #45

Closed sixFingers closed 8 years ago

sixFingers commented 8 years ago

Thanks again for the wonderful library, 90% of my game is Hardon Collider. Nonetheless, i often miss the ability to rapidly cast a ray and get a list of collisions ordered from the origin of the ray toward the ray's direction. This would complement well the other side of the api (which tells: given a shape, give me ray intersections).. with a "given a ray, give me the shapes". I'm proposing a very simple addition to the code. May be not optimized, but works fast enough for me. The pr only includes a single method for the Spatialhash class, intersectionsWithSegment which tries to mimick Shape's intersectionsWithRay. It's called "with segment" since only the points lying on the given segment will be returned.

sixFingers commented 8 years ago

Forgot to document: you would call the method this way:

local intersections = hash:intersectionsWithSegment(startX, startY, endX, endY)

.. then you would iterate the results like:

for _, intersection in ipairs(intersections) do
    -- t = ray parameter
    -- x, y = intersection point
    -- shape = intersecting shape
    local shape, t, x, y = unpack(intersection)
sixFingers commented 8 years ago

@vrld Hi, just pinging to check if this PR is being considered useful. I keep cloning from here :)

vrld commented 8 years ago

Sorry, I've been quite busy the past few weeks.

This looks tremendously useful! Thanks :+1:

sixFingers commented 8 years ago

Oh well, you did the work. I just juggled with what you made available :)