How to handle collision resolutions?

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.
onedaysnotice
Citizen
Posts: 63
Joined: Sun May 13, 2012 2:49 am

How to handle collision resolutions?

Post by onedaysnotice » Sun Jul 01, 2012 1:41 am

||===========onedaysnotice help thread============||

**I moved the thread to this board since its more appropriate :D**

Before i infuriate the whole love2d community with my incessant making of 'help me' and 'how to' threads, I've decided to create a centralised thread xD

Current problem: viewtopic.php?p=60456#p60456

------------------------------------------------------------------------------------------------------------

Issue #1
--------------------------------
STATUS: resolved.

if you remove elements of a table using table.remove(), does the next existing element become index 1, or does it remain the same index and new elements will just replace the empty indexes?

like if I have a table:

a = {}
a[1] = 4
a[2] = 2
a[3] = 7

if I remove a[1] and a[2], does a[3] become index 1 afterwards?

thanks :D Sorry for the noob question xD.
Last edited by onedaysnotice on Sun Jul 08, 2012 1:08 pm, edited 15 times in total.

User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Denver, CO
Contact:

Re: table.remove question

Post by TechnoCat » Sun Jul 01, 2012 1:46 am

onedaysnotice wrote:if I remove a[1] and a[2], does a[3] become index 1 afterwards?
http://codepad.org/dd5AWIeT

Code: Select all

sample = {"hello", "felicia", "day"}
print(sample[1])
table.remove(sample,1)
table.remove(sample,1)
print(sample[1])

Code: Select all

hello
day

onedaysnotice
Citizen
Posts: 63
Joined: Sun May 13, 2012 2:49 am

Re: table.remove question

Post by onedaysnotice » Sun Jul 01, 2012 1:57 am

TechnoCat wrote:
onedaysnotice wrote:if I remove a[1] and a[2], does a[3] become index 1 afterwards?
http://codepad.org/dd5AWIeT

Code: Select all

sample = {"hello", "felicia", "day"}
print(sample[1])
table.remove(sample,1)
table.remove(sample,1)
print(sample[1])

Code: Select all

hello
day
ok good. thanks :D

finally had the sense to check the tables library tutorial at:
http://lua-users.org/wiki/TableLibraryTutorial

and it clearly states "The remaining elements are reindexed sequentially and the size of the table is updated to reflect the change."

*facepalm*. Sorry for the stupid questions guys :S.

onedaysnotice
Citizen
Posts: 63
Joined: Sun May 13, 2012 2:49 am

Issue #2

Post by onedaysnotice » Sun Jul 01, 2012 5:04 am

STATUS: resolved.


How can you print the value of input_time in this table?

Code: Select all

table.insert(box[i].keyCompare, {input_time = hidden_timer})
I've tried:

Code: Select all

	for i = 1,2 do 
		if #box[i].keyCompare.input_time > 0 then
			table.foreachi(box[i].keyCompare.input_time, print)
		end
	end

Code: Select all

	for i = 1,2 do
		if #box[i].keyCompare.inputTimer > 0 then
			love.graphics.print(box[i].keyCompare.inputTimer[1], 100, 170)
			if #box[i].keyCompare.inputTimer > 1 then
				love.graphics.print(box[i].keyCompare.inputTimer[2], 100, 170)
			end
		end
	end
and

Code: Select all

	for j = 1,2 do
		for i,v in ipairs(box[j].keyCompare.input_time) do
			love.graphics.print(v.keyCompare.input_time, 100, 170)
		end
	end
but all of them have either gotten nil error, or just printed the table id in terminal. :S
Attachments
Archive.love
I've commented out my tries
(1.73 KiB) Downloaded 87 times
Last edited by onedaysnotice on Wed Jul 04, 2012 7:34 am, edited 3 times in total.

User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: how to print the values inside a sub, sub, sub table? lo

Post by bartbes » Sun Jul 01, 2012 9:47 am

You shouldn't use table.foreach, btw, that's deprecated.

Code: Select all

for i, v in ipairs(box[i].keyCompare) do
    print(v.input_time)
end
If you check your table.insert you can see you're inserting into box.keyCompare, so that's what you should be interating over.

onedaysnotice
Citizen
Posts: 63
Joined: Sun May 13, 2012 2:49 am

Re: how to print the values inside a sub, sub, sub table? lo

Post by onedaysnotice » Sun Jul 01, 2012 10:10 am

bartbes wrote:You shouldn't use table.foreach, btw, that's deprecated.

Code: Select all

for i, v in ipairs(box[i].keyCompare) do
    print(v.input_time)
end
If you check your table.insert you can see you're inserting into box.keyCompare, so that's what you should be interating over.


I tried plugging that in, but it just gets the nil value error as well :S

Zeliarden
Party member
Posts: 139
Joined: Tue Feb 28, 2012 4:40 pm

Re: how to print the values inside a sub, sub, sub table? lo

Post by Zeliarden » Sun Jul 01, 2012 10:23 am

Try this one. It uses a "table to string" to show what happens when you press keys. Do you realy need a table for input_time?
Attachments
ArchiveX.love
with (tabel)tostring
(2.41 KiB) Downloaded 87 times

onedaysnotice
Citizen
Posts: 63
Joined: Sun May 13, 2012 2:49 am

Re: how to print the values inside a sub, sub, sub table? lo

Post by onedaysnotice » Sun Jul 01, 2012 10:07 pm

Zeliarden wrote:Try this one. It uses a "table to string" to show what happens when you press keys. Do you realy need a table for input_time?
Oh yay :D That works perfectly :D Thanks so much!

and ummm...I probably don't need to but...idk xD

hrmm to continue with this version or my new simplified one...

EDIT: Now that I can see the values of the input_time, it looks like keyCompare is not doing what I want it to. So I guess I might as well just stick to my simplified version.

onedaysnotice
Citizen
Posts: 63
Joined: Sun May 13, 2012 2:49 am

Issue #3 - How to make a wait function?

Post by onedaysnotice » Wed Jul 04, 2012 7:31 am

STATUS: Scrapped.

I am trying to make a wait function in an external file that outputs false until the specified time, at which it becomes true. I want it to be easily integrateable to any part of any code. I am almost certain that there is a much, much easier way of doing this but this is what I've got so far. (Hint: It doesn't work xD)

Code: Select all

local time = {}
time.__index = time

function newWait(seconds)
	local t = {}
	t.elapsed = 0
	t.total = seconds
	t.waiting = false
end

function time:update(dt)
	if self.waiting then
		self.elapsed = self.elapsed + dt
		if self.elapsed >= self.total then 
			return true end
			self.elapsed = 0
			self.waiting = false
		end
	end
end

function time:reset()
	self.elapsed = 0
end

function time:wait()
   self:reset()
	self.waiting = true
end
I have no idea what most of the code means btw, I was just tailoring Bartbes' AnAL code to suit this function lmao.

Can you please tell me how to change the code so that it works, or an easier method of solving this issue? :)
Last edited by onedaysnotice on Sun Jul 08, 2012 1:09 pm, edited 1 time in total.

User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: How to make a Wait function? :s

Post by Robin » Wed Jul 04, 2012 7:47 am

First:

Code: Select all

function newWait(seconds)
   local t = setmetatable({}, time) -- you need to set the metatable of t to time
   t.elapsed = 0
   t.total = seconds
   t.waiting = false
   return t -- if you don't return the table, you can't use it
end
Fun fact, you can make the above one line:

Code: Select all

function newWait(seconds)
   return setmetatable({elapsed = 0, total = seconds, waiting = false}, time)
end
Secondly, time.update should be different:

Code: Select all

function time:update(dt)
   if self.waiting then
      self.elapsed = self.elapsed + dt
      if self.elapsed >= self.total then
         self.elapsed = 0
         self.waiting = false
         return true -- NOT "return true end", that makes no sense
                     -- also notice you need to return AFTER you reset those properties, not before
      end
   end
end
Help us help you: attach a .love.

Post Reply

Who is online

Users browsing this forum: ki and 30 guests