Skip to content

Instantly share code, notes, and snippets.

@demiurg906
Created October 3, 2019 15:18
Show Gist options
  • Save demiurg906/fe06945edfb1777ed2fc2dd75c425013 to your computer and use it in GitHub Desktop.
Save demiurg906/fe06945edfb1777ed2fc2dd75c425013 to your computer and use it in GitHub Desktop.
GlowScript 2.9 VPython
xmax = 100
ymax = 100
zmax = 100
# Количество красных шариков
a = 100
# Количество синих шариков
b = 100
# Радиус синих шариков
arad = 1.6
# Радиус красных шариков
brad = 1.6
c = 0
aact = a
dt = 0.2
deltat = 150
acolor = vector(1, 0.1, 0.1)
bcolor = vector(0.2, 0.2, 1)
ccolor = vector(0.1, 1, 0.1)
amol = []
bmol = []
t = 0
t1 = 0
for i in arange(a):
amol.append(sphere(pos=vector(xmax * random(), ymax * random(), zmax * random()), radius=arad, color=acolor))
amol[i].vel = vector(random() - 0.5, random() - 0.5, random() - 0.5)
amol[i].react = 0
for i in arange(b):
bmol.append(sphere(pos=vector(xmax * random(), ymax * random(), zmax * random()), radius=brad, color=bcolor))
bmol[i].vel = vector(random() - 0.5, random() - 0.5, random() - 0.5)
bmol[i].react = 0
xx = box(pos=vector(0, 0, 0), size=vector(0.2, 0.2, 0.2), color=vector(0.1, 0.1, 0.1))
# def reaction():
# xx.pos = (1, 0, 0)
# c = controls(title='Controlling the Scene', x=50, y=50, width=200, height=200, range=50)
# but = button(pos=(0, 0), width=60, height=60, text='Go!', bind=lambda: reaction())
# box(pos=vector(-arad, ymax / 2., zmax / 2.), size=vector(0.5, ymax + 2 * arad, zmax + 2 * arad),
# color=vector(0.3, 0.3, 0.3))
# box(pos=vector(xmax + arad, ymax / 2., zmax / 2.), size=vector(0.5, ymax + 2 * arad, zmax + 2 * arad),
# color=vector(0.3, 0.3, 0.3))
# box(pos=vector(xmax / 2., -arad, zmax / 2.), size=vector(xmax + 2 * arad, 0.5, zmax + 2 * arad),
# color=vector(0.3, 0.3, 0.3))
# box(pos=vector(xmax / 2., ymax + arad, zmax / 2.), size=vector(xmax + 2 * arad, 0.5, zmax + 2 * arad),
# color=vector(0.3, 0.3, 0.3))
# box(pos=vector(xmax / 2., ymax / 2., -arad), size=vector(xmax + 2 * arad, ymax + 2 * arad, 0.5),
# color=vector(0.2, 0.2, 0.2))
box(pos=vector(-arad, ymax / 2., zmax / 2.), size=vector(0.5, ymax + 2 * arad, zmax + 2 * arad),
color=vector(0.3, 0.3, 0.3))
box(pos=vector(xmax + arad, ymax / 2., zmax / 2.), size=vector(0.5, ymax + 2 * arad, zmax + 2 * arad),
color=vector(0.3, 0.3, 0.3))
box(pos=vector(xmax / 2., -arad, zmax / 2.), size=vector(xmax + 2 * arad, 0.5, zmax + 2 * arad),
color=vector(0.3, 0.3, 0.3))
box(pos=vector(xmax / 2., ymax + arad, zmax / 2.), size=vector(xmax + 2 * arad, 0.5, zmax + 2 * arad),
color=vector(0.3, 0.3, 0.3))
box(pos=vector(xmax / 2., ymax / 2., -arad), size=vector(xmax + 2 * arad, ymax + 2 * arad, 0.5),
color=vector(0.2, 0.2, 0.2))
while xx.pos == (0, 0, 0):
rate(5000)
for i in arange(a):
if amol[i].pos.x >= xmax - arad:
amol[i].vel.x = -amol[i].vel.x
if amol[i].pos.x <= arad:
amol[i].vel.x = -amol[i].vel.x
if amol[i].pos.y >= ymax - arad:
amol[i].vel.y = -amol[i].vel.y
if amol[i].pos.y <= arad:
amol[i].vel.y = -amol[i].vel.y
if amol[i].pos.z >= ymax - arad:
amol[i].vel.z = -amol[i].vel.z
if amol[i].pos.z <= arad:
amol[i].vel.z = -amol[i].vel.z
amol[i].pos = amol[i].pos + amol[i].vel * dt
for i in arange(b):
if bmol[i].pos.x >= xmax - brad:
bmol[i].vel.x = -bmol[i].vel.x
if bmol[i].pos.x <= brad:
bmol[i].vel.x = -bmol[i].vel.x
if bmol[i].pos.y >= ymax - brad:
bmol[i].vel.y = -bmol[i].vel.y
if bmol[i].pos.y <= brad:
bmol[i].vel.y = -bmol[i].vel.y
if bmol[i].pos.z >= ymax - brad:
bmol[i].vel.z = -bmol[i].vel.z
if bmol[i].pos.z <= brad:
bmol[i].vel.z = -bmol[i].vel.z
bmol[i].pos = bmol[i].pos + bmol[i].vel * dt
for j in arange(a):
if mag(amol[j].pos - bmol[i].pos) <= arad + brad:
bmol[i].vel = bmol[i].vel + (dot(amol[j].vel - bmol[i].vel, bmol[i].pos - amol[j].pos) / (
mag(amol[j].pos - bmol[i].pos) ** 2)) * (bmol[i].pos - amol[j].pos)
amol[j].vel = amol[j].vel - (dot(amol[j].vel - bmol[i].vel, bmol[i].pos - amol[j].pos) / (
mag(amol[j].pos - bmol[i].pos) ** 2)) * (bmol[i].pos - amol[j].pos)
while t <= True:
rate(50)
for i in arange(a):
if amol[i].pos.x >= xmax - arad:
amol[i].vel.x = -amol[i].vel.x
if amol[i].pos.x <= arad:
amol[i].vel.x = -amol[i].vel.x
if amol[i].pos.y >= ymax - arad:
amol[i].vel.y = -amol[i].vel.y
if amol[i].pos.y <= arad:
amol[i].vel.y = -amol[i].vel.y
if amol[i].pos.z >= ymax - arad:
amol[i].vel.z = -amol[i].vel.z
if amol[i].pos.z <= arad:
amol[i].vel.z = -amol[i].vel.z
amol[i].pos = amol[i].pos + amol[i].vel * dt
for i in arange(b):
if bmol[i].pos.x >= xmax - brad:
bmol[i].vel.x = -bmol[i].vel.x
if bmol[i].pos.x <= brad:
bmol[i].vel.x = -bmol[i].vel.x
if bmol[i].pos.y >= ymax - brad:
bmol[i].vel.y = -bmol[i].vel.y
if bmol[i].pos.y <= brad:
bmol[i].vel.y = -bmol[i].vel.y
if bmol[i].pos.z >= ymax - brad:
bmol[i].vel.z = -bmol[i].vel.z
if bmol[i].pos.z <= brad:
bmol[i].vel.z = -bmol[i].vel.z
bmol[i].pos = bmol[i].pos + bmol[i].vel * dt
for j in arange(a):
if mag(amol[j].pos - bmol[i].pos) <= arad + brad:
bmol[i].vel = bmol[i].vel + (dot(amol[j].vel - bmol[i].vel, bmol[i].pos - amol[j].pos) / (
mag(amol[j].pos - bmol[i].pos) ** 2)) * (bmol[i].pos - amol[j].pos)
amol[j].vel = amol[j].vel - (dot(amol[j].vel - bmol[i].vel, bmol[i].pos - amol[j].pos) / (
mag(amol[j].pos - bmol[i].pos) ** 2)) * (bmol[i].pos - amol[j].pos)
if amol[j].react == 0:
if bmol[i].react == 0:
amol[j].react = 1
bmol[i].react = 1
amol[j].color = ccolor
bmol[i].color = ccolor
aact = aact - 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment