Page 2 of 2

Re: HardonCollider usage

Posted: Tue Nov 01, 2011 8:51 pm
by TechnoCat

Re: HardonCollider usage

Posted: Tue Nov 01, 2011 9:47 pm
by vrld
Kazagha wrote:I have tried increasing the size of the scanner line, changed to "HC.init(5, on_collision)" down from 100, as I was of the understanding that it's the size of the scanning grid, I don't know why it won't detect the collision.
Don't do that: it will make things really slow! It's not a scanning grid, but the "bucket size" of a field of buckets that is used as first step to decide whether two shapes collide or not:
Each shape is contained in at least one bucket. To find all shapes that might collide with a shape A, you simply have to look what other shapes are in the same buckets as A. That way, you don't have to check every other shape as collision candidate.
But if the buckets are too small, HC has to look at a lot of buckets, possibly even more than there are shapes.
A good bucket size would be that of the largest object in the game.

As for collision dispatching, I use this method:

Code: Select all

function on_collide(_,a,b,dx,dy)
    a.object:collideWith(b.object, dx,dy)
    b.object:collideWith(a.object, dx,dy)
end

function stop_collide(_,a,b,dx,dy)
    a.object:stopCollide(b.object, dx,dy)
    b.object:stopCollide(a.object, dx,dy)
end

function make_collidable(class)
	class._onCollide = {}
	class._onStopCollide = {}

	function class:onCollide(group, reaction)
		self._onCollide[group] = reaction
	end

	function class:onStopCollide(group, reaction)
		self._onStopCollide[group] = reaction
	end

	function class:register(group, onCollide, onStop)
		class:onCollide(group, onCollide)
		class:onStopCollide(group, onStop)
	end

	function class:collideWith(other,dx,dy)
		(self._onCollide[other.group] or self._onCollide["*"] or _NULL_)(self, other, dx,dy)
	end

	function class:stopCollideWith(other,dx,dy)
		(self._onStopCollide[other.group] or self._onStopCollide["*"] or _NULL_)(self, other, dx,dy)
	end
end
The shapes contain references to the parent objects, which in turn have references to the shape (don't worry, the garbage collector can handle this), e.g.:

Code: Select all

Scrap = Class{function(self, x,y)
	self.sprite = math.random(1, #Scrap.sprites.frames)
	self.shape = HC.addCircle(x,y,12)
	self.shape.object = self
	self.group = "Scrap"

	self.rotvel = (math.random() * 2 - 1) * .5
	self.velocity = vector(10,0):rotate_inplace(math.random() * 2 * math.pi)
	self.velocity = vector(0,0)

	HC.setPassive(self.shape)
	Entities.add(self, Entities.MID)
end}
Scrap.sprites = newAnimation(Image.scrap, 32,32, 0,6)
make_collidable(Scrap)

function Scrap:draw()
	love.graphics.setColor(1,1,1)
	local x,y = self.shape:center()
	Scrap.sprites:drawFrame(self.sprite, x,y, self.shape:rotation(),1,1, 16,16)
end

function Scrap:update(dt)
	self.shape:move((self.velocity * dt):unpack())
	self.shape:rotate(self.rotvel * dt)
end

local spoken = {}
local messages = {
	"don't listen to her",
	"it's a trap!",
	"she only wants consume you",
	"stay here",
	"stay with us",
	"all the others are dead",
}

local bag = Randombag(1,#messages)
bag.bag = {2,1}
Scrap:onCollide("Magnet", function(self)
	if not spoken[self] then
		local t = Textfader(messages[bag:next()], Font[60], 2.5, .1, .2, vector(self.shape:center()))
		spoken[self] = true
	end
end)

Scrap:onCollide("Bomb", function(self,other,dx,dy)
	if not self.magnet then return end
	self.magnet:scrapHitBomb(vector(other.shape:center()), vector(dx,dy))
end)

Re: HardonCollider usage

Posted: Wed Nov 02, 2011 2:59 am
by Kazagha
Thanks guys, it works a charm now.

-Kazagha