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,20:03 QUOTE

Quote
since you're taking the squares of the distance, you don't need to take the absolute values of each component
I didn't know that. Thanks.
EDIT: seems like it is needed after a quick test

Quote
Also, it seems that there is always a gravity point at the top left corner (0,0)?
I'd seen that in other tests as well. Since I don't know real math, I just ignored that. I think I've got that fixed, though, as well as the problem with all objects always moving at 45 degree angles.

I messed around with the numbers for a while and came up with something else I like. So far I see one problem with it, that the objects gravitate toward a single x,y intersection over time if you don't move the mouse.
Code Sample
balls=1500
bsize=2
min_grav=0
max_grav=6

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) -- random color
 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 xspeed=(min_grav+max_grav)/distance*xdistance
   local yspeed=(min_grav+max_grav)/distance*ydistance
   if distance <= 25 then -- warp it offscreen
     ball[i]:color(c)
     xspeed=xspeed*max_x/distance*math.random(2,3)
     yspeed=yspeed*max_y/distance*math.random(2,3)
   end      
   if my_x > ball[i]:x() then bx=ball[i]:x()+xspeed else bx=ball[i]:x()-xspeed end
   if my_y > ball[i]:y() then by=ball[i]:y()+yspeed else by=ball[i]:y()-yspeed end
   ball[i]:position(bx,by)
 end
w:redraw()
grav_timer:doWait(.05)
end

w=fltk:Fl_Double_Window(Fl:w(),Fl:h(),"gravity test")
w:color(0)

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)
ball[i]:box(fltk.FL_FLAT_BOX)
end

grav_timer = murgaLua.createFltkTimer()
grav_timer:callback(grav_loop)
grav_timer:do_callback()

w:fullscreen()
w:show()
Fl:run()

EDIT: fixed a typo


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





Group: Members
Posts: 2163
Joined: Feb. 2007
Posted: Jan. 23 2008,20:20 QUOTE

Quote
I messed around with the numbers for a while and came up with something else I like. So far I see one problem with it, that the objects gravitate toward a single x,y intersection over time if you don't move the mouse.
If you could decide that point, it could be desired behavior. I suggest setting it to the center of the display.


--------------
There's no such thing as life. Those mean little jocks invented it ;)
-
Windows is not a virus. A virus does something!
Back to top
Profile PM 
mikshaw Offline





Group: Members
Posts: 4856
Joined: July 2004
Posted: Jan. 23 2008,20:25 QUOTE

That wouldn't be very useful, though, If this turns into something I might want to use as a screensaver.  When I say an intersection, I mean all objects gravitate to travelling along either a single x coordinate or a single y coordinate, so what you have on screen is essentially just two lines that intersect at the location of the cursor.

--------------
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,20:37 QUOTE

Quote (mikshaw @ Jan. 23 2008,15:03)
Quote
since you're taking the squares of the distance, you don't need to take the absolute values of each component
I didn't know that. Thanks.
EDIT: seems like it is needed after a quick test

That was just for your first one where you only used the direct distance.

Your 2nd one is looking better too - but seeing the same problems that you have described.

Took at look at your 2nd one... not sure if you already have something like this:
Code Sample
-- snip
  local yspeed=(min_grav+max_grav)/distance*ydistance
-- cut out if statement
  if my_x > ball[i]:x() then bx=ball[i]:x()+xspeed else bx=ball[i]:x()-xspeed end
  if my_y > ball[i]:y() then by=ball[i]:y()+yspeed else by=ball[i]:y()-yspeed end
-- add back warp
  if distance <= 25 then -- warp it
    ball[i]:color(b)
    bx = math.random(1,max_x)
    by = math.random(1,max_y)
  end
-- /snip
This doesn't make the new dots offscreen though... did you want to specifically do that?
Back to top
Profile PM 
mikshaw Offline





Group: Members
Posts: 4856
Joined: July 2004
Posted: Jan. 23 2008,20:59 QUOTE

Quote
This doesn't make the new dots offscreen though... did you want to specifically do that?
Yeah, It pretty much just shoots them forward a ways, but I don't really understand how much. It looks like somewher between halfway across the screen to waaay outside the screen.  I'm working on a different method now that should place them just offscreen and also hopefully cut down on the processing a little.


--------------
http://www.tldp.org/LDP/intro-linux/html/index.html
Back to top
Profile PM WEB 
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