• Gwilty's Programming Assignments #2
    191 replies, posted
Nevermind, no idea what happened but it fixed itself.
I don't even know where to start.
[QUOTE=BlkDucky;33047392][media]http://www.youtube.com/watch?v=oRxv_HaKa-A[/media] eh[/QUOTE] Wow, I just realised that you can't see most of the particles in the video. :v:
Well, I got all the basics done. I never really realized this, but the combination of lua's power and simpleness with love's ease of use makes it (love) a really powerful tool. Also, I love the fact that the scope of anonymous functions includes that of the functions they're created in, so I can do this: [lua] local function firework(pos) local color = { math.random(0,255), math.random(0,255), math.random(0,255)} for i=1,250,1 do local temppos = randcircle(pos,50) particles:Add(Particle(temppos,(temppos-pos):normalized()*math.random(10,200), function(pos) --this function is the draw function for the Particle. It can access the local color variable even though its outside of its scope (is it?) love.graphics.setColor(color) love.graphics.circle("fill",pos.x,pos.y,1,20) end)) end end [/lua]
[QUOTE=All0utWar;33042631]Holy shit, I didn't even get the last assignment done. I actually quit because I couldn't figure out how to make the ball have gravity, make it bounce or add collisions.[/QUOTE] I've tried adding collision once, but it wouldn't work since it didn't detect the collision of the window and the paddle sprite. I could perform it with SDL but I've had problems initializing the window(it would open and close immediately in less than 1 second).
[CODE] function love.load() targX = 0 targY = 0 mouseX = 0 mouseY = 0 button = 0 end function love.mousepressed(mouseX, mouseY, button) if button == "l" then targX = mouseX targY = mouseY end end function love.draw() love.graphics.print("Put sprite here", targX, targY) love.graphics.print("Targ Pos: [:"..targX..","..targY.."]", 0, 0) love.graphics.print("Mouse Pos: [:"..mouseX..","..mouseY.."]", 0, 10) love.graphics.print("Button: [:"..button.."]", 0, 20) end [/CODE] Does anyone understand why it wont print my Mouse Pos/ button properly?
You never set [i]mouseX[/i] or [i]mouseY[/i] to anything, only [i]targX[/i] and [i]targY[/i]. The mouseX and mouseY that you get in the "mousepressed" event is only available for use in that function, as it's a local variable. If you want to get the current mouse's position, you would use: love.mouse.getX() and love.mouse.getY() For more about the mouse functions, see this page: [url]http://love2d.org/wiki/love.mouse[/url]
At least I'll have an excuse to stay in on the 5th. To watch my own private firework show.
Didn't have enough motivation to work on the other one, this one shall be different. It involves particles! I love particles! Anyhow, I'm working in XNA since that what I want to train myself in. [media]http://www.youtube.com/watch?v=sdL6QCb-y48[/media] Next up, trails and rockets.
[QUOTE=HeroicPillow;33051725]You never set [i]mouseX[/i] or [i]mouseY[/i] to anything, only [i]targX[/i] and [i]targY[/i]. The mouseX and mouseY that you get in the "mousepressed" event is only available for use in that function, as it's a local variable. If you want to get the current mouse's position, you would use: love.mouse.getX() and love.mouse.getY() For more about the mouse functions, see this page: [url]http://love2d.org/wiki/love.mouse[/url][/QUOTE] Thanks, I figured it was something small. EDIT: I fixed my debug completely. Now it's beautiful. Thanks guys!
[media]http://www.youtube.com/watch?v=eWqtpVw6Z7w[/media] it's like i'm really there
Any chance you'd be willing to share sounds? Also, I can't get my head around managing particles properly, how did you do it?
[QUOTE=Storm_Ninja;33055229]Any chance you'd be willing to share sounds? Also, I can't get my head around managing particles properly, how did you do it?[/QUOTE] [url]http://dl.dropbox.com/u/1032139/firework_sounds/bang.ogg[/url] [url]http://dl.dropbox.com/u/1032139/firework_sounds/sparkle1.ogg[/url] [url]http://dl.dropbox.com/u/1032139/firework_sounds/whistle.ogg[/url] The particles are a class with draw/think, every particle is listed in a Particles array. The love update calls the think for every particle, the render calls the draw for every particle. Positions are modified in the think, stuff like gravity, wind etc and it's rendered in draw. I don't know if that's the most common way of doing things or if there's better ways of doing it but it works for me so yeah
I've been doing the thinks inside, update. It works, but it's very complicated for more that a couple of types of particle
So I have a 'particle' drawing on the screen where I click, which is what I want it to do. So all is going well. I have this: [lua]particleList = {} Particle = {} Particle.__index = Particle function Particle.create(mouseX, mouseY) local part = {} setmetatable(part,Particle) part.X = mouseX part.Y = mouseY return part end function Particle:draw() love.graphics.circle("fill", self.X, self.Y, 10, 10) end function love.load() love.graphics.setMode(800, 600, false, false, 0) love.graphics.setBackgroundColor(0,0,0) end function love.update(dt) end function love.draw() love.graphics.setColor(255, 255, 255) love.graphics.print(#particleList, 10, 10) for i = 1, #particleList do particleList[i]:draw() end end function love.mousepressed() table.insert(particleList, Particle.create(love.mouse.getX(), love.mouse.getY())) end[/lua] Could someone explain to me what 'Particle.__index = Particle' actually does. As well as 'setmetatable(part,Particle)'. I used the example from the link in the op, but I dont quite understand their explanation.
Another entry! .love download: [url]http://www.filedropper.com/fireworks[/url] packed download: [url]http://www.filedropper.com/fireworks_1[/url]
Shit, this one is going to be more difficult.
[QUOTE=Darkest_97;33055763]Could someone explain to me what 'Particle.__index = Particle' actually does. As well as 'setmetatable(part,Particle)'. I used the example from the link in the op, but I dont quite understand their explanation.[/QUOTE] using your code example, when you have created your particle 'part', and then call part:draw(), you'll notice part never had a draw() function. However it notices it has a metatable, so it goes looking at that table instead. Normally just that table would be returned, but that table has __index set - so now it goes looking through the keys/values in the table that references, and lo and behold, Particle:draw() exists! Don't concern yourself too much about it unless you intend to use Lua a lot. Languages that handle classes do this stuff much cleaner. [Edit: ignore me and just read jA_c0p's explanation, it's much better] My entry so far! [url]http://dl.dropbox.com/u/12733613/fireworks.love[/url] I'll stick jimbo's sounds on before recording a video.
I'm having trouble clearing my table of particles as they're no longer used, I can overwrite the ones I'm done with, but it seems to either refuse to make the table value = nil or is just clears the whole table and then I can't add to it. [editline]31st October 2011[/editline] Also, Ace BG/FG suben
[QUOTE=Darkest_97;33055763]Could someone explain to me what 'Particle.__index = Particle' actually does. As well as 'setmetatable(part,Particle)'. I used the example from the link in the op, but I dont quite understand their explanation.[/QUOTE] All tables/userdata can have a single so-called metatable which lets you hook into actions performed on the table/userdata, akin to "operator overloading" in many class-based OO languages. The metatable can be any table and an object's metatable is set with the [I]setmetatable[/I] function. The hooks are called metamethods. [I]__index[/I] is the metamethod that lets you hook into the index operator. Example of the index operator: [I]t.foo[/I], which is syntax sugar for [I]t["foo"][/I] - which means "look up the string [B]foo[/B] in table (or userdata) [B]t[/B]. Note that there is a separate metamethod, called [I]__newindex[/I], for the index assign operator (i.e. [I]t.foo = bar[/I]). The [I]__index[/I] metamethod must be either a function or a table. When it's a function, it will be called every time the table is indexed, passing two arguments; the table currently being indexed and the key to index, in that order. The result of the callback becomes the result of the lookup. When [I]__index[/I] is another table, such as in your case, the following happens on indexing: first, the table is indexed just like under normal circumstances, and if the result is non-nil, it works exactly like a regular table lookup. If the regular lookup yielded nil, then the secondary table (the [I]__index[/I] member of the table's metatable) is looked up instead. The effect can be chained. Here's some illustrative code: [lua] local meta = {foo = 0, fallback = 0} meta.__index = meta -- all tables which metatable is "meta" will fall back to "meta" when indexed local instance = {foo = 1} setmetatable(instance, meta) -- the metatable of "instance" is now "meta" print(instance.foo) -- prints 1, "foo" is found in "instance", so the fallback is never checked print(instance.fallback) -- prints 0, "instance" does not contain the key "fallback", but __index does print(instance.baz) -- prints nil, "baz" was not found in either table print(meta.foo) -- prints 0, the keys in "meta" are not overwritten, just potentially overriden [/lua] In your case, the [I]__index[/I] metamethod is used to expose the [I]draw[/I] function to all particles. Remember that [I]part:draw()[/I] is just syntax sugar for [I]part.draw(part)[/I] - which is syntax sugar for [I]part["draw"](part)[/I], which can now be clearly identified as a table lookup. Individual particles can override the base draw function simply by setting the [B]draw[/B] field of the particle. You might ask, why not just set the [B]draw[/B] field for every particle? In this case, it makes no difference, but it's handy when more methods are added to Particle. The [I]__index[/I] table doesn't have to be the same as the metatable itself, that just happens to be convenient in many cases. It just means the same table serves the double purpose of metatable for all particles as well as the [I]__index[/I] metamethod for all particles. (Yes, that means [I]print(someParticle.__index)[/I] will print the metatable for someParticle!)
Suddenly I hit a brick wall in trying to get the same sound source playing on top of itself, and copying the source into a new variable is only copying a reference After doing all that stuff, to be held up with love's sound system is frustrating! :v: I'll take a rest and have a think.
I've hit a problem, and could do with a bit of help. Basically I'm trying to detect a dead firework to remove it from the table, I've got this working for a single firework, however with multiple fireworks an error occurs. This is my code: [code] if fireworks[i].dead == true then for i = 1, 250, i+1 do table.remove(fireworks, ???) table.insert(particles, Particle.create(0, 0)) end end [/code] I have no idea what to put in the "???" part. "#fireworks" was what I used to make a single firework disappear. I'm figuring to make this work with more than 1 firework I need to find the position of the value in the table that fireworks[i] is at, how do I do this? It probably shouldn't be in the for loop either, just noticing.
table.remove takes a table and the position in the table the entry to remove is, which as I'm sure you guessed, isn't necessarily the same as the key assigned to the object you want to remove. Instead try: [lua]fireworks[i] = nil[/lua] Lua's garbage collector will do the rest.
That also works removing a single firework at a time, but not multiple. Error: [img]http://puu.sh/7Tur[/img] and the 2nd line here is line 30 [code] for i = 1, #fireworks do fireworks[i]:draw() end [/code] When wrapping "if fireworks[i] ~= nil" around that same error at a different place. Damn :v:
[QUOTE=PieClock;33057591]That also works removing a single firework at a time, but not multiple. Error: [img]http://puu.sh/7Tur[/img] and the 2nd line here is line 30 [code] for i = 1, #fireworks do fireworks[i]:draw() end [/code] When wrapping "if fireworks[i] ~= nil" around that same error at a different place. Damn :v:[/QUOTE] Sorry, I didn't read your earlier post properly :v: That problem is when it's trying to reference the table with a key that doesn't exist in it. This is why most table manipulations are done in the 'for key, value in [pairs/ipairs](table) do' loop - all returned keys would be valid. for clearing out a bunch of them at once, you will either need to loop and find each one that's ready to be removed, or if you're clearing the lot, you could just recreate the table, overwriting itself. You have a "dead" variable on each instance though, so you should only need: [lua]for k, v in pairs(fireworks) do --using pairs as opposed to ipairs as ipairs iterates "up to the first integer key absent from the table." if v.dead then fireworks[k] = nil end table.insert(particles, Particle.create(0, 0)) end[/lua]
[QUOTE=subenji99;33058147]Sorry, I didn't read your earlier post properly :v: That problem is when it's trying to reference the table with a key that doesn't exist in it. This is why most table manipulations are done in the 'for key, value in [pairs/ipairs](table) do' loop - all returned keys would be valid. for clearing out a bunch of them at once, you will either need to loop and find each one that's ready to be removed, or if you're clearing the lot, you could just recreate the table, overwriting itself. You have a "dead" variable on each instance though, so you should only need: [lua]for k, v in pairs(fireworks) do --using pairs as opposed to ipairs as ipairs iterates "up to the first integer key absent from the table." if v.dead then fireworks[k] = nil end table.insert(particles, Particle.create(0, 0)) end[/lua][/QUOTE] This done the trick, thanks. :smile: Now I'm having the same problem removing particles, except this doesn't work. aughh
It chooses between 10 and 50 'particles' to create for each one. Sometimes theres a weird pattern and they dont make a nice circle like at the beginning. I feel like it has something to do with sin/cos. Im assuming Lua does trig functions in radians? [video=youtube;sicxQJy3qXc]http://www.youtube.com/watch?v=sicxQJy3qXc[/video] I also don't remove particles, I just reuse them.
[QUOTE=subenji99;33056887]Suddenly I hit a brick wall in trying to get the same sound source playing on top of itself, and copying the source into a new variable is only copying a reference After doing all that stuff, to be held up with love's sound system is frustrating! :v: I'll take a rest and have a think.[/QUOTE] Found the answer! I needed to be using SoundData objects :v: [editline]e[/editline] Finally got a video sorted - not happy with the quality really, no good at that sort of stuff [media]http://www.youtube.com/watch?v=tBB-9kWXEtY[/media] (Still have some ideas for improvements yet)
My particle engine is messed up, when i fix that i'll be done with cheesy effects [editline]31st October 2011[/editline] Fixed.. now to add different types of fireworks
[video=youtube;L8VDGMG91Ts]http://www.youtube.com/watch?v=L8VDGMG91Ts[/video] I love it when bugs create something awesome.
Sorry, you need to Log In to post a reply to this thread.