Murga-Projects Forums
animations in murgalua - Printable Version

+- Murga-Projects Forums (http://www.murga-projects.com/forum)
+-- Forum: Project Forums (/forumdisplay.php?fid=1)
+--- Forum: MurgaLua - General (/forumdisplay.php?fid=2)
+--- Thread: animations in murgalua (/showthread.php?tid=28)


animations in murgalua - widged - 05-28-2007 09:16 AM

Having a go at this. Read the Lua 5.0 manual.. nice language! Problem is this is my first lua program. What I tried to do was rewrite a nice little script to move a sprite along a bezier line I wrote with another language.



As I don't know how to make a drag and drop, I use dials to specify the position of the two orange points that makes the extremities of a bezier curve and two green points to define the position of the control handles. This part works nicely.

What I can't figure out how to manage is the animation business. I defined the part of the script that will move the sprite as a coroutine and I resume that coroutine when the user press on the play button. This works when I force an onscreen event to occur (having a message box display a message). This doesn't work anymore when I remove it.

That's the relevant part of script:

Code:
function animate_callback(object)
      starttime = os.time()
      repeat
        curtime = os.time()
        delay = curtime - starttime
        if delay >= 1 then
          coroutine.resume(anim)
          fltk.fl_message(delay) --> doesn't work if I remove this
          window:redraw();
          starttime = curtime
        end
      until coroutine.status(anim) == "dead"
      -- io.output("bezier_points.txt")
      -- io.write(serialize(points))
    end


The full script is available at: animation script
(as the script refers to lib files and pictures with a path relative to the anim.lua script, it needs to be run from within the folder itself)

Is this a Lua issue?


RE: animations in murgalua - widged - 05-28-2007 10:28 AM

Bill (igame3D) provided the solution:

Code:
function animate_callback(object)
      repeat
        Fl:wait(.05);
        coroutine.resume(anim)
        window:redraw();
      until coroutine.status(anim) == "dead"
end


much simpler... now I only need to understand how to get an animation to run each time I press on that play button.


RE: animations in murgalua - widged - 05-28-2007 10:36 AM

Okay, I don't need the coroutine business anymore. I only need a call

Code:
    run_anim(sprite,points)


and a function

Code:
function run_animate(sprite,points)
    local pos = {}
    for p = 1,100 do
      pos = points[p]
      Fl:wait(.05);
      sprite:position(pos.x,pos.y)
      window:redraw();
    end
end


New version of the script:
animation script
(run anim.lua from within the folder that contains that file)

And for the fun of it, the script in action, captured in a screencast
(quicktime movie recorded on a mac, may not show properly on a pc or linux machine -- the animation is a *lot* smoother than on this movie)


RE: animations in murgalua - iGame3D - 05-28-2007 12:31 PM

This is awesome, I'm glad you found it something you could roll with.
Can't wait to see what happens next...


RE: animations in murgalua - chrimo - 05-29-2007 08:10 AM

Hi,
WOW, cool!
This very interesting example of lua programming answers many of my beginners questions Big Grin

Thanks for more
Bye
Christian


RE: animations in murgalua - mikshaw - 05-29-2007 09:52 PM

This is a groovy test. I'm wondering if I'm seeing it the way it's supposed to be, though. Rather than gradually moving across the window as I expected, the car appears to leap very quickly between a pair of points. It seems to be animated in the sense that I can see the car very briefly at a middle point, but the movement is so quick I'm not sure if it isn't just an optical illusion or if the car is being redrawn as quickly as possible (no wait) at each point along the curve.

Using murgaLua 0.4.1 on Slackware and Damn Small Linux


RE: animations in murgalua - iGame3D - 05-30-2007 12:08 AM

At the bottom of lib/bezier.lua

Code:
function run_anim(srpite,points)
    local pos = {}
    for p = 1,100 do -- should loop for the number of entries in points
      pos = points[p]
    
      Fl:wait(.004);
      sprite:position(pos.x,pos.y)
      window:redraw();
    end
    
      
end


Should be some kind of speed adjuster around Fl:wait(.004);

On my iMac this is very smooth and Marielle has the same iMac specs mostly.

I replaced with Fl:check();
and it remained smooth so Fl;wait isn't even necessary in there

This works smooth here.

Code:
function run_anim(srpite,points)
    local pos = {}
    for p = 1,100 do -- should loop for the number of entries in points, set to 10 for testing purposes
      pos = points[p]
      sprite:position(pos.x,pos.y)
            Fl:check();
   window:redraw();
    end  
end


THIS LOOPS BACK AND FORTH

Code:
function run_anim(sprite,points)
    local pos = {}
    local cnt=1
    local loopMod=1

--- LoopBackAndForth
    while cnt < 101 do  
      pos = points[cnt]
      sprite:position(pos.x,pos.y)
            Fl:check();
           window:redraw();

--- rebound looping
if cnt==100 then mSpeed =-1
  elseif cnt==1 then mSpeed =1
  end

--- move the counter
cnt=cnt+mSpeed
    end
end




RE: animations in murgalua - mikshaw - 06-23-2007 05:57 PM

Attached is an animated image sequence demo.
It uses a repeat button as a cheat for an actual loop, which I guess should not be needed with murgaLua 0.5
From John's recent drawing tests it seems like it could be done in 0.4.1, but I don't quite understand how =o)


RE: animations in murgalua - iGame3D - 06-25-2007 10:36 AM

Ok here's my version. Auto Plays on start.
Space bar starts and stops the animation and you can control the animation speed in the script.


RE: animations in murgalua - mikshaw - 06-25-2007 11:31 AM

That's pretty cool. I definitely have to look into that autoplay.
Unfortunately it will need some tweaks to work on my system.
The animation doesn't "animate", in that there is usually a set number of frames per second in an animation but this one tore through the frames so violently that it skipped most of them, my cpu went crazy, and my desktop froze =o)

I wonder if this is a similar issue to the one I had with the cars


RE: animations in murgalua - JohnMurga - 06-27-2007 10:01 AM

It is all related to this ...

http://www.murga-projects.com/forum/showthread.php?tid=38&pid=228#pid228

I guess the busy loops don't have as much of an impact on MacOS, so iGame3D is OK ...

But they are a killer on Win32 or Linux unless you have some kinda delay to let the system catch up ... Currently the only way to do that to less than a second is to use something like this :

http://www.murga-projects.com/forum/showthread.php?tid=59&pid=227#pid227

0.5 does have a nicer sleep, and might have timeout callbacks too.

I'll publish my release plans for that tomorrow.

Cheers
JohnM