Page 1 of 1
Hardon Collider remove shape
Posted: Thu Feb 09, 2012 5:09 pm
by lordlight7
I am using hardon collider to detect my collision.
I have a problem in this code:
Code: Select all
for _,obj in pairs(enemiess) do
if atack:collidesWith(obj) then
HC.remove(obj)
player.health = player.health + 1000 -- just to check if the collision still works
end
i have a table for enemies,like this,and i tried to remove the "enemy1" for example for the table
Code: Select all
enemiess = {
enemy1=HC.addRectangle(blabla)
... and more enemies
}
I can't figure out why i can stand on the enemy even after the HC.remove(shape) and get 1000 more health.
Re: Hardon Collider remove shape
Posted: Thu Feb 09, 2012 5:58 pm
by tentus
Shouldn't HC.remove(obj) be HC:remove(obj) ?
Re: Hardon Collider remove shape
Posted: Thu Feb 09, 2012 6:23 pm
by bartbes
tentus wrote:Shouldn't HC.remove(obj) be HC:remove(obj) ?
I thought HC became a singleton (so no).
Re: Hardon Collider remove shape
Posted: Thu Feb 09, 2012 7:04 pm
by tentus
Got it. The below code works for me:
Code: Select all
function love.load()
HC = require('hardoncollider')(100, on_collide)
hero = HC:addCircle(300, 100, 25)
shapes = {}
for i=1, 4 do
shapes[i] = HC:addRectangle(i * 100, i * 100, 100, 100)
end
end
function love.update(dt)
local x, y, s = 0, 0, 200 * dt
if love.keyboard.isDown("left") then x = -s
elseif love.keyboard.isDown("right") then x = s
end
if love.keyboard.isDown("up") then y = -s
elseif love.keyboard.isDown("down") then y = s
end
hero:move(x, y)
HC:update(dt)
end
function love.draw()
hero:draw('line', 24)
for i=1, #shapes do
shapes[i]:draw('line')
end
end
function love.mousereleased(x, y, b)
for i=1, #shapes do
if shapes[i]:contains(x, y) then
HC:remove(shapes[i])
table.remove(shapes, i)
break
end
end
end
function on_collide(dt, a, b, x,y)
a:move(x,y)
end
I think that the problem is that the terms HC and Collider are easily switched in different implementations. In some implementations, HC is what the require returns. In my example, it's an individual initialization.
Re: Hardon Collider remove shape
Posted: Thu Feb 09, 2012 8:13 pm
by lordlight7
i found this on the official site of hardon collider,but i don't know how to "refresh" the collision...
Code: Select all
Remove a shape from the collision detection system. Note that if you remove a shape in the collide() callback, other shapes might still have collided with it, so the shape will be argument to the other calls of collide(). In any case, the stop() callback will be called in the next call to update for each shape which the removed shape collided with.
Re: Hardon Collider remove shape
Posted: Thu Feb 09, 2012 10:08 pm
by vrld
lordlight7 wrote:I can't figure out why i can stand on the enemy even after the HC.remove(shape) and get 1000 more health.
It's a shot in the blue (
since you did not provide a .love), but I guess it's due to two things:
Firstly, you roll your own collision detection loop (which is fine), but at the same time use the full collider API (which does it's own collision detection). If you use the full API, collisions are reported by the callback-system and there is no need for shape:collidesWith(other).
If you don't, you should not create shapes with Collider:addRectangle(...), but rather using shapes.new<FOO>Shape().
Secondly, you remove a colliding object from the internal object manager (via HC.remove(obj)), but not from the enemies table (which you should do, if you use shape:collidesWith()).