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
SuperMeijin
Prole
Posts: 15
Joined: Wed Oct 03, 2012 3:37 pm

Collision

Post by SuperMeijin »

Hello again
I have a problem.
This is my collision code for my bullet and enemy

Code: Select all

local remEnemy = {}
	local remShot = {}
	for i,v in ipairs(player.shots) do
        v.x = v.x + 5
		if v.x > 800 then
		    table.insert(remShot, i)
		end
        for ii,vv in ipairs(enemies) do
	        if CheckCollision(v.x,v.y,2,5,vv.x,vv.y,vv.width,vv.height) then
			    table.insert(remEnemy, ii)
				table.insert(remShot, i)
		    end
	    end
	end
    for i,v in ipairs(remEnemy) do
	    table.remove(enemies, v)
	end
	for i,v in ipairs(remShot) do
	    table.remove(player.shots, v)
    end
I tried replicating this for my player vs enemy collision but it does not work what part is wrong

Code: Select all

local remPlayer = {}
	for i,v in ipairs(player) do
        for ii,vv in ipairs(enemies) do
	        if CheckCollision(v.x,v.y,2,5,vv.x,vv.y,vv.width,vv.height) then
			    table.insert(remPlayer, i)
		    end
	    end
	end
    for i,v in ipairs(remPlayer) do
	    table.remove(player, v)
    end
Here is my enemy and player tables

Code: Select all

	enemies = {} -- enemy mutiple spawn
    for i=0,0 do
        enemy = {}
        enemy.width = 72
		enemy.height = 102
		enemy.x = math.random(0,728)
        enemy.y = math.random(0,428)
    table.insert(enemies, enemy)
    end

Code: Select all

    player = {}
        player.width = 72
		player.height = 102
		player.x = 365
        player.y = 260
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Collision

Post by Robin »

Notice your enemies table is a sequence of enemy object, while player is the player object itself.

I would suggest doing this:

Code: Select all

-- have in love.load:
playerDead = false

-- then, in your update function:
    for ii,vv in ipairs(enemies) do
       if CheckCollision(player.x,player.y,2,5,vv.x,vv.y,vv.width,vv.height) then
           playerDead = true
       end
    end
Your enemy/shot collision code also has a problem: it removes the wrong ones if there's more than one collision at the same time. (I can explain why if you can't figure it out yourself). Here's a solution:

Code: Select all

    table.sort(enemies)
    for i = #remEnemy, 1, -1 do
       table.remove(enemies, remEnemy[i])
   end
    for i = #remShot, 1, -1 do
       table.remove(enemies, remShot[i])
   end
Also, please fix your indentation. It makes your code look sloppy and makes your code confusing to read.
Help us help you: attach a .love.
SuperMeijin
Prole
Posts: 15
Joined: Wed Oct 03, 2012 3:37 pm

Re: Collision

Post by SuperMeijin »

Thanks for explanation very useful. As for the indentation it seems to have gone wrong when I copied it over from notepad++.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Collision

Post by Robin »

SuperMeijin wrote:As for the indentation it seems to have gone wrong when I copied it over from notepad++.
That probably means there were tabs mixed with spaces in there, which can result in pretty confusing things when copy/pasting or viewing it on another computer or with another editor (tabs are converted to different number of spaces). There is an option to make whitespace visible, you can use that to see if that's really what's going on.
Help us help you: attach a .love.
Post Reply

Who is online

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