Skip to content

Instantly share code, notes, and snippets.

@SamyBencherif
Created March 3, 2016 21:43
Show Gist options
  • Save SamyBencherif/213ac4c2baf87e2a9f2a to your computer and use it in GitHub Desktop.
Save SamyBencherif/213ac4c2baf87e2a9f2a to your computer and use it in GitHub Desktop.
Energy Animation.py
# coding: utf-8
from scene import *
from random import *
from math import *
def mag(v):
return (v.x**2+v.y**2)**.5
class MyScene (Scene):
def setup(self):
self.p = []
self.f = []
self.g = []
self.v = []
self.c = 0
k = 300
for i in range(k):
self.p.append(100*Point(cos(i*2*pi/float(k)), sin(i*2*pi/float(k))))
self.f.append(50+50*random())
self.g.append(-20-20*random())
self.v.append(Point(0,0))
def did_change_size(self):
pass
def update(self):
background('#ebfffc')
stroke('#4545ff')
stroke_weight(1)
for i in range(len(self.p)):
#physics
self.v[i] += self.g[i]*self.dt*(self.p[i])#/mag(self.p[i])
self.p[i] += self.v[i]*self.dt
#rendering
p1 = self.p[i]
p2 = self.p[(i+1)%len(self.p)]
line(self.bounds.w/2+p1.x, self.bounds.h/2+p1.y, self.bounds.w/2+p2.x, self.bounds.h/2+p2.y)
def touch_began(self, touch):
self.c += 1
for i in range(len(self.p)):
self.v[i] += self.f[i]*(self.p[i])
self.f[i] = (50+50*random())
self.g[i] = (-20-20*random())
if self.c > 5:
self.setup()
def touch_moved(self, touch):
pass
def touch_ended(self, touch):
pass
if __name__ == '__main__':
run(MyScene(), show_fps=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment