Table Spawning and Collision

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
User avatar
tochy97
Prole
Posts: 24
Joined: Sat May 24, 2014 5:06 pm
Location: Dallas, Texas

Table Spawning and Collision

Post 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.
caldur
Prole
Posts: 20
Joined: Tue Mar 13, 2012 3:30 pm

Re: Table Spawning and Collision

Post 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
User avatar
tochy97
Prole
Posts: 24
Joined: Sat May 24, 2014 5:06 pm
Location: Dallas, Texas

Re: Table Spawning and Collision

Post by tochy97 »

Thank you. read through both the bumb.lua and the hardoncollider.lua
Thanks for your help. <3
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 43 guests