vrld / HC

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

Misleading documentation for HC.collisions(shape) #50

Closed avi-0 closed 6 years ago

avi-0 commented 7 years ago

The documentation says:

The separating vector points away from shape.

Example code is provided, however, it has a syntax error - no do keyword for a for loop - which could mean it hasn't ever been run. In my experience, however, it seems that the separating vector actually points towards shape. Here's some testing code:

HC = require 'HC'

radius = 25
width = 200
height = 50

function love.load()
    -- creates a circle and a rectangle so that they intersect
    shape = HC.circle(400, 300, radius)
    alsoShape = HC.rectangle(300, 315, width, height)
end

function love.keypressed(key, scancode)
    -- resolves collisions when 1 or 2 key is pressed
    if scancode == "1" then
        -- example code from HC docs, but without a syntax error
        local collisions = HC.collisions(shape)
        for other, separating_vector in pairs(collisions) do
            shape:move(-separating_vector.x/2, -separating_vector.y/2)
            other:move( separating_vector.x/2,  separating_vector.y/2)
        end
    end
    if scancode == "2" then
        -- same code, but with reversed movement
        local collisions = HC.collisions(shape)
        for other, separating_vector in pairs(collisions) do
            shape:move( separating_vector.x/2,  separating_vector.y/2)
            other:move(-separating_vector.x/2, -separating_vector.y/2)
        end
    end
end

function love.draw()
    -- draws shapes
    local x, y = shape:center()
    love.graphics.circle("line", x, y, radius)

    local x, y = alsoShape:center()
    love.graphics.rectangle("line", x - width / 2, y - height / 2, width, height)
end

When I press 1, shapes actually move towards each other. When I press 2, shaped move away from each other.

Initially:

test

After pressing 1:

test1

After pressing 2:

test2