水平抛射

作者:王一哲
日期:2018/3/20


將一個小球由高度 h 處以水平初速 v0 抛出,小球受到重力作用向下加速,小球撞到地板時反彈,共有以下3種不同的狀況:

  1. 只考慮重力的作用,小球與地板間為彈性碰撞。 (GlowScript 網站動畫連結
  2. 只考慮重力的作用,小球撞地板的恢復係數為 e。 (GlowScript 網站動畫連結
  3. 同時考慮重力及空氣阻力 f = -bv。 (GlowScript 網站動畫連結

成果如下:

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
只考慮重力的作用,小球與地板間為彈性碰撞

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
只考慮重力的作用,小球撞地板的恢復係數為 e = 0.9

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
只考慮重力的作用,小球撞地板的恢復係數為 e = 0.8

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
同時考慮重力及空氣阻力 f = -bv, b = 0.1

程式 5-1:水平抛射 (取得程式碼

""" VPython教學: 5-1.水平抛射 Ver. 1: 2018/2/19 Ver. 2: 2019/9/6 作者: 王一哲 """ from vpython import * """ 1. 參數設定, 設定變數及初始值 """ size = 1 # 小球半徑 v0 = 5 # 小球水平初速 h = 15 # 小球離地高度 L = 50 # 地板長度 g = 9.8 # 重力加速度 9.8 m/s^2 i = 0 # 小球撞地板次數 t = 0 # 時間 dt = 0.001 # 時間間隔 """ 2. 畫面設定 """ scene = canvas(title="Projectile", width=800, height=600, x=0, y=0, center=vec(0, h/2, 0), background=vec(0, 0.6, 0.6)) floor = box(pos=vec(0, -size, 0), size=vec(L, 0.01, 10), texture=textures.metal) ball = sphere(pos=vec(-L/2, h, 0), radius=size, texture=textures.wood, make_trail=True, v=vec(v0, 0, 0), a=vec(0, -g, 0)) """ 3. 物體運動部分, 小球觸地時反彈 """ while ball.pos.x < L/2: rate(1000) ball.v += ball.a*dt ball.pos += ball.v*dt if ball.pos.y - floor.pos.y <= size + 0.5*floor.height and ball.v.y < 0: i += 1 print(i, t, ball.pos.x + L/2) ball.v.y = -ball.v.y t += dt

參數設定

在此定義的變數有 size、v0、h、L、g、i、t、dt,用途都已經寫在該行的註解中。

畫面設定

  1. 這次在 sphere 及 box 物件當中沒有設定顏色 (color),反而設定了材質 (texture),語法為
    ​​​​texture = textures.[材質名稱]
    
    VPython 7 目前支援的材質有下列12種。
VPython 7 支援的材質

VPython 7 支援的顏色

也可以使用網路上的圖片自訂材質,只要將圖片網址加在 texture 之後並用引號包起來即可,以木星圖片為例

jupiter = sphere(pos=vec(0, 0, 0), radius=1, 
                 texture="https://upload.wikimedia.org/wikipedia/commons/c/c1/Jupiter_New_Horizons.jpg")

使用網路圖片自訂材質(木星圖片來源 https://upload.wikimedia.org/wikipedia/commons/c/c1/Jupiter_New_Horizons.jpg

內建的地球材質

  1. 用 sphere 產生小球 ball 時可以同時設定初速度及加速度,也可以之後再用以下的方式設定。

    ​​​​ball.v = vec(v0, 0, 0)
    ​​​​ball.a = vec(0, -g, 0)
    

物體運動

  1. 在 while 迴圈中設定的條件為

    ​​​​ball.pos.x < L/2
    

    所以動畫會一直執行直到小球到達地板的右側邊緣為止。

  2. 當小球撞到地板時,將撞擊次數 i 加 1,印出撞擊次數 i、經過時間 t、水平射程 ball.pos.x + L/2,並將小球的 y 方向速度反向,因此要加上以下的程式碼

    ​​​​if ball.pos.y - floor.pos.y <= size + 0.5*floor.height and ball.v.y < 0:
    ​​​​    i += 1
    ​​​​    print(i, t, ball.pos.x + L/2)
    ​​​​    ball.v.y = -ball.v.y
    

程式 5-2:水平抛射,恢復係數e (取得程式碼

""" VPython教學: 5-2.水平抛射, 恢復係數e Ver. 1: 2018/2/19 Ver. 2: 2019/9/6 作者: 王一哲 """ from vpython import * """ 1. 參數設定, 設定變數及初始值 """ size = 1 # 小球半徑 v0 = 5 # 小球水平初速 e = 0.8 # 恢復係數 h = 15 # 小球離地高度 L = 50 # 地板長度 g = 9.8 # 重力加速度 9.8 m/s^2 i = 0 # 小球撞地板次數 t = 0 # 時間 dt = 0.001 # 時間間隔 """ 2. 畫面設定 """ scene = canvas(title="Projectile", width=800, height=600, x=0, y=0, center=vec(0, h/2, 0), background=vec(0, 0.6, 0.6)) floor = box(pos=vec(0, -size, 0), size=vec(L, 0.01, 10), texture=textures.metal) ball = sphere(pos=vec(-L/2, h, 0), radius=size, texture=textures.wood, make_trail=True, v=vec(v0, 0, 0), a=vec(0, -g, 0)) """ 3. 物體運動部分, 小球觸地時反彈 """ while ball.pos.x < L/2: rate(1000) ball.v += ball.a*dt ball.pos += ball.v*dt if ball.pos.y - floor.pos.y <= size + 0.5*floor.height and ball.v.y < 0: i += 1 print(i, t, ball.pos.x + L/2) ball.v.y = -ball.v.y*e t += dt

程式 5-2 與 5-1 幾乎一模一樣,只有增加恢復係數 e,並在小球撞擊地板時改為

if ball.pos.y - floor.pos.y <= size + 0.5*floor.height and ball.v.y < 0:
    i += 1
    print(i, t, ball.pos.x + L/2)
    ball.v.y = -ball.v.y*e

程式 5-3:水平抛射,考慮重力及空氣阻力 f = -bv (取得程式碼

""" VPython教學: 5-3.水平抛射, 恢復係數e, 空氣阻力 f = -bv Ver. 1: 2018/2/19 Ver. 2: 2019/9/6 作者: 王一哲 """ from vpython import * """ 1. 參數設定, 設定變數及初始值 """ size = 1 # 小球半徑 v0 = 5 # 小球水平初速 e = 1 # 恢復係數 m = 1 # 小球質量 h = 15 # 小球離地高度 L = 50 # 地板長度 g = 9.8 # 重力加速度 9.8 m/s^2 b = 0.1 # 空氣阻力 f = -bv i = 0 # 小球撞地板次數 t = 0 # 時間 dt = 0.001 # 時間間隔 """ 2. 畫面設定 """ scene = canvas(title="Projectile", width=800, height=600, x=0, y=0, center=vec(0, h/2, 0), background=vec(0, 0.6, 0.6)) floor = box(pos=vec(0, -size, 0), size=vec(L, 0.01, 10), texture=textures.metal) ball = sphere(pos=vec(-L/2, h, 0), radius=size, texture=textures.wood, make_trail=True, v=vec(v0, 0, 0), a=vec(0, -g, 0)) """ 3. 物體運動部分, 小球觸地時反彈 """ while ball.pos.x < L/2 and i < 20: rate(1000) f = -b*ball.v ball.a = vec(0, -g, 0) + f/m ball.v += ball.a*dt ball.pos += ball.v*dt if ball.pos.y - floor.pos.y <= size + 0.5*floor.height and ball.v.y < 0: i += 1 print(i, t, ball.pos.x + L/2) ball.v.y = -ball.v.y*e t += dt

程式 5-3 與 5-2 幾乎一模一樣,不同之處在於:

  1. 增加了空氣阻力的係數 b。

  2. while 迴圈當中增加

    ​​​​    f = -b*ball.v
    ​​​​    ball.a = vec(0, -g, 0) + f/m
    

    計算當時的空氣阻力及小球的加速度。

  3. 為了避免小球受到的阻力太大,小球沒辦法到達地板的右側邊緣,因此在 while 迴圈的條件當中增加了 i < 20,當小球撞擊地板20次時停止動畫。

結語

程式 5-1 是為了寫出 5-2 及 5-3 的前置作業,先寫出最簡單程式再慢慢把條件加上去。在程式 5-1 中,可以試著將 h 改成不同的值,看看小球撞擊地板所需時間與高二物理中學到的理論計算是否相符。在程式 5-2 中,可以試著將 e 改成不同的值,觀察每次撞擊後反彈的高度飛行時間。在程式 5-3 中,可以試著將 b 改成不同的值,觀察小球運動的軌跡有何不同之後。

VPython官方說明書

  1. canvas: http://www.glowscript.org/docs/VPythonDocs/canvas.html
  2. box: http://www.glowscript.org/docs/VPythonDocs/box.html
  3. sphere: http://www.glowscript.org/docs/VPythonDocs/sphere.html
  4. texture: http://www.glowscript.org/docs/VPythonDocs/textures.html

tags: VPython