Search Members Help

» Welcome Guest
[ Log In :: Register ]

Mini-ITX Boards Sale, Fanless BareBones Mini-ITX, Bootable 1G DSL USBs, 533MHz Fanless PC <-- SALE $200 each!
Get The Official Damn Small Linux Book. DSL Market , Great VPS hosting provided by Tektonic
Pages: (3) </ [1] 2 3 >/

[ Track this topic :: Email this topic :: Print this topic ]

reply to topic new topic new poll
Topic: some math help please?< Next Oldest | Next Newest >
mikshaw Offline





Group: Members
Posts: 4856
Joined: July 2004
Posted: Jan. 23 2008,00:42 QUOTE

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.


--------------
http://www.tldp.org/LDP/intro-linux/html/index.html
Back to top
Profile PM WEB 
mikshaw Offline





Group: Members
Posts: 4856
Joined: July 2004
Posted: Jan. 23 2008,01:53 QUOTE

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!


--------------
http://www.tldp.org/LDP/intro-linux/html/index.html
Back to top
Profile PM WEB 
roberts Offline





Group: Members
Posts: 4983
Joined: Oct. 2003
Posted: Jan. 23 2008,05:48 QUOTE

Please post it. We want to see it too.
Back to top
Profile PM WEB 
mikshaw Offline





Group: Members
Posts: 4856
Joined: July 2004
Posted: Jan. 23 2008,13:32 QUOTE

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


--------------
http://www.tldp.org/LDP/intro-linux/html/index.html
Back to top
Profile PM WEB 
^thehatsrule^ Offline





Group: Members
Posts: 3275
Joined: July 2006
Posted: Jan. 23 2008,16:51 QUOTE

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)?
Back to top
Profile PM 
13 replies since Jan. 23 2008,00:42 < Next Oldest | Next Newest >

[ Track this topic :: Email this topic :: Print this topic ]

Pages: (3) </ [1] 2 3 >/
reply to topic new topic new poll
Quick Reply: some math help please?

Do you wish to enable your signature for this post?
Do you wish to enable emoticons for this post?
Track this topic
View All Emoticons
View iB Code