Hardon Collider remove shape

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
lordlight7
Prole
Posts: 6
Joined: Mon Jan 23, 2012 8:39 am

Hardon Collider remove shape

Post 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.
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Hardon Collider remove shape

Post by tentus »

Shouldn't HC.remove(obj) be HC:remove(obj) ?
Kurosuke needs beta testers
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Hardon Collider remove shape

Post by bartbes »

tentus wrote:Shouldn't HC.remove(obj) be HC:remove(obj) ?
I thought HC became a singleton (so no).
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Hardon Collider remove shape

Post 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.
Kurosuke needs beta testers
lordlight7
Prole
Posts: 6
Joined: Mon Jan 23, 2012 8:39 am

Re: Hardon Collider remove shape

Post 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.
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: Hardon Collider remove shape

Post 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()).
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 3 guests