Page 1 of 1

Table Spawning and Collision

Posted: Fri Oct 24, 2014 5:04 pm
by tochy97

Code: Select all

function zombie_spawn(x,y,id)
	table.insert(zombie, {x = x, y = y,id = id})
end

function zombie_draw()
	for i,z in ipairs(zombie) do
		love.graphics.draw(zombie.pic,z.x,z.y)
	end
end

function zombie_move(dt)
	for i,thisZombie in ipairs(zombie) do
		local dx,dy = thisZombie.x - player.x - pw, thisZombie.y-player.y
		local distance = math.sqrt(dx^2+dy^2)
		thisZombie.x = thisZombie.x - dx/distance * zombie.speed * dt
		thisZombie.y = thisZombie.y - dy/distance * zombie.speed * dt
	end
end
I am trying to find a way to check if the zombies that are being spawned in have collided with the player. They are automatically moving towards the player but i cant think of a way to check if the x of any zombie has collided with the x of the player. and if there is a way to stop them from stacking on top of each other. Any help will be appreciated.

Re: Table Spawning and Collision

Posted: Sat Oct 25, 2014 1:16 am
by caldur
Well, then you need to do collision detection (and properly resolve them).
If you won't have a large number of entities on screen then simple looping will do; represent entities with axis aligned bounding boxes and check collision among them (even at O(n^2) it should be acceptable).
However if you have a LOT zombies on the screen then consider employing some broad phase optimization. If you don't want to roll out you own, check bump.lua or HardonCollider. They all use space partritioing and offer collision resolution.
Besides, you can get a good idea of handling this from some of the articles under Physics / Movement section on the wiki

Re: Table Spawning and Collision

Posted: Tue Oct 28, 2014 4:53 pm
by tochy97
Thank you. read through both the bumb.lua and the hardoncollider.lua
Thanks for your help. <3