Page 1 of 1

[library] Moving objects on paths (alpha)

Posted: Sat Mar 26, 2016 11:46 pm
by Megadardery
Yep, no need to mess with Xes and Ys. This library allows you to set a path and let your object or character move across it with ease.
Image

Currently this alpha version supports:
  • Setting a continuous path and setting an object to loop it.
  • shapes includes: lines and arcs
  • Doesn't break even if the delta time increases significantly
  • Expand path on the fly.
  • Draw the path
Please test it and say what do you think about it, remember this is the alpha version, literally created in 2 days. So bugs are welcome here :D
path.lua
(6.72 KiB) Downloaded 202 times
Usage Note:
Simply download the library and add it to your project file, add the line:

Code: Select all

require 'path'
to your main.lua file at the very top.
Then proceed to create an object for the path, and use the newPath function to create a new path, setMovement to prepare the movement and follow to follow it. Such as

Code: Select all

require 'path'
function love.load()
  path1=newPath(3,2,6,2,8,4,'arc',7,5,.1,false,'arc',6,5,.5,true,6,7,'arc',3,7,3,true,'arc',3,2,7,false)
  path1:setMovement(5,true)
end

function love.update(dt)
  path1:follow(dt)
end

function love.draw()
  love.graphics.scale(40)
  love.graphics.setLineWidth(0.05)
	love.graphics.circle('fill', path1.x,path1.y,.25)
	for i = 0,20,1 do
    love.graphics.line(0,i,50,i)
    love.graphics.line(i,0,i,50)
  end
 	love.graphics.setColor(255,0,0)
	path1:draw()
	love.graphics.setColor(255,255,255)
	
	--you only need path1:draw(), but the rest helps with the example
	--also adds grid lines.
end
You can use newPath as following: To define a line use two parameters (x,y), if you want to create an arc, you can use ('arc',x,y,width,flipped) which makes the whole syntax something like this:

Code: Select all

newPath(x1,y1,x,y,x,y,'arc',x,y,width,flipped,x,y,x,y)
x1,y1 is the starting position of the path, every other x,y is the end point of either an arc or a line, width is how high the arc should be (the distance between the pinpoint of the arc and the midpoint between the start point of the arc and the end point), flipped is either true or false, decides the direction to set the arc. You can mess around with them, better documentary coming with the beta version.


Upcoming and planned features includes
  • Multiple objects on the same path (top priority)
  • Acceleration and Deceleration support.
  • More shapes, suggest ideas and needs.
  • General optimizations and cleaner code for clarity.

Re: [library] Moving objects on paths (alpha)

Posted: Sun Mar 27, 2016 2:24 am
by Tanner
This is cool but I find the number of arguments passed to the newPath function to be sort of hard to reason about. Have you considered making the path building incremental? So, you create the Path object and then have Path:moveTo, Path:lineTo, Path:arcTo. You could even have helpers for simple complete shapes like rectangles and ellipses.

Re: [library] Moving objects on paths (alpha)

Posted: Sun Mar 27, 2016 10:19 am
by Megadardery
Like I said you can add lines and arcs on the fly. So you can start by path1=newPath(x1,y1) the starting point then use path1:addArc(x,y,wid,flipstate) and path1:addLine(x,y). I think I'm just bad at documentaries.

On a side note, lineTo and arcTo are better names than addLine and addArc. Adding the helpers will also happen eventually. Thanks for helping.

Re: [library] Moving objects on paths (alpha)

Posted: Mon Apr 11, 2016 4:56 am
by AntonioModer
Thanks, Megadardery! :)

Re: [library] Moving objects on paths (alpha)

Posted: Mon Apr 11, 2016 9:30 am
by Sheepolution
Megadardery wrote:Like I said you can add lines and arcs on the fly. So you can start by path1=newPath(x1,y1) the starting point then use path1:addArc(x,y,wid,flipstate) and path1:addLine(x,y). I think I'm just bad at documentaries.
Tanner means that if you would add return self to your add functions like this

Code: Select all

function path_mt:addLine(x,y)
  local index=#self + 1
  self[index]={x=x,y=y}
  self:updateLine(index)
  return self
end
you could go from this

Code: Select all

path1=newPath(3,2)
path1:addLine(10, 4)
path1:addCurve(17,6)
path1:addLine(20, 12)
to this

Code: Select all

path1=newPath(3,2):addLine(10, 4):addCurve(17,6):addLine(20, 12)
Also, I recommend you take a look at Kikito's module guide