• fullscreen
  • noc_walker.pde
  • walker.pde
  • import controlP5.*;
    
    int max_particle = 50;
    Walker []walkers = new Walker[max_particle];
    PVector anchor;
    ControlP5 controlP5;
    
    ControlWindow controlWindow;
    public float global_acceleration = 0.1;
    public float global_attraction = 0.3;
    public float global_chaos = 15;
    public int global_particles = 5;
    public int noiseCounter = 0;
    public int velNoiseCounter = 0;
    
    
    
    void setup() {
    	size(800,600);
    	smooth();
    	background(0);
    	
    	controlP5 = new ControlP5(this);
    	controlP5.setAutoDraw(false);
    	controlWindow = controlP5.addControlWindow("controlP5window",100,100,250,150);
    	controlWindow.setBackground(color(40));
    	Controller attraction_slider = controlP5.addSlider("global_acceleration",0,1,global_acceleration,10,10,100,10);
    	Controller adjust_slider = controlP5.addSlider("global_attraction",0,1,global_attraction,10,30,100,10);
    	Controller step_slider = controlP5.addSlider("global_chaos",0,40,global_chaos,10,50,100,10);
    	Controller particle_slider = controlP5.addSlider("global_particles",1,max_particle,global_particles,10,70,100,10);
    	
    	attraction_slider.setWindow(controlWindow);
    	adjust_slider.setWindow(controlWindow);
    	step_slider.setWindow(controlWindow);
    	particle_slider.setWindow(controlWindow);
    	
    	
    	for (int i = 0; i < walkers.length ; i++)
    	{
    		walkers[i] = new Walker();
    	}
    	
    	anchor = new PVector(width, height);
    	
    	
    }
    
    void draw() {
    	noStroke();
    	fill(0,50);
    	rect(0,0,width,height);
    	
    	for (int i = 0; i < global_particles ; i++)
    	{
    		walkers[i].walk();
    		walkers[i].render();
    	}
    	
    	//draw anchor
    	noStroke();
    	fill(255,0,0);
    	ellipse(anchor.x,anchor.y,1,1);
    	noFill();
    	stroke(230,0,0);
    	line(anchor.x - 4,anchor.y,anchor.x + 4,anchor.y);
    	line(anchor.x,anchor.y - 4,anchor.x,anchor.y + 4);
    	
    }
    
    void keyPressed()
    {
    	if (key == 'c' || key == 'C')
    	{
    		//cameraMode = !cameraMode;
    	}
    }
    
    void mousePressed()
    {
    	anchor.x = mouseX;
    	anchor.y = mouseY;
    }
    
    void mouseDragged()
    {
    	anchor.x = mouseX;
    	anchor.y = mouseY;
    }
    
    class Walker 
    {
    	public PVector location;
    	public PVector velocity;
    
    	Walker() 
    	{
    		location = new PVector(width/2 + random(0,20) - 10,height/2 + random(0,20) - 10);
    		velocity = new PVector(0,0);
    	}
    
    	void render() 
    	{
    		float walkerSize = map(noise(++noiseCounter),0,1,3,8);
    
    			// Display circle at x location
    			stroke(140);
    			//noStroke();
    			fill(80);
    			
    			ellipse(location.x,location.y,walkerSize,walkerSize);
    	}
    
    	// Randomly move up, down, left, right, or stay in one place
    	void walk() 
    	{
    		
    		//velocity.x = global_chaos * (noise(++velNoiseCounter) - 0.499);
    		//velocity.y = global_chaos * (noise(++velNoiseCounter) - 0.499);
    		
    		velocity.x = global_chaos * (random(0,1) - 0.5);
    		velocity.y = global_chaos * (random(0,1) - 0.5);
    		
    		//println(velocity.x + "," + velocity.y);
    		
    		if (random(0,1) > (1 - global_attraction))
    		{
    			PVector gravity = new PVector(anchor.x,anchor.y);
    			gravity.sub(location);
    			gravity.mult(global_acceleration * 0.3);
    			velocity.add(gravity);
    		}
    		
    		
    		// Add the current speed to the location.
    		location.add(velocity);
    		
    		// Stay on the screen
    		location.x = constrain(location.x,0,width-1);
    		location.y = constrain(location.y,0,height-1);
    		
    		
    		
    	}
    }
    

    code

    tweaks (0)

    about this sketch

    This sketch is running as Java applet, exported from Processing.

    license

    advertisement

    Report Sketch

    Report for inappropriate content

    Please provide details if possible:

    Your have successfully reported the sketch. Thank you very much for helping to keep OpenProcessing clean and tidy :)

    Make a Copyright Infringement claim

    Liangjie Xia

    walker for nature of code class

    Add to Faves Me Likey@! 7
    You must login/register to add this sketch to your favorites.

    You need to login/register to comment.