Page 2 of 2

Re: New to lua, need help with tables (and now collisions)

Posted: Thu Oct 28, 2010 10:04 am
by nevon
First of all, create a function to handle your collision checking. That will make things a lot more readable. For example, this one (you're welcome) checks a rectangle against a circle (which seems like what you want to do):

Code: Select all

function circRectCollision(rectx, recty, rectwidth, rectheight, pointx, pointy, radius)
	local pow = math.pow
	local root = math.sqrt
	--Check if the projectile is inside the enemy bounding box
	if pointx > rectx-radius and pointy > rectyy-radius and pointx < rectx+rectwidth+radius and pointy < recty+rectheight+radius then
		return true
	--Check if the projectile is touching the corner of the rect bounding box
	elseif root( pow( rectx-pointx, 2 ) + pow( recty-pointy, 2 ) ) < radius or
	   root( pow( rectx+rectwidth-pointx, 2 ) + pow(recty-pointy, 2 ) ) < radius or
	   root( pow( rectx-pointx, 2 ) + pow( recty+rectheight-pointy, 2 ) ) < radius or
	   root( pow( rectyx+rectwidth-pointx, 2 ) + pow( recty+rectheight-pointy, 2 ) ) < radius then
		return true
	end
end
Then you would do something like this:

Code: Select all

function love.update(dt)
    local enemyremovetable = {}
    local projectileremovetable = {}

    for i,v in ipairs(enemies) do
        for n,k in ipairs(projectiles) do
            
            if circRectCollision( v.x, v.y, 32, 32, k.x, k.y, 1 ) then
                table.insert(enemyremovetable, i)
                table.insert(projectileremovetable, n)
            end

        end
    end

    for i,v in ipairs(enemyremovetable) do
        table.remove(enemies, v-i+1)
    end

    for i,v in ipairs(projectileremovetable) do
        table.remove(projectiles, v-i+1)
    end
end

Re: New to lua, need help with tables (and now collisions)

Posted: Thu Oct 28, 2010 10:20 am
by fylth
You sir are a gentleman of the highest order, with a bit of tweaking that worked a treat! Have some karma :)

Just going to play around with this a bit more (adding a health variable which is decremented on collision due to the amazingly huge amount of bullets my player spews forth, then get some increasingly hard waves) and then get to work on scoring and a menu, then I will have completed my first (amazingly simple) game! Hooray!

*EDIT*

Hate to keep asking questions, this one's to do with tables again though and I just need to know the correct syntax to deal with it.

I'm trying to get the enemies hp to reduce by 1 per hit, so in the deceleration for the enemy type I have

Code: Select all

enemy1 = {
	image = love.graphics.newImage("enemy1.png"),
	x = 0,
	y = -32,
	hp = 30
	}
And it i've changed the code for entering each enemy in the enemy table to

Code: Select all

		table.insert(enemy, {x = (enemy1.x + randnum), y = enemy1.y, image = enemy1.image, hp = 30})
However, i'm not sure how to now effect this variable now in the collision code, at the moment I have

Code: Select all

enemy(i,v).hp= enemy(i,v).hp - 1
which I know isn't right and I must've tried a hundred variations trying to use examples from earlier in the code but none seem to work...so, a little bit more help? Much appreciated, again sorry for asking for help so often

Re: New to lua, need help with tables (and now collisions)

Posted: Thu Oct 28, 2010 11:47 am
by kikito
I would get rid of the initial 'enemy1'. Use variables instead:

Code: Select all

  image = love.graphics.newImage("enemy1.png")
  enemy_y = -32
I also recommend renaming your 'enemy' variable to 'enemies'; this name is more appropiate since the variable holds more than one enemy.

So your enemies can now be created like this:

Code: Select all

table.insert(enemies, {x = randnum, y = enemy_y, image = image, hp = 30})
Which is just a bit simpler.

You nearly got the e syntax right. But remember that if you want to access the attribute x of a table you have to use brackets: table[x], not table(x)

Code: Select all

enemies[i].hp= enemies[i].hp - 1
I also removed the 'v'; I didn't know what it was, but probably it didn't belong there (enemies is a mono-dimensional table, and you can't index it with two variables ).

Re: New to lua, need help with tables (and now collisions)

Posted: Thu Oct 28, 2010 12:58 pm
by zac352
Aww. I missed out. :d

Re: New to lua, need help with tables (and now collisions)

Posted: Fri Oct 29, 2010 8:14 pm
by fylth
Well, this appears to be becoming a thread where by I post all progress and problems I'm having with this game, so why not continue haha :)

I have a...perplexing bug (for me, at least), which is when one enemy leaves the bottom of the screen the next enemy on the screen is removed instead (or as well? not sure what happens to the ones which make it off screen, if they aren't being removed it might explain the slow down later down).

Here's the code for the enemy removal section, see any problems?

Code: Select all

for i,v in ipairs(enemy) do
		v.y = v.y + 0.6
		if v.y > 600 then
			table.insert(removeenemy, i)
			health = health - 5
		end
	end

--big chunk of code for other stuff, I'm not very organised

 for i,v in ipairs(removeenemy) do
       table.remove(enemy, v-i+1)
    end
I'll attach the file itself in case anyone wants to take a look?

(Oh, and if anyone can recommend a way of lessening the slow down when firing that would be appreciated)

Re: New to lua, need help with tables (and now collisions)

Posted: Fri Oct 29, 2010 8:53 pm
by Robin
Using a remove flag, rather than a remove queue might help:

Code: Select all

for i,v in ipairs(enemy) do
	v.y = v.y + 0.6
	if v.y > 600 then
		v.remove = true
		health = health - 5
	end
end

-- ...

for i = #enemy, 1, -1 do
	if enemy[i].remove then
		table.remove(enemy, i)
	end
end
Hope this helps.