Skip to content

Instantly share code, notes, and snippets.

@horstjens
Created January 9, 2021 09:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save horstjens/09a1e2ad768e6e827a80624121b98f3e to your computer and use it in GitHub Desktop.
Save horstjens/09a1e2ad768e6e827a80624121b98f3e to your computer and use it in GitHub Desktop.
peter_emile_vball
import vpython as v
xpointer = v.arrow(
pos = v.vector(0,0,0),
axis= v.vector(1,0,0),
shaftwidth = 0.1,
color=v.color.yellow,
)
ypointer = v.arrow(
pos = v.vector(0,0,0),
axis= v.vector(0,1,0),
shaftwidth = 0.1,
color=v.color.purple,
)
zpointer = v.arrow(
pos = v.vector(0,0,0),
axis= v.vector(0,0,1),
shaftwidth = 0.1,
color=v.color.blue,
)
xlabel = v.label(
pos=xpointer.pos + xpointer.axis,
text='x', xoffset=0, yoffset=5,space=1, height=16, border=4
)
ylabel = v.label(
pos=ypointer.pos + ypointer.axis,
text='y', xoffset=0, yoffset=5,space=1, height=16, border=4
)
zlabel = v.label(
pos=zpointer.pos + zpointer.axis,
text='z', xoffset=0, yoffset=5,space=1, height=16, border=4
)
story = "Es war einmal in \nin einem Universum\nweit weit weg..."
#mytext = v.text(
# text=story,
# align='center',
# color=v.color.green,
# pos=v.vector(0,2,0),
# )
#mybox = v.box()
# define Aquarium
wallR = v.box(
pos=v.vector(16,0,0),
size=v.vector(0.6,12,12),
color=v.color.green)
wallL = v.box(
pos=v.vector(-16,0,0),
size=v.vector(0.6,12,12),
color=v.color.green)
wallH = v.box(
pos=v.vector(0,6,-6),
size=v.vector(32,32,0.6),
color=v.color.green)
wallV = v.box(
pos=v.vector(0,6,6),
size=v.vector(32,32,0.6),
color=v.color.blue,opacity=0.1)
wallO = v.box(
pos=v.vector(0,40,0),
size=v.vector(32,0.6,32),
color=v.vector(0.8,0.3,0.0))
wallU = v.box(
pos=v.vector(0,-40,0),
size=v.vector(32,0.6,32),
color=v.vector(0.8,0.3,0.0))
myball = v.sphere(
pos=v.vector(1,1,1),
color=v.color.red,
radius = 0.5,
)
kugel_move = v.vector(v.random()*2-1, v.random()*2-1, v.random()*2-1)
dt = 0.005 # delta t
while True:
v.rate(200)
# kugel bewegung
myball.pos += kugel_move * dt * 5 # 5x so schnell
# reflect ball from walls
# left, right
if myball.pos.x > wallR.pos.x:
myball.pos.x = wallR.pos.x
kugel_move.x *= -1
if myball.pos.x < wallL.pos.x:
myball.pos.x = wallL.pos.x
kugel_move.x *= -1
# up, down
if myball.pos.y > wallO.pos.y:
myball.pos.y = wallO.pos.y
kugel_move.y *= -1
if myball.pos.y < wallU.pos.y:
myball.pos.y = wallU.pos.y
kugel_move.y *= -1
# front, back
if myball.pos.z > wallV.pos.z:
myball.pos.z = wallV.pos.z
kugel_move.z *= -1
if myball.pos.z < wallH.pos.z:
myball.pos.z = wallH.pos.z
kugel_move.z *= -1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment