[Solved] Problem with HC

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Thorkal
Prole
Posts: 11
Joined: Tue Aug 06, 2019 4:42 pm

[Solved] Problem with HC

Post by Thorkal »

Hi everyone,

I want to use HC (HardonCollider) to handle collision in my project.
I have a level class which contains entities (for now, a player and two platforms, that display their own sprite). This class also contains an instance of HC (it's a global variable) :

Code: Select all

world = HC.new(1)
each entity contains a shape variable that I instanciate that way :

Code: Select all

self.shape = world:rectangle(self:getRect())
and draws a line around it for debugging purpose :

Code: Select all

self.shape:draw('line')
I have a class that handles gravity, and more specifically a function applying it to my moving entities (so, for now, only the player) :

Code: Select all

function Gravity:apply(dt)
  for k, entity in pairs(self.entities) do
    if (entity.collisionType == 'dynamic') then
      if not entity.isJumping then -- the object is led down only if is not accelerating upward
        entity.yVelocity = entity.yVelocity + entity.weight * dt
      end

      if entity.yVelocity-entity.oldYVelocity > 2 then
        entity.isGrounded = false
        entity.lockJump = true
      end

      local goalX = entity.x + entity.xVelocity
      local goalY = entity.y + entity.yVelocity
      local candidates = world:collisions(entity.shape)

      if #candidates == 0 then
        entity.x = goalX
        entity.y = goalY
        entity.shape:moveTo(entity.x+entity.w/2, entity.y+entity.h/2)
      else
        for other in pairs(candidates) do
          local collides, dx, dy = entity.shape:collidesWith(other)
          if collides then
            other:move(dx, dy)
            entity.x = entity.x + entity.xVelocity
            entity.y = entity.y + entity.yVelocity
          end
        end
      end
    end
  end
end
So, my player and platforms are displayed and their shape is displayed with the line around them, and when my player moves, the shape follows it. When I run the program, as you can imagine, my player falls through the platform. That's pretty normal since I know that the code in the for loop on "candidates" is wrong. What's not normal to me is that the "candidates" variable is always empty, even when the player passes through the platform.

I don't understand how it works. I tried to look at the HC code and it seems to update its spatialhash only when a shape is created, and not when it's moved...

If anyone can help...
Last edited by Thorkal on Wed Aug 07, 2019 3:20 pm, edited 1 time in total.
Thorkal
Prole
Posts: 11
Joined: Tue Aug 06, 2019 4:42 pm

Re: Problem with HC

Post by Thorkal »

Ok I resolved my problem, collisions are in fact detected, and there's no point in checking if #candidates > 0
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Amazon [Bot], Bing [Bot] and 0 guests