vrld / HC

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

Fatal error when using HC:register #53

Closed LennDG closed 6 years ago

LennDG commented 6 years ago

I am trying to use HC for my small hobby project game and I have immediately run in to an issue. My Entity objects store their own shapes created by using the standard methods (e.g. HC.rectangle(x, y, w, h)). Since I need multiple collision layers I then register the shape in the HC instances kept in the World object like so:

-- Add an entity to the world
function World:addEntity(entity)
    -- Give the entity an ID
    entity.ID = self:getNewID()
    if entity.collidable then
        self.collider:register(entity.shape)
    end

    -- Use that ID to add the entity
    self.entities[entity:getID()] = entity
end

When doing this I immediately get the error

Error: modules/HC/spatialhash.lua:49: attempt to perform arithmetic on field 'cell_size' (a table value)
stack traceback:
        modules/HC/spatialhash.lua:49: in function 'cellCoords'
        modules/HC/spatialhash.lua:101: in function 'register'
        modules/HC/init.lua:60: in function 'register'
        world.lua:37: in function 'addEntity'
        abstracts/entity.lua:47: in function 'addToWorld'
        actors/player.lua:64: in function 'initialize'
        modules/middleclass.lua:122: in function 'new'
        main.lua:55: in function 'load'
        [string "boot.lua"]:440: in function <[string "boot.lua"]:436>
        [C]: in function 'xpcall'

I have been drilling down a bit and it seems that somehow during the register operation the self.cell_size field gets overwritten by the init table. The following is the print out of what self.cell_size contains when executing the register.

table: 0x074932b0
collisions
function: 0x074935a8
new
function: 0x07493460
rectangle
function: 0x07493528
point
function: 0x07493568
circle
function: 0x07493548
neighbors
function: 0x07493588
remove
function: 0x074934e8
register
function: 0x074934c8
resetHash
function: 0x07493488
hash
function: 0x074935c8
polygon
function: 0x07493508

Am I misusing the lib or is this a pretty serious bug?

LennDG commented 6 years ago

I have been able to solve the issue. I was doing self.collider = HC:new(100) instead of self.collider = HC.new(100)