Help with collisions and removing from tables...

General discussion about LÖVE, Lua, game development, puns, and unicorns.
User avatar
xFade
Prole
Posts: 41
Joined: Mon Dec 23, 2013 6:04 pm

Help with collisions and removing from tables...

Post by xFade »

Code: Select all

function Coin:update(dt)
	for i,v in ipairs(Coin) do
		 v.x = v.x -- v is coin
		 v.y = v.y + .5
		 v.angle = v.angle + math.pi * dt
                -- Starting here is where I need help...
		 if v.y + Coin.model:getHeight() > 500 then
			table.remove( v, id )
		end
		if v.x + Coin.model:getHeight()  >= player.x  and v.x <= player.x + player.model:getWidth() and v.y + Coin.model:getHeight() >= player.y and v.y <= player.y + player.model:getHeight() then
			table.remove(v,id)
			cc = cc + 1
		end
	end
end
Okay so this function pretty much updates the coin as it falls down toward the player. My Issue is that I can't seem to remove it when either the player touches it or if it gets out of bounds. My other issue is cc = cc + 1. Which is the total amount of coins collected. However, whenever the player comes in contact with the falling coin instead of adding 1 coin it seems to add the amount of time the two were in contact. Help would be greatly appreciated :D, Thank you.
(ง'̀-'́)ง
User avatar
substitute541
Party member
Posts: 484
Joined: Fri Aug 24, 2012 9:04 am
Location: Southern Leyte, Visayas, Philippines
Contact:

Re: Help with collisions and removing from tables...

Post by substitute541 »

When you are using the colon operator, it sets an argument known as "self", which references itself. So you can make the code simpler and do this:

Code: Select all

function Coin:update(dt)
	self.x = self.x -- v is coin
	self.y = self.y + .5
	self.angle = self.angle + math.pi * dt
	
	if self.y + Coin.model:getHeight() > 500 then
		self.remove = true
	end
	if self.x + Coin.model:getHeight()  >= player.x and 
	   self.x <= player.self + player.model:getWidth() and
	   self.y + Coin.model:getHeight() >= player.y and
	   self.y <= player.y + player.model:getHeight() then
		 self.remove = true
		 cc = cc + 1
	end
end

-- somewhere inside of your update loop
for i=#coins, 1, -1 do
	local v = coins[i]
	if v.remove then
		table.remove(coins, i)
	end

	-- other update code...
end
"coins" should be your coins list. Also, on the loop, we are looping backwards so the loop doesn't do any funky stuff when we remove the coins.
Currently designing themes for WordPress.

Sometimes lurks around the forum.
User avatar
xFade
Prole
Posts: 41
Joined: Mon Dec 23, 2013 6:04 pm

Re: Help with collisions and removing from tables...

Post by xFade »

I can't thank you enough :D
(ง'̀-'́)ง
User avatar
xFade
Prole
Posts: 41
Joined: Mon Dec 23, 2013 6:04 pm

Re: Help with collisions and removing from tables...

Post by xFade »

What if I wasn't using a colon operator?
(ง'̀-'́)ง
User avatar
substitute541
Party member
Posts: 484
Joined: Fri Aug 24, 2012 9:04 am
Location: Southern Leyte, Visayas, Philippines
Contact:

Re: Help with collisions and removing from tables...

Post by substitute541 »

xFade wrote:What if I wasn't using a colon operator?
Use a table for each coin object, and store those coins in a coins table. Then loop over (preferably backwards, if you plan to remove some coins) and update each coin.
Currently designing themes for WordPress.

Sometimes lurks around the forum.
User avatar
xFade
Prole
Posts: 41
Joined: Mon Dec 23, 2013 6:04 pm

Re: Help with collisions and removing from tables...

Post by xFade »

Ehhh.. I'm sorry i'm a noob to Lua ,and have no idea how to do that ;-;
(ง'̀-'́)ง
User avatar
Jasoco
Inner party member
Posts: 3726
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Help with collisions and removing from tables...

Post by Jasoco »

The method I use for all entities:

Code: Select all

entitySample = {}

function entitySample:create(v)
	local _i = {}
	setmetatable(_i, {__index = self})
	_i:setup(v)
	return _i
end
function entitySample:setup(v)
	self.x = v.x or 0
	self.y = v.y or 0
	self.spinFrame = 0
	self.whatever = v.whatever or defaultValue
	...
end

function entitySample:update(dt)
	self.spinFrame = self.spinFrame + dt * self.spinSpeed
	if self.spinFrame > self.frameCount then self.spinFrame = 0 end
	...
end

function entitySample:draw()
	love.graphics.draw(self.image, self.quad[math.floor(self.spinFrame)], self.x, self.y)
	...
end
Of course this is just a sample, all you ned to know is the function creation stuff. You'd then create a new "coin" by calling coinEntity[#coinEntity+1] = entitySample:create {x = 100, y = 100} whenever you want to create one. And loop through all your entities and call their update functions in update and their draw functions in draw.

Someone else can elaborate but this is the gist of creating an "object" in Lua/Löve.
User avatar
xFade
Prole
Posts: 41
Joined: Mon Dec 23, 2013 6:04 pm

Re: Help with collisions and removing from tables...

Post by xFade »

Ah thanks for the clarification, and the example :)
(ง'̀-'́)ง
User avatar
xFade
Prole
Posts: 41
Joined: Mon Dec 23, 2013 6:04 pm

Re: Help with collisions and removing from tables...

Post by xFade »

Code: Select all

Taco = {}

ti = 0
tc = 0

tacopic = love.graphics.newImage("Index/taco.png")

function Taco:create(v)
   local _i = {}
   setmetatable(_i, {__index = self})
   _i:setup(v)
   return _i
end
function Taco:setup(v)
   self.x = v.x or 0
   self.y = v.y or 0
   self.angle = 0
   --self.whatever = v.whatever or defaultValue
end

function Taco:update(dt)
	self.angle = self.angle + math.pi * dt
end

function Taco:draw()
   love.graphics.draw(tacopic,self.angle, self.x, self.y)
end

timer1 = 0

function Taco_rain(dt)
	timer1 = timer1 + dt
	if timer1 >= .5 then
		x = math.random(50,690)
		sTaco[#sTaco+1] = Taco:create {x = x, y = 0}
		timer1 = 0
		ti = ti + 1
	end
end
So um I keep getting an error "attempt to perform arithmetic on field angle" a nil value... :/
(ง'̀-'́)ง
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Help with collisions and removing from tables...

Post by Robin »

I suspect your real problem hides somewhere else. Please upload a .love of your game, so that we can help you find it.

I did find something else that's wrong: love.graphics.draw(tacopic,self.angle, self.x, self.y) should be love.graphics.draw(tacopic, self.x, self.y, self.angle)
Help us help you: attach a .love.
Post Reply

Who is online

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