e.append(Particle(0, 0, mass=random(10,50), radius=MASS, life=random(50,250))) s.append(e) # Install the emitter in the system. # Add an obstacle that other particles want to stay away from. class Obstacle(Particle): def draw(self): # The only way to style individual particles is to redefine draw() in a subclass. # Set the fill color directly for this ellipse, i.e. don't use fill(), # because this sets the state and then other particles might use this color too. ellipse(self.x, self.y, self.radius*2, self.radius*2, fill=(0,0,0.3,0.1)) obstacle = Obstacle(0, 0, mass=70, radius=70, fixed=True) s.append(obstacle) s.force(6, source=obstacle) # Repulsive force from this particle to all others. def draw(canvas): background(1) fill(0,0.5,1,0.1) stroke(0,0.3) strokewidth(1) s.update() s.draw() # Put the obstacle at the mouse position. # This way we can play around with the fountain spray. obstacle.x = canvas.mouse.x obstacle.y = canvas.mouse.y
# Add the upper directory (where the nodebox module is) to the search path. import os, sys sys.path.insert(0, os.path.join("..", "..")) from nodebox.graphics import * from nodebox.graphics.physics import Particle, Force, System # This example demonstrates the Animation object, # which can be used to store and replay a sequence of image frames. # This is useful for caching pre-rendered effects, like a spell or an explosion. # This is just the explosion system from the previous example in a more condensed form. s = System(gravity=(0, 1.0)) for i in range(80): s.append(Particle(x=200, y=250, mass=random(5.0, 10.0), radius=5, life=30)) s.force(strength=4.5, threshold=70) # Instead of drawing the system directly to the canvas, # we render each step offscreen as an image, # and then gather the images in an animation loop. explosion = Animation(duration=0.75, loop=False) for i in range(30): s.update(limit=20) img = render(s.draw, 400, 400) explosion.append(img) # This can take several seconds: # - calculating forces in the system is costly (if possible, load Psyco to speed them up), # - rendering each image takes time (specifically, initialising a new empty image). # If possible, reduce the image frames in size (smaller images = faster rendering).
from nodebox.graphics.context import * from nodebox.graphics import * from nodebox.graphics.physics import Particle, Force, System from nodebox.graphics.shader import render # This example demonstrates the Animation object, # which can be used to store and replay a sequence of image frames. # This is useful for caching pre-rendered effects, like a spell or an explosion. # This is just the explosion system from the previous example in a more condensed form. s = System(gravity=(0, 1.0)) for i in range(80): s.append(Particle(x=200, y=250, mass=random(5.0,10.0), radius=5, life=30)) s.force(strength=4.5, threshold=70) # Instead of drawing the system directly to the canvas, # we render each step offscreen as an image, # and then gather the images in an animation loop. explosion = Animation(duration=0.75, loop=False) for i in range(30): s.update(limit=20) img = render(s.draw, 400, 400) explosion.append(img) # This can take several seconds: # - calculating forces in the system is costly (if possible, load Psyco to speed them up), # - rendering each image takes time (specifically, initialising a new empty image). # If possible, reduce the image frames in size (smaller images = faster rendering).