Log In  

This cart has devkit mode enabled and requires a mouse and keyboard.
It's intended to be run locally; not all feature work correctly in the web player.

A simple path editor for creating animation paths for game objects. The paths created are simple interpolated paths not bezier or similar curves. The path is guaranteed to pass through all of the control points. You don't have as much control over the exact shape of the path but you can still get some pretty nice results.

Cart #animation_path_editor-0 | 2023-12-07 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA
3

Features

  • Save and Load path files in a binary format to .p8 files so they can be easily changed later.
  • Export paths and related functions to a .txt file which can be #included in your project.
  • When exported, paths are translated so the first control point is at (0, 0) so you can easily add any offset allowing you to use the same path in multiple locations without having to change the path itself.
  • When you save a path file the filename is saved in persistent cart data and that file will be automatically loaded the next time you start the editor. (Assumes the editor and path file are in the same directory.)
  • Path files contain multiple paths so you can keep all paths for a single project in a single file.
  • Path names are editable so you can give them meaningful names.
  • Create open or closed paths.
  • A timeline allows you to adjust the animation timing of individual sections of the path as well as the total duration (in seconds) of the animation.
  • Import sprite and map data from another cart. (Assumes cart is in the same directory as the editor.)
  • Select sprites to preview path animation. (Currently a single, static, 8x8 sprite can be selected per path. This is just for preview purposes: selected sprites are saved in path files but are not exported.)
  • Adjust map position either by dragging it or via text input. (As with sprites this is just for preview purposes. Each path can have the map displayed at a different position and those postiions are saved in the path file but not exported.)
  • If you don't like my colour choices, the first tab of the .p8.png contains configuration data which you can edit to better suit your needs/tastes. (You can also suppress the help message which pops up when you run the cart from here if you want to.) I'm not sure I made everything configurable so, if I've missed something you'd like to change the colour of, feel free to let me know and I'll add it in.

Command Summary

(also available in the editor by pressing 'h')

Example

A simple example using a single path to animate two squares at different offsets and with different timing.

Code:

-- exported file ------------------------------------------------------------
do
   local function parse(p_str)
      -- parses the exported path string into a table.

      -- paths are accessed by name with dashes (-) and spaces
      -- converted to underscores. for example: 'path-1' in the editor
      -- becomes 'paths.path_1' when exported.

      -- each path is a three element array. the first element of each
      -- path is the animation duration in seconds. the other two
      -- elements are arrays used by p_lerp to interpolate the
      -- position and timing between points.
      local paths = {}
      local pstrs = split(p_str, '|')
      for p in all(pstrs) do
         local vals = split(p, ':')
         paths[ vals[1] ] = {vals [2], split(vals[3]), split(vals[4])}
      end
      return paths
   end

   local function choose(n, k)
      -- combinatorial choose function for real number 'n' and
      -- positive integer 'k'. this function is used by the
      -- interpolation functions.
      local num, den = 1, 1
      for i=1,k do
         num *= n - (i - 1)
         den *= i
      end
      return k == 0 and 1 or num/den
   end

   function p_lerp(path, t)
      -- calculates the position of 'path' at time 't'
      -- where 0 <= t <= duration in seconds. The duration of a path
      -- is stored at index 1 of the path array.
      local _, coefs, times = unpack(path)
      local ti = 0
      for i=1,#times do
         ti = t >= times[i] and i or ti
      end
      local t1 = times[ti] or -times[ti+2]
      local t2 = times[ti + 1] or 2*times[ti] - times[ti-1]
      local x, y = 0, 0
      for i=1,#coefs / 2 do
         local c = choose(ti - 1 + (t - t1) / (t2 - t1), i)
         x += coefs[2*i-1]*c
         y += coefs[2*i]*c
      end
      return x, y
   end

   function p_lerp_norm(path, t)
      -- a wrapper around 'p_lerp'.
      -- 'p_lerp_norm' expects 't' to be normalized
      -- such that 0 <= t <= 1. This is more in line with how
      -- interpolation functions usually work and is the better choice
      -- if you're planning on applying easing functions to the
      -- animation or if you want to use the same path but vary the
      -- durations.
      return p_lerp(path, t*path[1])
   end

   paths=parse"line:1:46,34:0,1|loop:1:-19,31,51,-13,-61,-32,14,82:0,0.3,0.5,0.8,1|curve:1.8:-6,35,66,-49,-134,91:0,0.6,1.2,1.8"
end
-- end exported file --------------------------------------------------------

-- example ------------------------------------------------------------------
ts1 = time()
ts2 = time()
--p = paths.curve
--p = paths.line
p = paths.loop

function _update()
   t1 = time() - ts1
   if t1 >= p[1] then -- p[1] is the animation duration as set in the editor
      ts1 = time()
   end
   x1,y1 = p_lerp(p, t1)
   x1 += 30
   y1 += 30

   t2 = time() - ts2
   if t2 >= 3 then -- make the animation take 3 seconds instead
      ts2 = time()
   end
   x2,y2 = p_lerp_norm(p, t2/3) -- normalize the time so it's between 0 and 1
   x2 += 60
   y2 += 60
end

function _draw()
   cls(13)
   rectfill(x1, y1, x1+7, y1+7, 7)
   rectfill(x2, y2, x2+7, y2+7, 7)
   print('timer 1:'..t1)
   print('timer 2:'..t2)
end
-- end example --------------------------------------------------------------

P#138411 2023-12-08 13:08


[Please log in to post a comment]