Enemies Overlapping

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
izaDEgo
Prole
Posts: 9
Joined: Sun Feb 08, 2015 2:14 am

Enemies Overlapping

Post by izaDEgo »

Hello, can anyone give me an example code to keep my enemies from colliding and overlapping.

Here is my enemy.lua they spawn perfectly its just they overlap & collide when chasing the player.

Code: Select all

enemy = {}
enemy.timer = 0
enemy.timerLim = math.random(3,5)
enemy.amount = math.random(1,3)
enemy.side = math.random(1,4)

function enemy.generate(dt)
	enemy.timer = enemy.timer + dt
	if enemy.timer > enemy.timerLim then
		for i=1,enemy.amount do
			if enemy.side == 1 then --left
				enemy.spawn(-20,screenHeight / 2 - 25)
			end
			if enemy.side == 2 then --top
				enemy.spawn(screenWidth / 2 - 25, -20)
			end
			if enemy.side == 3 then --right
				enemy.spawn(screenWidth,screenHeight / 2 - 25)
			end
			if enemy.side == 4 then --bottom
				enemy.spawn(screenWidth / 2 - 25, screenHeight)
			end
			enemy.side = math.random(1,4)
		end
		enemy.amount = math.random(1,3)
		enemy.timerLim = math.random(3,5)
		enemy.timer = 0
	end
end

enemy.width = 20
enemy.height = 20
enemy.speed = 1500
enemy.friction = 7.5
enemy.gravity = 700

function enemy.spawn(x,y)
	table.insert(enemy, { x = x, y = y, xvel = 0, yvel = 0, health = 2, width = enemy.width, height = enemy.height })
end

function enemy.draw()
	for i,v in ipairs(enemy) do
		love.graphics.rectangle('fill',v.x,v.y,enemy.width,enemy.height)
	end
end

function enemy.physics(dt)
	for i,v in ipairs(enemy) do
		v.x = v.x + v.xvel * dt
		v.y = v.y + v.yvel * dt
		v.xvel = v.xvel * (1 - math.min(dt*enemy.friction, 1))
		v.yvel = v.yvel * (1 - math.min(dt*enemy.friction, 1))
	end
end

function enemy.AI(dt)
	for i,v in ipairs(enemy) do
		if player.x + player.width / 2 < v.x + v.width / 2 then
			if v.xvel > -enemy.speed then
				v.xvel = v.xvel - enemy.speed  * dt
			end
		end
		if player.x + player.width / 2 > v.x + v.width / 2 then
			if v.xvel < enemy.speed then
				v.xvel = v.xvel + enemy.speed  * dt
			end
		end
		if player.y + player.height / 2 < v.y + v.height / 2 then
			if v.yvel > -enemy.speed then
				v.yvel = v.yvel - enemy.speed * dt
				end
			end
		if player.y + player.height / 2 > v.y + v.height / 2 then
			if v.yvel < enemy.speed then
				v.yvel = v.yvel + enemy.speed * dt
			end
		end
	end
end

function enemy.bulletcollide()
	for i,v in ipairs(enemy) do
		for ia,va in ipairs(bullet) do
			if va.x + va.width > v.x and
			va.x < v.x + v.width and
			va.y + va.height > v.y and
			va.y < v.y + v.height then
				table.remove(enemy, i)
				table.remove(bullet, ia)
			end
		end
	end
end


function enemy.collide()
	for i,v in ipairs(enemy) do
			if v.x < 0 then
		v.x = 0
	end
	if v.y < 0 then
		v.y = 0
	end
	if v.x > 777 then
		v.x = 777
	end
	if v.y > 577 then
		v.y = 577
	end
	end
end



function DRAW_ENEMY()
	 enemy.draw()
end

function UPDATE_ENEMY(dt)
	enemy.physics(dt)
	enemy.AI(dt)
	enemy.collide()
	enemy.generate(dt)
	enemy.bulletcollide()
	enemy.overlapping()
end
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Enemies Overlapping

Post by s-ol »

Check The distance, and if its smaller than the sum of radius' of an enemy and the player then dont move.

If you then need additional physics, for example to collide with walls please search around the forum, about 10% of threads deal with that problem.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
izaDEgo
Prole
Posts: 9
Joined: Sun Feb 08, 2015 2:14 am

Re: Enemies Overlapping

Post by izaDEgo »

S0lll0s wrote:Check The distance, and if its smaller than the sum of radius' of an enemy and the player then dont move.

If you then need additional physics, for example to collide with walls please search around the forum, about 10% of threads deal with that problem.
Thanks for the advice but my wall collide is working perfectly with enemys and i figured it out.

Now they collide sometimes but they spread apart once they touch.

Code: Select all

function enemy.collidewith()
	for i,v in ipairs(enemy) do
		for ia,va in ipairs(enemy) do
			if va.x + va.width > v.x and
			va.x < v.x + v.width and
			va.y + va.height > v.y and
			va.y < v.y + v.height then
				v.xvel = v.xvel + math.random(-100,100)
				v.yvel = v.yvel + math.random(-100,100)
				end
			end
		end
end
I posted it just in case anyone would like to use it.
User avatar
nfey
Citizen
Posts: 63
Joined: Tue Feb 18, 2014 9:50 am
Location: Romania

Re: Enemies Overlapping

Post by nfey »

Just a quick tip, try to contain your most used logic in it's own method. For example, increasing horizontal and vertical velocities should each have it's own method. It's easier to debug and trace problems back to a specific batch of code if it's only written once in a method instead of duplicated all around the code base.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 5 guests