이번에는 수평, 수직 방향으로 운동하는 두 물체를 물리코딩으로 구현하여 살펴보는 시간을 가졌다.
크기와 무게 모두 같고 색만 다른 두 공이 만약 외력을 받지 않고 중력만 작용한다면,
보이는 것과 같이 동시에 지표면에 도달하게 된다.
또한 역학적 에너지는 보존됨으로 운동에너지와 위치에너지의 합은
위의 그래프와 같이 일정하게 보존됨을 확인할 수 있다.
코드는 지표면에 두 공이 닿기까지의 while 무한루프를 돌려 구현했다.
코드)
GlowScript 3.0 VPython
#Creating Objects & Scene Setting
ball1 = sphere(pos = vec(-15,20,0), color = color.blue)
ball2 = sphere(pos = vec(-12,20,0), color = color.red)
ground = box(color = color.green, size = vec(40,0.01,20))
scene.center = 0.5*(ball1.pos + ground.pos)
scene.background = color.white
#Initial Setting & Constants
ball1.m = 1
ball2.m = 1 #2
ball1.v = vec(0,0,0)
ball2.v = vec(15,0,0)
g = 9.8
ball1.U = ball1.m*g*ball1.pos.y
ball2.U = ball2.m*g*ball2.pos.y
ball1.K = 0.5*ball1.m*mag(ball1.v)**2
ball2.K = 0.5*ball2.m*mag(ball2.v)**2
ball1.work = 0
ball2.work = 0
#Graph
gd = graph(xtitle = 's', ytitle = 'J')
ball1_Wgraph = gcurve(color = color.blue)
ball2_Wgraph = gcurve(color = color.red)
ball1_Ugraph = gcurve(color = color.cyan)
#Time
t = 0
dt = 0.01
scene.waitfor('click')
#Simulation Loop
while True:
rate(1/dt)
#Forces
ball1.f = ball1.m*vec(0,-g,0)
ball2.f = ball2.m*vec(0,-g,0)
#Time integration
ball1.v = ball1.v + ball1.f/ball1.m*dt
ball2.v = ball2.v + ball2.f/ball2.m*dt
ball1.pos = ball1.pos+ ball1.v*dt
ball2.pos = ball2.pos+ ball2.v*dt
#Work done by gravity
ball1.work = ball1.work + dot(ball1.f,ball1.v*dt)
ball2.work = ball2.work + dot(ball2.f,ball2.v*dt)
ball1_Wgraph. plot(t, ball1.work)
#Potential Energy
ball1.U = ball1.m*g*ball1.pos.y
ball2.U = ball2.m*g*ball2.pos.y
ball1_Ugraph.plot(t, ball1.U)
#Detecting Collision
if ball1.pos.y < ground.pos.y:
break
t = t + dt
GlowScript IDE
GlowScript is an easy-to-use, powerful environment for creating 3D animations and publishing them on the web. Here at glowscript.org, you can write and run GlowScript programs right in your browser, store them in the cloud for free, and easily share them w
www.glowscript.org
'물리코딩' 카테고리의 다른 글
등속도, 등속직선, 등가속도 운동 (0) | 2021.12.31 |
---|---|
3체 운동 (0) | 2021.07.07 |
달이 멈춘다면 ?! (0) | 2021.06.24 |
달의 공전 (0) | 2021.06.24 |
지구의 자전 (0) | 2021.06.24 |