Python Forum
Changing object values to see how air resistance works
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Changing object values to see how air resistance works
#1
Hey all, I need to make a program in Vpython where you can press a button to launch a ball, which is affected by the air resistance, forward, and change things like the mass and radius of the ball to see how the air resistance affects the flying arc. I got most of the code ready but I'm having problems on actually making the air resistance to work properly. Currently when I press the launch button on the program, nothing seems to happen. When I increase the mass of the ball, the ball gets launched skyhigh.

from visual import *
 
import wx
 
#(default window)
scene.width = 800
scene.height = 600
scene.title='Ball throwing with air resistance'
 
#############################################################################  
 
 
w = window(x=800,y=0,width=350, height=350, menus=True,title='default settings')
 
#widget settings
 
 
def setAngle(evt): # called on slider 1 events,
    #someValue = slider1.GetValue()  # value is min-max slider position, 0 to 90
    ball.make_trail=False
    ball.pos=start
    ball.acceleration=vector(0,0,0)
    ball.velocity=vector(0,0,0)
 
def setSpeed(evt): # called on slider 2 events
    #someValue = slider2.GetValue() # value is min-max slider position, 0 to 50
    ball.make_trail=False
    ball.pos=start
    ball.acceleration=vector(0,0,0)
    ball.velocity=vector(0,0,0)
 
def setMass(evt): # called on slider 3 events
    global mass
    mass = slider3.GetValue()# value is min-max slider position, 0.15 to 10
    ball.make_trail=False
    ball.pos=start
    ball.acceleration=vector(0,0,0)
    ball.velocity=vector(0,0,0)
 
def setRadius(evt): # called on slider 4 events
    global radius
    radius = slider4.GetValue()# value is min-max slider position, 0.08 to 5
    ball.make_trail=False
    ball.pos=start
    ball.acceleration=vector(0,0,0)
    ball.velocity=vector(0,0,0)
 
 
def shot(evt):
 
    ball.pos=start
    angle0=slider1.GetValue()
    v0mag=slider2.GetValue()
    vy=v0mag*sin(radians(angle0))
    vx=v0mag*cos(radians(angle0))
    ball.velocity=vector(vx,vy,0)
    ball.acceleration=vector(0,-9.8,0)
    ball.mass=slider3.GetValue()
    ball.radius=slider4.GetValue()
    Fgravitation=ball.mass*vector(0,-9.81,0)
    ball.make_trail=True
 
 
#### texts and sliders
p = w.panel
 
 
wx.StaticText(p, pos=(10,10), size=(300,30),
    label='Set with the sliders',
              style=wx.ALIGN_LEFT | wx.ST_NO_AUTORESIZE)

slider1 = wx.Slider(p, pos=(10,70), size=(100,20), minValue=0, maxValue=90)
slider1.Bind(wx.EVT_SCROLL, setAngle)
wx.StaticText(p, pos=(10,50), label='Set angle (0-90) ')
 
slider2 = wx.Slider(p, pos=(10,150), size=(100,20), minValue=0, maxValue=50)
slider2.Bind(wx.EVT_SCROLL, setSpeed)
wx.StaticText(p, pos=(10,120), label='Set speed (0-50) m/s')
 
slider3=wx.Slider(p, pos=(10,200), size=(100,20), minValue=0.15, maxValue=10)
slider3.Bind(wx.EVT_SCROLL, setMass)
wx.StaticText(p,pos=(10,180), label='Set mass kg')
 
slider4=wx.Slider(p, pos=(10,250), size=(100,20), minValue=0.08, maxValue=5)
slider4.Bind(wx.EVT_SCROLL, setRadius)
wx.StaticText(p,pos=(10,220), label='Set radius m')
 
 
shotButton = wx.Button(p, pos=(150,190),label='Launch')
shotButton.Bind(wx.EVT_BUTTON, shot)
 
 
#####################################################################
 
 
# throwing movement from start.
# sliders change the throwing angle, speed, mass and radius
# start after pressing button
 
start=vector(-30,-20,0)
curve(pos=[start,start+vector(50,0,0)])
points(pos=start, size=3, color=color.red)
 
ball=sphere(pos=start,radius=0.1,color=color.magenta,make_trail=True)
 
# start values
slider1.SetValue(45)
angle0=slider1.GetValue()
slider2.SetValue(25)
v0mag=slider2.GetValue()
slider3.SetValue(0.15)
ball.mass=slider3.GetValue()
slider4.SetValue(0.08)
ball.radius=slider4.GetValue()
 
 
# velocity acceleration and gravitation
vy=v0mag*sin(radians(angle0))
vx=v0mag*cos(radians(angle0))
ball.velocity=vector(0,0,0)# set to 0 before launch
ball.acceleration=vector(0,-9.8,0)
Fgravitation=ball.mass*vector(0,-9.81,0)
 
 
# resistance
C=0.5
rho = 1.29 #kg/m^3
A = pi*ball.radius**2 #pi*r^2
FairResistance=-0.5*C*rho*A*mag2(ball.velocity)*norm(ball.velocity)
F=FairResistance+Fgravitation
 
t=0
dt=0.01
 
while True:
    rate(100)
    t+=dt
    if ball.pos.y >= start.y:
 
        A = pi*ball.radius**2
        FairResistance=-0.5*C*rho*A*mag2(ball.velocity)*norm(ball.velocity)
        F=FairResistance+Fgravitation
 
        ball.acceleration=F/ball.mass
 
        ball.velocity+=ball.acceleration*dt
        ball.pos+=ball.velocity*dt
Does anyone know where's the problem? Are the sliders correctly set? (I think) I got the air resistance to work without the mass and radius slider codes, but I need help on how to make it work with those sliders too.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020