Fatal bug when using HC for collision detection

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
Posts: 4
Joined: Fri Dec 08, 2017 1:38 pm

Fatal bug when using HC for collision detection

Post by Habba » Fri Dec 08, 2017 1:52 pm

Hi all,
first off, very new to game dev (though not programming in general) and new to the forums!

I have been working on my first ever hobby game project and am just working on the framework upon which I will build the game. I need a decent system for collision detection so I thought I would use HC as recommended on https://github.com/love2d-community/awe ... e2d#helper.

Every entity would store their own Shape to keep things encapsulated. Their shapes are created by using the provided functions (e.g HC.rectangle()).
Since I would like to have multiple collider instances and think that keeping state in the general one will cause issues down the line my World object holds an instance of HC, created like:

Code: Select all

self.collider = HC:new(100)
When adding an entity to the world I would also register its shape to the collider like:

Code: Select all

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

    -- Use that ID to add the entity
    self.entities[entity:getID()] = entity
However, this immediately causes a fatal bug and crashes the program. The stacktrace is

Code: Select all

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 done some digging and found that the value self.cell_size is being overwritten by the table of init.lua somehow. Here is the printout of the value of self.cell_size when the crash occurs
table: 0x074932b0
function: 0x074935a8
function: 0x07493460
function: 0x07493528
function: 0x07493568
function: 0x07493548
function: 0x07493588
function: 0x074934e8
function: 0x074934c8
function: 0x07493488
function: 0x074935c8
function: 0x07493508
I am not sure if I am doing something horribly wrong here or there is actually a fatal bug in a normal use case in HC. In any case, I have also made an issue on the HC github page but I am not sure how maintained it is.

If you have any suggestions, feel free to give them to me!

User avatar
Sex machine
Posts: 4944
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands

Re: Fatal bug when using HC for collision detection

Post by bartbes » Fri Dec 08, 2017 9:15 pm

It should be 'HC.new(100)', with a dot, not a colon.

Posts: 4
Joined: Fri Dec 08, 2017 1:38 pm

Re: Fatal bug when using HC for collision detection

Post by Habba » Sat Dec 09, 2017 8:23 am

Many thanks! No idea how I overlooked that!

Post Reply

Who is online

Users browsing this forum: Exabot [Bot] and 7 guests