Log In  
Follow
AmazingChest
[ :: Read More :: ]

Cart #46853 | 2017-11-30 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA
6

This is my second experiment with PICO-8, this time making a flexible and customizable particle emitter system. In this demo I've pre-made several different emitters that you can view by pressing (left) or (right).

Particles are only rendered as lines, but there are a lot of options to control how they look and behave. It took a while to figure out how to handle colliding with the edges of the screen such that it was accurate no matter how fast the particles were traveling or how long their trails were. Here's just the particle update function to see what's going on:

function particle:update()
  if self.age < self.lifespan then
    if self.jitter > 0 then
      local curr_jitter = rnd(self.jitter)-self.jitter/2
      local curr_angle = atan2(self.dx,self.dy)
      local curr_mag = sqrt(self.dx^2+self.dy^2)
      self.dx = cos(curr_angle+curr_jitter)*curr_mag
      self.dy = sin(curr_angle+curr_jitter)*curr_mag
    end
    self.dx = (self.dx+self.xgrav)*self.drag
    self.dy = (self.dy+self.ygrav)*self.drag
    local next_x = self.x+self.dx
    local next_y = self.y+self.dy

    local line_pts = {{self.x, self.y}}
    line_pts.segments = 0

    while self.collide do
      -- left collision
      if next_x < 0 and btwn(self.y+((next_y-self.y)*self.x/(self.x-next_x)),0,screen) then
        self.y  += (next_y-self.y)*self.x/(self.x-next_x)
        self.x  = 0
        self.dx *= -self.bounce
        next_x  *= -self.bounce

      -- right collision
      elseif next_x > screen and btwn(self.y+((next_y-self.y)*(screen-self.x)/(next_x-self.x)),0,screen) then
        self.y  += (next_y-self.y)*(screen-self.x)/(next_x-self.x)
        self.x  = screen
        self.dx *= -self.bounce
        next_x  = screen-(next_x-screen)*self.bounce

      -- top collision
      elseif next_y < 0 and btwn(self.x+((next_x-self.x)*self.y/(self.y-next_y)),0,screen) then
        self.x  += (next_x-self.x)*self.y/(self.y-next_y)
        self.y  = 0
        self.dy *= -self.bounce
        next_y  *= -self.bounce

      -- bottom collision
      elseif next_y > screen and btwn(self.x+((next_x-self.x)*(screen-self.y)/(next_y-self.y)),0,screen) then
        self.x  += (next_x-self.x)*(screen-self.y)/(next_y-self.y)
        self.y  = screen
        self.dy *= -self.bounce
        next_y  = screen-(next_y-screen)*self.bounce

      else -- no collision
        break
      end
      add(line_pts, {self.x, self.y})
      line_pts.segments += 1
    end

    self.x = next_x
    self.y = next_y
    add(line_pts, {self.x, self.y})
    line_pts.segments += 1
    add(self.lines, line_pts)

  elseif self.maxlength >= 1 then
    self.maxlength -= 1
  end
  -- trim the lines
  while #self.lines > self.maxlength do
    del(self.lines, self.lines[1])
  end
  -- get older
  self.age += 1
end

With this code a particle can bounce multiple times in a single frame.

Anyway, if this is useful at all feel free to steal it, or just check out the demo.

edit: You can now press (x) to toggle performance info.
edit2: Improved readability of performance info and fixed bug where the particle:draw() function was being called on "dead" particles.

P#46841 2017-11-29 17:52 ( Edited 2017-11-30 07:19)

[ :: Read More :: ]

Let's say I have a table called 'objects' that contains all of the objects in my scene. I want to loop through it and call each object's update function. The order in which the objects are updated doesn't matter. Of the following examples:

-- example 1
for obj in all(objects) do
  obj:update()
end

-- example 2
for _,obj in pairs(objects)
  obj:update()
end

-- example 3
for i in pairs(objects)
  objects[i]:update()
end

-- example 4
foreach(objects, function(obj)
  obj:update()
end)

which is the fastest?

Thanks.

P#46558 2017-11-21 16:37 ( Edited 2017-11-26 18:32)

[ :: Read More :: ]

Cart #46571 | 2017-11-22 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA
3

My first cart with PICO-8, Dot Party is a very simplistic implementation of a particle system using OOP techniques. I'm an hobbyist programmer and I've never used Lua before so this was mainly a learning experience figuring out how to make and use "classes" in PICO-8.

I'm not sure if my implementation is the best way (or even good), but here's the breakdown:

dot = {
  x      = 64,
  y      = 64,
  size   = 15,
  shrink = 0.2
}

function dot:new(o)
  local o = o or {}
  setmetatable(o, self)
  self.__index = self
  return o
end

function dot:update()
  self.x -= (self.x-64)/128
  self.y -= (self.y-64)/128
  self.size -= self.shrink
end

function dot:draw()
  circfill(self.x,
           self.y,
           self.size,
           self.size)
end

function dot:exists()
  return self.size > 0
end

This is the "dot" class and its members. It's got a constructor and various functions to update, draw, and return its status.

function spawn_dot(t)
  add(t, dot:new {
    x      = rnd(128),
    y      = rnd(128),
    size   = 1+rnd(15),
    shrink = 0.2+rnd(0.3)
  })
end

function update_all(t)
  local d = {}
  for _,o in pairs(t) do
    if o:exists() then
      o:update()
    else
      add(d, o)
    end
  end
  for _,o in pairs(d) do
    del(t, o)
  end
end

function draw_all(t)
  for _,o in pairs(t) do
    o:draw()
  end
end

Here we have a function that spawns a new dot and functions to update and draw tables of instanced objects. They assume these objects contain certain functions that they call for each.

function _init()
  dots = {}
end

function _update()
  update_all(dots)
  spawn_dot(dots)
end

function _draw()
  cls()
  draw_all(dots)
end

And here's the game loop, which looks nice and neat thanks to our functions.

This is certainly more abstracted than it needs to be, but again I'm just trying to learn. If there's something that I could be doing better I'd definitely like to know. I'm really enjoying using PICO-8 so far.

edit1: Updated the code to use foreach() where I could instead of for in all() because I've heard it's faster. Saved a couple tokens too at the expense of a few more chars.

edit2: Updated to use the pairs() iterator function because I found it to be the fastest method in my own tests. One thing to keep in mind is that pairs() does not iterate in a particular order, but for this it didn't seem to matter.

edit3: Removed recycling to simplify the code and I don't think it made a performance difference anyway.

edit4: Fixed bug in update_all() where deleting dots inside the loop caused the next dot to get skipped entirely. Also realized that pairs() does iterate in order when dealing with a sequenced table, so that's nice.

P#46540 2017-11-21 03:47 ( Edited 2017-11-22 07:02)