Collisions and ipairs! Eep!

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
hiccupface
Prole
Posts: 14
Joined: Sat Apr 25, 2015 7:55 pm

Collisions and ipairs! Eep!

Post by hiccupface »

Here's a lobster-y problem that I've been trying to figure out with no luck. Can you help me? (Also, bear with me - I'm no Lua wiz... yet)

So, in my game, there are two players, Crusader Red and Crusader Blue. They can each press a key to summon a lobster. Fun!

Code: Select all

function crusaderControl(crusaderTable, dt, up, down, left, right, shoot)
    -- movement code here. blahblah...

    -- The fun stuff: lobster summoning!
    if love.keyboard.isDown(shoot) and crusaderTable.canShoot then
        newlobster = { 
			x = (crusaderTable.x)+21, -- spawn from center of player
			y = (crusaderTable.y)+32,
	        img = crusaderTable.lobsterImg,
	        speed = 50
	        } 
        table.insert(crusaderTable.lobsters, newlobster)
        crusaderTable.canShoot = false
        crusaderTable.canShootTimer = crusaderTable.canShootTimerMax
        crusaderTable.lobsterCount = crusaderTable.lobsterCount + 1
    end
end
Now using the simple collision check below, I can have these summoned lobsters collide with the other player or objects in the environment. Such as:

Code: Select all

--collision check function, from the love wiki:

function CheckCollision(x1,y1,w1,h1, x2,y2,w2,h2)
	return 	x1 < x2+w2 and
			x2 < x1+w1 and
			y1 < y2+h2 and
			y2 < y1+h1
end

--Now the fun fun stuff. A lobster collides with another player.

for i, lobster in ipairs(crusaderTable.lobsters) do
	
--If player generated lobsters collide with other player crusader,
--the lobster is destroyed and you get 1000 pts.

	if CheckCollision (lobster.x, lobster.y, lobster.img:getWidth(), lobster.img:getHeight(), theirCrusader.x, theirCrusader.y, theirCrusader.width, theirCrusader.height) then 
		crusaderTable.score = crusaderTable.score +1000
	end 
end
Now my stuggle: to add some icing to the cake, I want to have something happen when opposing player lobsters collide. I can't seem to wrap my head around it. How can I check the lobsters in ipairs(crusaderRed) vs those in ipairs(crusaderBlue). Does this make any sense?
szensk
Party member
Posts: 155
Joined: Sat Jan 19, 2013 3:57 am

Re: Collisions and ipairs! Eep!

Post by szensk »

That's a double for loop then, eh?

Some pseudocode:

Code: Select all

for i, redLobster in ipairs(crusaderRed.lobsters) do 
     for i, blueLobster in ipairs(crusaderBlue.lobsters) do
        if CheckCollision(...) then --arguments omitted for brevity
             --do stuff here
        end
     end
end
hiccupface
Prole
Posts: 14
Joined: Sat Apr 25, 2015 7:55 pm

Re: Collisions and ipairs! Eep!

Post by hiccupface »

Szensk, thanks so much, that worked!

I was trying to implement a nested loop earlier but I was doing it wrong.

So does this mean that whenever you call the code below, 'nameofthing' is a variable that is created specifically for that loop? Lua figures out the rest? I don't know how to explain it better than that...

Code: Select all

for i, nameofthing in ipairs(object.table) do
User avatar
MadByte
Party member
Posts: 533
Joined: Fri May 03, 2013 6:42 pm
Location: Braunschweig, Germany

Re: Collisions and ipairs! Eep!

Post by MadByte »

hiccupface wrote:Szensk, thanks so much, that worked!

I was trying to implement a nested loop earlier but I was doing it wrong.

So does this mean that whenever you call the code below, 'nameofthing' is a variable that is created specifically for that loop? Lua figures out the rest? I don't know how to explain it better than that...

Code: Select all

for i, nameofthing in ipairs(object.table) do
not exactly. You can think of it like a function that return two values. The first is the "key" or "index" and the second one is the "value".
How you name these variables doesnt matter. So, to give an example: If you have a table called "enemies" and each element in this table is another table with it's own functions (i.e update or draw the element), then you can do something like this:

Code: Select all

function updateEnemies(dt)
  for i, enemy in ipairs(enemies) do
    enemy:update(dt)
  end
end
In other words, the variable you set is pointing to every single table in your "container table".
The following code do kinda the same:

Code: Select all

function updateEnemies(dt)
  for i, #enemies do
    local enemy = enemies[i]
    enemy:update(dt)
  end
end
http://www.lua.org/pil/7.3.html
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 4 guests