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)
Code: Select all
self.shape = world:rectangle(self:getRect())
Code: Select all
self.shape:draw('line')
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
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...