1
\$\begingroup\$

I want to add collision detection mechanism to my game, which is written in love2d. As i do not want to implement my own algorithm, i have found a HardonCollider library to do the heavy lifting for me.

I have followed the example code of the library but got an error even with a basic setup.

local hc  = require "hardoncollider"

local hero
local collider

function on_collision(dt, shape_a, shape_b, mtv_x, mtv_y)

end

function love.load(arg)
    collider = hc(100, on_collision)
    hero = collider:addRectangle(30,30,16,16)
end

Running this code will throw the following error message:

PS C:\dev\love-0.10.0-win64> Error: main.lua:12: attempt to call method 'addRectangle' (a nil value)
stack traceback:
        main.lua:12: in function 'load'
        [string "boot.lua"]:437: in function 
        [C]: in function 'xpcall'

I have been investigating the problem, but did not find any solutions nor anybody else who faced this error.

\$\endgroup\$
2
\$\begingroup\$

addRectangle is a method of hardoncollider, not a method of the object produced from running hc(100, on_collision). The correct way to do this would be:

function love.load(arg)
collider = hc(100, on_collision)
hero = hc:addRectangle(30,30,16,16)
end
\$\endgroup\$
  • \$\begingroup\$ I don't know why this is being upvoted, but the suggested solution does not work. running the updated code produces the same error message. \$\endgroup\$ – Alexander Feb 17 '16 at 16:48
  • \$\begingroup\$ Sorry, didn't realise you weren't running the linked version. \$\endgroup\$ – ChrisUC Feb 17 '16 at 22:59
2
\$\begingroup\$

Answering my own question here, in case somebody else encounters this issue.

Looks like i have been reading outdated documentation & samples, according to the new docs there is no such method as addRectangle.

It should be:

collider = hc.new(100, on_collision)
hero = collider:rectangle(30,30,16,16)
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.