I don't know how to describe this issue concisely, so that would explain the crappy title. Sorry.

Also, this is a cross-post from the murgaLua forums. I know there are some brilliant minds here so I'm trying to increase my odds of finding a solution.

Quote |

I've been messing around with a "gravity" test today, and although I came up with something cool to look at, it's not what I was trying for. I can get the x,y position of the mouse, the x,y position of the object I want to move, and from there get the distance between the two points: xdistance=math.abs(Fl:event_x()-object:x()) ydistance=math.abs(Fl:event_y()-object:y()) distance=math.sqrt(xdistance*xdistance+ydistance*ydistance) What I would really like to do now is use that distance as a way to increase the motion of the object toward the mouse as the distance gets shorter. Essentially I'm looking for a single (floating point?) value that gradually increases as distance decreases. The problems I'm having are as follows: 1) I suck at math 2) The target speed should get larger as the distance gets smaller 3) The target speed should always be a fairly small non-negative number Is there anyone who might be able to help? Thanks. |

I got a quick answer from Juergen on the murgaLua forum that seems to fit nicely:

speed=<minimum speed>+<maximum speed>/distance

This is really cool!

Please post it. We want to see it too.

I posted beta1 of the murgaLua widgets collection last night, over at Murga's place, and the 3 stages of gravity tests up to this point have been included.

I still want to improve it over time, maybe turn it into some sort of interactive screensaver thing, and have to work out some kinks.

Code Sample |

#!/bin/murgaLua balls=250 bsize=10 function grav_loop() local my_x,bx=Fl:event_x(),0 local my_y,by=Fl:event_y(),0 local c=math.random(1,255) local b=math.random(1,balls) ball[b]:color(c) for i=1,balls do local xdistance=math.abs(my_x-ball[i]:x()) local ydistance=math.abs(my_y-ball[i]:y()) local distance=math.sqrt(xdistance*xdistance+ydistance*ydistance) local speed=max_slider:value()/distance if distance > 0 then if my_x > ball[i]:x() then bx=ball[i]:x()+speed else bx=ball[i]:x()-speed end if my_y > ball[i]:y() then by=ball[i]:y()+speed else by=ball[i]:y()-speed end ball[i]:position(bx,by) end end w:redraw() grav_timer:doWait(.05) end w=fltk:Fl_Double_Window(640,480,"gravity test") w:color(0) fltk.fl_define_FL_OVAL_BOX() math.randomseed(os.time()) max_x=w:w()-bsize max_y=w:h()-bsize ball={} for i=1,balls do ball[i]=fltk:Fl_Box(math.random(1,max_x),math.random(1,max_y),bsize,bsize) if fltk._FL_OFLAT_BOX then ball[i]:box(fltk._FL_OFLAT_BOX) else ball[i]:box(fltk.FL_OFLAT_BOX) end end max_slider=fltk:Fl_Hor_Value_Slider(0,w:h()-20,w:w(),20) max_slider:minimum(50) max_slider:maximum(5000) max_slider:value(500) max_slider:step(10) grav_timer = murgaLua.createFltkTimer() grav_timer:callback(grav_loop) grav_timer:do_callback() w:show() Fl:run() |

Edit: I replaced the plastic with a flat oval box after seeing how poorly it runs in fullscreen

Interesting use of murgalua... looking good. Took a quick look.

misc. math note:

since you're taking the squares of the distance, you don't need to take the absolute values of each component (unless you prefer to keep it consistent with the variable name)

Also, it seems that there is always a gravity point at the top left corner (0,0)?

Next Page...

original here.