daily pastebin goal
72%
SHARE
TWEET

Untitled

a guest Nov 20th, 2017 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from visual import *
  2.  
  3. import wx
  4.  
  5. #(default window)
  6. scene.width = 800
  7. scene.height = 600
  8. scene.title='Ball throwing with air resistance'
  9.  
  10. #############################################################################  
  11.  
  12.  
  13.  
  14.  
  15.  
  16. w = window(x=800,y=0,width=350, height=350, menus=True,title='default settings')
  17.  
  18. #widget settings
  19.  
  20.  
  21. def setAngle(evt): # called on slider 1 events,
  22.     #someValue = slider1.GetValue()  # value is min-max slider position, 0 to 90
  23.     ball.make_trail=False
  24.     ball.pos=start
  25.     ball.acceleration=vector(0,0,0)
  26.     ball.velocity=vector(0,0,0)
  27.  
  28.  
  29. def setSpeed(evt): # called on slider 2 events
  30.     #someValue = slider2.GetValue() # value is min-max slider position, 0 to 50
  31.     ball.make_trail=False
  32.     ball.pos=start
  33.     ball.acceleration=vector(0,0,0)
  34.     ball.velocity=vector(0,0,0)
  35.  
  36. def setMass(evt): # called on slider 3 events
  37.     global mass
  38.     mass = slider3.GetValue()/1000 # value is min-max slider position, 0.15 to 10
  39.     ball.make_trail=False
  40.     ball.pos=start
  41.     ball.acceleration=vector(0,0,0)
  42.     ball.velocity=vector(0,0,0)
  43.  
  44.  
  45. def setRadius(evt): # called on slider 4 events
  46.     global radius
  47.     radius = slider4.GetValue()/100# value is min-max slider position, 0.08 to 5
  48.     ball.make_trail=False
  49.     ball.pos=start
  50.     ball.acceleration=vector(0,0,0)
  51.     ball.velocity=vector(0,0,0)
  52.  
  53.  
  54.  
  55. def shot(evt):
  56.  
  57.     ball.pos=start
  58.     angle0=slider1.GetValue()
  59.     v0mag=slider2.GetValue()
  60.     vy=v0mag*sin(radians(angle0))
  61.     vx=v0mag*cos(radians(angle0))
  62.     ball.velocity=vector(vx,vy,0)
  63.     ball.acceleration=vector(0,-9.8,0)
  64.     ball.mass=slider3.GetValue()
  65.     ball.radius=slider4.GetValue()
  66.     Fgravitation=ball.mass*vector(0,-9.81,0)
  67.     ball.make_trail=True
  68.  
  69.  
  70.  
  71. #### texts and sliders
  72. p = w.panel
  73.  
  74.  
  75. wx.StaticText(p, pos=(10,10), size=(300,30),
  76.     label='Set with the sliders',
  77.               style=wx.ALIGN_LEFT | wx.ST_NO_AUTORESIZE)
  78. slider1 = wx.Slider(p, pos=(10,70), size=(100,20), minValue=0, maxValue=90)
  79. slider1.Bind(wx.EVT_SCROLL, setAngle)
  80. wx.StaticText(p, pos=(10,50), label='Set angle (0-90) ')
  81.  
  82. slider2 = wx.Slider(p, pos=(10,150), size=(100,20), minValue=0, maxValue=50)
  83. slider2.Bind(wx.EVT_SCROLL, setSpeed)
  84. wx.StaticText(p, pos=(10,120), label='Set speed (0-50) m/s')
  85.  
  86. slider3=wx.Slider(p, pos=(10,200), size=(100,20), minValue=0.15, maxValue=10)
  87. slider3.Bind(wx.EVT_SCROLL, setMass)
  88. wx.StaticText(p,pos=(10,180), label='Set mass kg')
  89.  
  90. slider4=wx.Slider(p, pos=(10,250), size=(100,20), minValue=0.08, maxValue=5)
  91. slider4.Bind(wx.EVT_SCROLL, setRadius)
  92. wx.StaticText(p,pos=(10,220), label='Set radius m')
  93.  
  94.  
  95. shotButton = wx.Button(p, pos=(150,190),label='Launch')
  96. shotButton.Bind(wx.EVT_BUTTON, shot)
  97.  
  98.  
  99.  
  100. #####################################################################
  101.  
  102.  
  103. # throwing movement from start.
  104. # sliders change the throwing angle, speed, mass and radius
  105. # start after pressing button
  106.  
  107. start=vector(-30,-20,0)
  108. curve(pos=[start,start+vector(50,0,0)])
  109. points(pos=start, size=3, color=color.red)
  110.  
  111. ball=sphere(pos=start,radius=0.1,color=color.magenta,make_trail=True)
  112.  
  113. # start values
  114. slider1.SetValue(45)
  115. angle0=slider1.GetValue()
  116. slider2.SetValue(25)
  117. v0mag=slider2.GetValue()
  118. slider3.SetValue(0.15)
  119. ball.mass=slider3.GetValue()
  120. slider4.SetValue(0.08)
  121. ball.radius=slider4.GetValue()
  122.  
  123.  
  124. # velocity acceleration and gravitation
  125. vy=v0mag*sin(radians(angle0))
  126. vx=v0mag*cos(radians(angle0))
  127. ball.velocity=vector(0,0,0)# set to 0 before launch
  128. ball.acceleration=vector(0,-9.8,0)
  129. Fgravitation=ball.mass*vector(0,-9.81,0)
  130.  
  131.  
  132. # resistance
  133. C=0.5
  134. rho = 1.29 #kg/m^3
  135. A = pi*ball.radius**2 #pi*r^2
  136. FairResistance=-0.5*C*rho*A*mag2(ball.velocity)*norm(ball.velocity)
  137. F=FairResistance+Fgravitation
  138.  
  139.  
  140.  
  141.  
  142. t=0
  143. dt=0.01
  144.  
  145. while True:
  146.     rate(100)
  147.     t+=dt
  148.     if ball.pos.y >= start.y:
  149.  
  150.  
  151.  
  152.         A = pi*ball.radius**2
  153.         FairResistance=-0.5*C*rho*A*mag2(ball.velocity)*norm(ball.velocity)
  154.         F=FairResistance+Fgravitation
  155.  
  156.         ball.acceleration=F/ball.mass
  157.  
  158.         ball.velocity+=ball.acceleration*dt
  159.         ball.pos+=ball.velocity*dt
RAW Paste Data
Top