728x90
반응형
SMALL
Animations
Moving Graphs에서 볼 수 있듯이, Animations의 핵심은 일정 시간 동안 동작을 수행할 수 있는 rate(#)이다.
animation이 무한 재생되도록 하려면 (while True:) loop를 사용하고, (for i in range(#):) loop는 finite 시간 동안 재생한다.
참고 : 프로그램이 고장나면 loop에 rate(#)가 있는지 확인.
Bouncing Ball Example
# make sphere and 2 wall objects
my_sphere = sphere(pos=vector(0,0,0), radius=0.25, color=color.green )
wall1 = box(pos=vector(2,0,0), size=vector(0.1,1,1), color=color.white)
wall2 = box(pos=vector(-2,0,0), size=vector(0.1,1,1), color=color.white)
# define the inner edge that ball hits
edge1 = wall1.pos.x - wall1.size.x/2
edge2 = wall2.pos.x - wall2.size.x/2
# while loop does the animation
dx = 0.01 # dx is distance ball moves for each loop
while True:
rate(100) # delay between each loop
if (my_sphere.pos.x + my_sphere.radius >= edge1) or
(my_sphere.pos.x – my_sphere.radius <= edge2):
dx = -dx # if sphere at wall, reverse direction
my_sphere.pos.x = my_sphere.pos.x+dx # move sphere’s x position by dx
한정된 시간 동안 animation을 실행하려면(ex. 100 loops) (while True:) loop를 replace한다.
for i in range(100):
Animation with Moving Graph Example
# create sphere object
my_sphere = sphere(pos=vector(1,0,0), size=vector(0.5,0.5,0.5))
# create graph
g = graph(scroll=True, xmin=0, xmax=10)
x_plot = gcurve(graph=g, color=color.red, label="x")
y_plot = gcurve(graph=g, color=color.green, label="y")
# animation
time = 0
while True:
rate(100)
# move sphere x an y using cos and sin
my_sphere.pos.x = cos(time)
my_sphere.pos.y = sin(time)
# plot new x and y
x_plot.plot(time,cos(time))
y_plot.plot(time,sin(time))
time += 0.01 # increase time by 0.01
Rotating Gear Example – Animating 2D Shapes
# create gear shape
g = shapes.gear(n=20)
gear1 = extrusion(path=[vector(0,0,0), vector(0,0,0.1)],
shape=g, pos=vector(2,0,0))
gear2 = extrusion(path=[vector(0,0,0), vector(0,0,0.1)],
shape=g, pos=vector(0,0,0))
# rotate gear animation
gear1.rotate(axis=vector(0,0,1), angle=0.1)
while True:
rate(10)
gear1.rotate(axis=vector(0,0,1), angle=0.05) # rotate CCW
gear2.rotate(axis=vector(0,0,1), angle=-0.05) # rotate CW
728x90
반응형
LIST
'Python Library > VPython' 카테고리의 다른 글
[VPython] Mouse and Keyboard Events (0) | 2022.01.18 |
---|---|
[VPython] Math Functions (0) | 2022.01.18 |
[VPython] Graphs (2) (0) | 2022.01.17 |
[VPython] Graphs (1) (0) | 2022.01.17 |
[VPython] Widgets (0) | 2022.01.17 |