[Solved] Problem with HC
Posted: Tue Aug 06, 2019 5:57 pm
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) :
each entity contains a shape variable that I instanciate that way :
and draws a line around it for debugging purpose :
I have a class that handles gravity, and more specifically a function applying it to my moving entities (so, for now, only the player) :
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...
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...