Go Back   Science Forums
View Single Post
Old 06-06-2007   #1 (permalink)
CraigD's Avatar
CraigD
Creating


Location:
Silver Spring, MD, USA
 
CraigD has a reputation beyond reputeCraigD has a reputation beyond reputeCraigD has a reputation beyond reputeCraigD has a reputation beyond reputeCraigD has a reputation beyond reputeCraigD has a reputation beyond reputeCraigD has a reputation beyond reputeCraigD has a reputation beyond reputeCraigD has a reputation beyond reputeCraigD has a reputation beyond reputeCraigD has a reputation beyond repute
 



Not Ranked  0 score     
Cool Newtonian Bowling

A while back, with a mind toward exploring some speculation about time-traveling pool balls made in this post , I wrote a small program to simulate elastic collisions in 2 spatial. The bodies in it are “typed” that is, different program logic can apply to collisions between them, base on what they “are”. My intention was to have a “time travel hole” body that would displace “balls” that collided with it in time.

I’ve not yet gotten around to time travel-type bodies, but have 2 body types: “balls” (or, since they don’t have true angular momentum and they’re only in 2 dimensions, more like “pucks”) and “gutters” – linear objects that balls disappear when they contact.

This is enough to set up a “bowling alley” suitable for a competition.

Here’s the setup: the “alley” is a 9 by 9 square (corners at (1,1) and (9,9)) surrounded by gutters. 10 circular “pins”, each with diameter .5 and mass 1, are set at (5,5), (4,6), (6,6), (3,7), (5,7), (7,7), (2,8), (4,8), (6,8), (8,8). A ball with diameter 1 and mass 3 may be placed at any location (x,2), and given any velocity (h,v). It looks, therefore, something like this:
Code:
  1 2 3 4 5 6 7 8 9
1 . . . . . . . . .
2 . B             .
3 .               .
4 .               .
5 .       1       .
6 .     2   3     .
7 .   4   5   6   .
8 . 7   8   9   0 .
9 . . . . . . . . .
The object of the game is to remove all of the pins from play by causing them to collide with a gutter – since the surface is frictionless, all this requires in any but the most unusual situation is for each pin to be struck by the ball or another pin.

To play, post a value for x (between 1.5 and 8.5), h, and v. I’ll reply with the results.

For example, for x=1.5, (h,v)=(.87,1):
Left the 4 and 10 pins - not a win.
Code:
   c                               d

     f





                   1

               2       3

           4       5       6

       7       8       9       a

   e
Sim T:0

   c                               d







               f   1

              2        3

           4       5       6

       7       8       9       a

   e
Sim T:3.257532120793722407

   c                               d






                   1
                f

              2        3

           4       5       6

       7       8       9       a

   e
Sim T:3.518989094821347538

   c                               d






                      1


                  f    3

           4       5       6
              2
       7       8       9       a

   e
Sim T:4.662119398181308323

   c                               d





                       1



                   f   3

           4       5       6
             2
       7       8       9       a

   e
Sim T:5.08496613968448101

   c                               d





                        1



                    f  3

           4      5        6
            2
       7               9       a
                8
   e
Sim T:5.473444378169957758

   c                               d





                         1


                       3
                    f

           4      5        6
           2
       7               9       a

   e
Sim T:5.874075482978240739

   c                               d




                            1



                         3

                      f
           4               6

      7 2         5    9       a

   e
Sim T:7.00023810809474893

   c                               d



                               1



                           3



           4           f   6
     7
                       9       a
       2         5
   e
Sim T:8.035746865091581241

   c                               d



                                1



                           3



           4           f   6
     7
                      9        a
       2
   e
Sim T:8.322891012478744093

   c                               d



                                 1



                            3



           4            f  6
    7
                      9        a
       2
   e
Sim T:8.677763957767684519

   c                               d



                                 1



                            3


                           6
           4            f
    7
                      9        a

   e
Sim T:8.7871823262624635

   c                               d







                             3


                           6
           4            f
   7
                               a
                      9
   e
Sim T:9.35499389981379355

   c                               d







                              3


                            6
           4
                         f
                               a
                      9
   e
Sim T:9.73962210175333648

   c                               d






                                3



                             6
           4
                         f
                               a

   e
Sim T:10.65367876786687539

   c                               d









                              6

           4

                           f   a

   e
Sim T:12.32592343235649738

   c                               d











           4

                               a
                             f
   e
Sim T:16.15223318849125928

   c                               d











           4

                               a

   e
Sim T:16.26854475445460678
Here’re some technical details:
  • The simulation follows the classical laws of conservation of momentum and energy. In other words, it’s a purely Newtonian simulation of completely elastic collisions. (except for the “gutters”, which simply remove bodies, including their momentum and energy, from the system.
  • Although the simulation is “event based”, so as accurate as its arithmetic precision permits, it uses a built-in calculator with about 18 decimal digits precision, so has limited precision.
  • In the event that more than 2 bodies collide simultaneously, the simulation randomly chooses one body and alters its position slightly (eg: typically about 1e-16) so that only one collision occurs at a time. The way the pins are set up, you’ve got to try pretty hard to get this to happen.
Here’s the simulation’s MUMPS code:
Code:
n (XELCOL4,X,Y,R,M,VX,VY,T,CB,CT,B,C) x XELCOL4(-2),XELCOL4(-1) ;XELCOL4: elastic collison simulator w/multiple object types
x XELCOL4(0,0) w "Enter:(1)[Mass] (2){Radius|W,H} (3)X,Y (4)[VX,VY] (5)[type] (6)[time]",! f  R R,! q:R=""  x XELCOL4(0,1) ;XELCOL4(-2): read (default to circle)
s WTI=.25,WT=-WTI f  s WT=WT+WTI x XELCOL4(-1,1),XELCOL4(-1,2),XELCOL4(-1,3),XELCOL4(-1,5) s R=1 x XELCOL4(-1,6) q:R?1(1"Q",1"q").e  i R?1(1"D",1"d").e x XELCOL4(-1,4),XELCOL4(-1,5) s R=0 x XELCOL4(-1,6) q:R?1(1"Q",1"q").e ;XELCOL4(-1): interactive display
f  q:WT'>$o(C(""))!($d(C)<9)  X XELCOL4(1) ;XELCOL4(-1,1)
K W s B="" F  s B=$o(M(B)) q:B=""  S X=WT-T(B)*VX(B)+X(B),Y=WT-T(B)*VY(B)+Y(B),$E(W(Y/.5+.5\1),X/.25+.5\1)=$E("123456789abcdef",B) ;XELCOL4(-1,2)
w ! f W=$o(W("")):1:$o(W(""),-1) W $e($g(W(W)),1,79),! ;XELCOL4(-1,3)
w !,"(1)[Mass] (2){Radius|W,H} (3)X,Y (4)[VX,VY] (5)[type] (6)[time]",! s B="" f  s B=$o(M(B))  q:B=""  w M(B)," ",R(B)," ",WT-T(B)*VX(B)+X(B),",",WT-T(B)*VY(B)+Y(B)," ",VX(B),$s(VX(B)!VY(B):",",1:""),VY(B)," ",B(B),! ;XELCOL4(-1,4)
W "Sim T:",WT," Sim step T:",WTI," Event:" w:$o(C(""))]"" C($o(C(""))),",",CB(C($o(C("")))) w " Event T:",$o(C("")),! ;XELCOL4(-1,5)
w:R "[D]etails, " w "[Q]uit, Sim step," w:$d(C)>9 " [N] for next event," r " or enter to continue:",R s:R>0 WTI=R s:$tr(R,"n","N")="N"&($d(C)>9) WT="."_$TR($J("",17-$L($P(WT,".")))," ",0)_1+$o(C(""))-WTI ;XELCOL4(-1,6)
n (XELCOL4,X,Y,R,M,VX,VY,T,CB,CT,B,C) k C,CB,CT s T=0,B1="" f  s (B1,B2)=$o(M(B1)) q:B1=""  x XELCOL4(4) ;XELCOL4(0): initialize
k C,CB,CT,T,R,M,X,Y,VX,VY,B s T=0 ;XELCOL4(0,0): clear bodies
n (XELCOL4,X,Y,R,M,VX,VY,T,CB,CT,B,C) s (B,B1)=$o(M(""),-1)+1,T(B)=+$p(R," ",6),M(B)=$p(R," "),R(B)=$p(R," ",2),X(B)=$p($p(R," ",3),","),Y(B)=$p($p(R," ",3),",",2),VX(B)=$p($p(R," ",4),","),VY(B)=$p($p(R," ",4),",",2),B(B)=$p(R," ",5) s:B(B)="" B(B)=1 x XELCOL4(4) ;XELCOL4(0,1): returns B add body given R:(1)[Mass] (2){Radius|W,H} (3)X,Y (4)[VX,VY] (5)[type] (6)[time]
k:$d(CT(R)) C(CT(R)),CT(R),CT(CB(R)),CB(CB(R)),CB(R) k M(R),R(R),X(R),Y(R),VX(R),VY(R),T(R) ;XELCOL4(0,2): remove body R
n (XELCOL4,X,Y,R,M,VX,VY,T,CB,CT,B,C,CR) i $d(C)>9  s T=$o(C("")) k IVX,IVY x XELCOL4(2),XELCOL4(3,5,B(B1),B(B2)) k C(T),CB(B1),CB(B2),CT(B1),CT(B2) m VX=IVX,VY=IVY s B2P=B2 s B1=B1 x XELCOL4(4),XELCOL4(5) s B1=B2P x XELCOL4(4),XELCOL4(5) ;XELCOL4(1): next event
s B1=C(T),TD=T-T(B1),X(B1)=VX(B1)*TD+X(B1),Y(B1)=VY(B1)*TD+Y(B1),T(B1)=T,B2=CB(B1),TD=T-T(B2),X(B2)=VX(B2)*TD+X(B2),Y(B2)=VY(B2)*TD+Y(B2),T(B2)=T ;XELCOL4(2): move affected body(s) to time T positions
s CX1=VY2*RYX+VX2*M(B2)*RM-(VY1*RYX+VX1*M(B1))*2/(M(B2)*RM*RM+M(B1)*(RYX*RYX+1)),CY1=CX1*RYX,CX2=-CX1*RM,CY2=CX2*RYX ;XELCOL4(3,0,1): elastic collision calculation
i YD s RYX=XD/YD,VX1=VY(B1),VY1=VX(B1),VX2=VY(B2),VY2=VX(B2) x XELCOL4(3,0,1) s IVX(B1)=$g(IVX(B1),VX(B1))+CY1,IVY(B1)=$g(IVY(B1),VY(B1))+CX1,IVX(B2)=$g(IVX(B2),VX(B2))+CY2,IVY(B2)=$g(IVY(B2),VY(B2))+CX2 ;XELCOL4(3,0,1,0)
i XD s RYX=YD/XD,VX1=VX(B1),VY1=VY(B1),VX2=VX(B2),VY2=VY(B2) x XELCOL4(3,0,1) s IVX(B1)=$g(IVX(B1),VX(B1))+CX1,IVY(B1)=$g(IVY(B1),VY(B1))+CY1,IVX(B2)=$g(IVX(B2),VX(B2))+CX2,IVY(B2)=$g(IVY(B2),VY(B2))+CY2 ;XELCOL4(3,0,1,1)
s RM=M(B1)/M(B2),XD=X(B2)-X(B1),YD=Y(B2)-Y(B1) x XELCOL4(3,0,1,$tr(XD,"-")>$tr(YD,"-")) ;XELCOL4(3,5,1,1): circle-circle col effect
s R=B1 x XELCOL4(0,2) ;XELCOL4(3,5,1,2): gutter-circle col effect
s R=B2 x XELCOL4(0,2) ;XELCOL4(3,5,2,1): circle-gutter col effect
k CR i $d(M(B1)) s B2="" f  s B2=$o(M(B2)) q:B2=""  i B2'=B1 s IT=-1 x XELCOL4(4,5,B(B1),B(B2)) i IT'<0 s CT=IT+T x XELCOL4(4,0,2) ;XELCOL4(4): calculate next collisions
s X1=T-T(B1)*VX(B1)+X(B1),Y1=T-T(B1)*VY(B1)+Y(B1),X2=T-T(B2)*VX(B2)+X(B2),Y2=T-T(B2)*VY(B2)+Y(B2) ;XELCOL4(4,0,1)
s F1=$s($d(CT(B1)):$s(CT(B1)>CT:1,CT(B1)<CT:0,1:""),1:1),F2=$s($d(CT(B2)):$s(CT(B2)>CT:1,CT(B2)<CT:0,1:""),1:1) i $s(F1&F2:1,F1_F2="":$r(2),1:0) s BK=B1,FK=F1 x:$d(CT(BK)) XELCOL4(4,0,2,0) s BK=B2,FK=F2 x:$d(CT(BK)) XELCOL4(4,0,2,0) s C(CT)=B1,(CT(B1),CT(B2))=CT,CB(B1)=B2,CB(B2)=B1 k CR(B1),CR(B2) ;XELCOL4(4,0,2)
s BK2=CB(BK),(CR(BK),CR(BK2))="" k C(CT(BK)),CT(BK2),CT(CB(BK2)),CB(BK2),CB(BK) s:FK="" T(BK2)="."_$TR($J("",17-$L($P(T(BK2),".")))," ",0)_1+T(BK2) ;XELCOL4(4,0,2,0)
x XELCOL4(4,0,1) s XD=X1-X2,YD=Y1-Y2,VXD=VX(B1)-VX(B2),VYD=VY(B1)-VY(B2),A=VXD**2+(VYD**2),B=XD*VXD+(YD*VYD)*2,C=XD**2+(YD**2)-(R(B1)+R(B2)**2),D=B*B-(4*A*C) i D'<0&A s IT=D**.5+B/A/-2 ;XELCOL4(4,5,1,1): circle-circle col detect
s I=B1,B1=B2,B2=I x XELCOL4(4,5,2,1) s I=B1,B1=B2,B2=I ;XELCOL4(4,5,1,2): circle-gutter col detect
s XD=$p(R(B1),","),YD=$p(R(B1),",",2),F=$s(XD&VX(B2):1,YD&VY(B2):2,YD&VX(B2):3,XD&VY(B2):4,1:0) x:F XELCOL4(4,0,1),XELCOL4(4,5,2,1,F) ;XELCOL4(4,5,2,1): gutter-circle col detect
s A=YD/XD,B=VY(B2)/VX(B2),C=A-B i C s IT=A*X1-(B*X2)+Y2-Y1/C-X2/VX(B2) ;XELCOL4(4,5,2,1,1)
s A=XD/YD,B=VX(B2)/VY(B2),C=A-B i C s IT=A*Y1-(B*Y2)+X2-X1/C-Y2/VY(B2) ;XELCOL4(4,5,2,1,2)
s IT=X1-X2/VX(B2) ;XELCOL4(4,5,2,1,3)
s IT=Y1-Y2/VY(B2) ;XELCOL4(4,5,2,1,4)
q  ;XELCOL4(4,5,2,2): gutters can't collide
s B1="" f  s B1=$o(CR(B1)) q:B1=""  x XELCOL4(4) ;XELCOL4(5)


----------------
Moderator: Computers and Technology; Medical Science; Science Projects and Homework; Philosophy of Science; Physics and Mathematics; Environmental Studies

Last edited by CraigD; 06-17-2007 at 10:49 PM.. Reason: Added a display improvement to the MUMPS code
Reply With Quote
 
» Advertisement
» Current Poll
Who's the sexiest man alive? Johnny Depp or Robert Pattinson?
Johnny Depp - 27.27%
3 Votes
Robert Pattinson - 0%
0 Votes
Someone else (please specify) - 45.45%
5 Votes
I'm too macho to think a guy is sexy - 27.27%
3 Votes
Total Votes: 11
You may not vote on this poll.


All times are GMT -8. The time now is 03:24 AM.

Hypography?

Hypography [n.]: A combination of "hyperlink" and "bibliography" - ie, a list of links to electronic documents. Comparable to discography and bibliography, but not cartography.

We have been online since May 2000, and aim to be the best place to find and share science-related content of all kinds.

Share the love!

Please add more science to your life. Use our RSS feeds on your blog, your portal, or your favorite feedreader!


Powered by vBulletin® Version 3.8.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Copyright © 2000-2009 Hypography
Part of the Hypography - Science for Everyone Network