nature-of-code / noc-examples-processing Goto Github PK
View Code? Open in Web Editor NEWRepository for example code from The Nature of Code book
License: MIT License
Repository for example code from The Nature of Code book
License: MIT License
Hi Dan, i found a strange behaviour while making the exercises in the book. I could reproduce the problem in the example NOC_2_5_fluidresistance.pde.
Try to set
movers[i] = new Mover(random(0.1, 0.2), 40+i*70, 0);
at line 62 instead of
movers[i] = new Mover(random(0.5, 3), 40+i*70, 0);
You'll see that the small objects are actually bouncing off the liquid and going up at "speed of light".
Why is this happening? Is it to the low precision of the float data type or am I missing something?
Since Processing 2, you need to import the iterator class explicitly to handle it.
import java.util.Iterator;
In CollisionEqualMass (chapter 5) loc and pos name both are used which causes an error
In Processing example ch_9ga/EvolveFlowField/EvolveFlowField.pde using Processing 2.0b6, I get the following error when trying to run the sketch.
Expecting EOF, found 'import'
This occurred in line 9 of Obstacle.pde, the line that reads import java.awt.Rectangle;
I was able to remedy this by moving this library import line to the main file (EvolveFlowField.pde) at the top of the file, in which I could then run the sketch. Thanks.
In Chapter 1 of The Nature of Code Daniel challenged us with an exercise to develop a simple ecosystem.
You can find my solution here: https://github.com/Haemstead/BlobWorld
Kind regards!
Make bin-lattice spatial subdivision + flocking example
I don't know if it's something unique to my system, but I need to "import java.util.Random;" to have Random available. Otherwise it does not compile. For example in https://github.com/shiffman/The-Nature-of-Code-Examples/blob/master/Processing/introduction/Gaussian2/Gaussian2.pde
the ParticleSystem classes having an enforced origin where particles begin is limiting and confusing to students.
Create an object that both attracts and repels. What if it attracts any particle that is far away but repels those particles at a short distance?
Hi Daniel,
First of all, thanks for the wonderful work on code / videos / and your books.
Just wanted to post an enhancement for a variation for one of the exercises.
I'll call it Big-Bang.
It consists of exactly the same exercise (NOC_02forces_many_mutual_boundaries), but change the start position of all particles to the center of the screen.
Next generate a starting force and on each particle iteration, rotate the force so that each particle starts moving to a different angle simulating an explosion.
void setup() {
size(1500,1000);
for (int i = 0; i < movers.length; i++) {
movers[i] = new Mover(random(1,2),width/2,height/2);
// Determine an angle so that all particles get a force in a unique direction ( 360 / amount of particles )
float angle = 360/movers.length;
// Create a force with a random X
PVector some_force = new PVector(random(0,10),0);
// Rotate the force according to particle i
PVector force = some_force.rotate(radians(angle*(i+1)));
// Apply the force to the particle so that when the program starts, each one is moving in a direction
movers[i].applyForce(force);
}
}
It says Separation in the comment before cohesion() method in Vehicle class.
check them, specifically image particle systems
"Fluid resistance does not only work opposite to the velocity vector, but also perpendicular to it. This is known as “lift-induced drag” and will cause an airplane with an angled wing to rise in altitude. Try creating a simulation of lift."
Need to decide whether to use Iterator in the examples or not. It needs an explicit import now. It seems to be supported by Processing.js but maybe less risk to iterate with i.
Iterator<Particle> it = particles.iterator();
while (it.hasNext()) {
Particle p = it.next();
p.run();
if (p.isDead()) {
it.remove();
}
}
vs.
for (int i = particles.size()-1; i >=0; i--) {
Particle p = particles.get(i);
p.run();
if (p.isDead()) {
particles.remove(i);
}
}
from reader:
This is just a note on the PBox2D example code that came with Processing 2.0b6:
ApplyForceSimpleWind and ApplyForceAttractMouse around line 48
// When the mouse is clicked, add a new Box object
if (random(1) < 0.1) {
Box p = new Box(random(width),10);
boxes.add(p);
}
I guess the comment should read something like "add a new Box object about 10% of the time"
I have been trying to develop an application for timetable generation for universities/schools using Genetic Algorithm. It would be great if you could give a some guidance!!!!!!
Your link for "Learning Cinder tutorial" at
http://www.iamnayr.com/tutorials/learn-cinder-tutorial-part-1/
is now 404.
The-Nature-of-Code-Examples-master\chp08_fractals\Exercise_8_08_09_TreeArrayListLeaves\Leaf.pde
This:
class Leaf {
PVector pos;
Leaf(PVector l) {
loc = l.get();
}
Should be this:
class Leaf {
PVector pos;
Leaf(PVector l) {
pos = l.get();
}
or this:
class Leaf {
PVector loc;
Leaf(PVector l) {
loc = l.get();
}
update examples with andres colubri's fixes
Below is my solution for exercise 2.1, balloons.
//code by Densou
//email: [email protected]
Balloon[] balloons= new Balloon[10];
void setup(){
size(600,400);
smooth();
for(int i=0; i<balloons.length;i++){
balloons[i]= new Balloon(i);
}
}
void draw(){
background(200,40,60);
for(int i=0; i<balloons.length;i++){
balloons[i].render();
balloons[i].update();
balloons[i].checkCollision();
}
}
class Balloon{
//v for velocity
//f for acceleration of flotage
//r for acceleration of resistance
//l for location
//a for total acceleration
//wind force
PVector v, f, l, a, wind;
//w,h:size of this balloon
//stringl:string length
//sw: string swing range
//maxV stands for the max velocity a balloon can reach--
//when gracity and flotage get balance
float w,h, maxV,stringl,sw;
//covenient for array
int array;
//color of each balloon
color c;
Balloon(int array_){
v= new PVector(0,0);
//f= new PVector(0,-0.02);
//f minus 0.01, in case acceleration too small
f= new PVector(0,random(0.02)-1-0.01);
maxV= 2.5;
w=random(15)+30;
h=random(10)+w1.2;
//l= new PVector(width/2,height);
l = new PVector(random(width),random(height/2)+height*.5);
stringl=80.0;
sw=40;
array=array_;
c=color(random(150)+100,random(150)+100,random(150)+100);
//c=color(array%3150,(array+2)%3150,(array+1)%3*150);
}
void update(){
wind= new PVector((noise(1,frameCount/200)-0.5)*0.04,(noise(2,frameCount/200)-0.5)*0.01);
a=PVector.add(f,wind);//use static function here, otherwise a increases.
//println(a.x,a.y);
v.add(a);
l.add(v);
//println(v.y);
v.limit(2.2);
//if(v.y>maxV ){
// v.y=maxV;
//}else if(v.y<-maxV){
// v.y=-maxV;
//}
}
void render(){
pushMatrix();
translate(l.x,l.y+h/2);
//swinging string
stroke(242,192,86);
strokeWeight(1);
noFill();
float t=frameCount/500.0;
bezier(
0,0,
0,noise(1,array,t)*stringl-stringl/2,
noise(2,array,t)*sw-sw/2,noise(3,array,t)stringl0.6,
noise(4,array,t)*sw-sw/2,noise(5,array,t)*stringl
);
//balloon ball
//fill(220,30,100);
fill(c);
noStroke();
//ellipse(l.x,l.y,w,h);
ellipse(0,-h/2,w,h);
popMatrix();
}
void checkCollision(){
if(l.y<h/2){
v.y=v.y*-0.8;
f.y=f.y0.8;
if(v.y<0.02 && -0.02<v.y){
v.y=0;
f.y=0;
l.y=h/2;
}
}
//println(v.y);
if(l.x<w/2 || l.x>width-w/2){
v.x=v.x-0.6;
}
}
}
Update examples to use:
random2D()
setMag()
and other 2.0 PVector functions
I'm a newbie, but maybe import java.util.Iterator;
should be added to the NOC_4_04_SystemofSystems_c example found on Processing 2.0 so it can work out of the box.
I think line 31 should read something like this or the boxes just keep appearing regardless if you click your mouse or not.
// When the mouse is clicked, add a new Box object
if (mousePressed) {
Box p = new Box(mouseX, mouseY);
boxes.add(p);
}
"pull request guidelines? would think a lang specific readme with requirements and dependencies would be a good start."
Hey Daniel, just starting your book, really looking forward to getting into it!
I was just curious to see if you had any plans of creating a set of javascript examples - so that we can follow along using p5.js?
oops, this was me tinkering with the examples and should not have been committed.
I have a question about NOC_6_05_PathFollowingSimple
In the Vehicle class you calculate the target position outside the if (distance > p.radius)
, but actually you apply seek(target)
only if in that condition is true, could you save some calculation by moving the logic for finding the target within the if (distance > p.radius)
block?
Why did you choose to put that logic outside the conditional? Just for debugging reasons or there's something else useful you can do with that?
The example Exercise_6_08_FlowField3DNoise says: "click mouse to generate a new flow field", but this is not implemented in the code. Could be copy pasted from NOC_6_04_Flowfield maybe?
I'm sorry this is surely not the way to go but I'm not sure how to do it otherwise ...
Here's the code for example 1.3 from the book:
/* built with Studio Sketchpad:
// Nature of Code - EXERCISE 1.3 - 3D Bouncing Ball
// Extend the bouncing ball with vectors example into 3D.
// Can you get a sphere to bounce around a box?
// https://forum.processing.org/topic/
// the-nature-of-code-code-solutions-of-the-examples
// studio.processingtogether.com/sp/pad/export/ro.9o1T5z1ghf2s7/latest
final PVector location = new PVector();
final PVector velocity = new PVector(3, 2, 4);
final static int BALL_DIM = 30;
final static int CUBE_DIM = 450, CUBE_RAD = CUBE_DIM >> 1;
int cx, cy;
void setup() {
size(400, 400, P3D);
strokeWeight(2);
cx = width >> 1;
cy = height >> 1;
}
void draw() {
background(-1);
drawCube();
moveSphere();
drawSphere();
}
void drawCube() {
//move cube and ball to center:
translate(cx, cy, -CUBE_DIM);
//rotate cube and ball so it's 3D-ness can be appreciated:
rotateX(-QUARTER_PI);
rotateY(QUARTER_PI);
rotateZ(QUARTER_PI/2);
//draw cube:
noFill();
stroke(0);
box(CUBE_DIM);
}
void moveSphere() {
//move ball:
location.add(velocity);
//detect edges with center at 0,0 and edge minus width of ball:
if ( checkBounce(location.x) ) velocity.x *= -1;
if ( checkBounce(location.y) ) velocity.y *= -1;
if ( checkBounce(location.z) ) velocity.z *= -1;
}
void drawSphere() {
//draw ball, and lights for definition:
translate(location.x, location.y, location.z);
fill(0300);
noStroke();
lights();
sphere(BALL_DIM);
}
static final boolean checkBounce(float coord) {
return coord > CUBE_RAD - BALL_DIM | coord < BALL_DIM - CUBE_RAD;
}
via @craigprotzel
My incorrectly-placed issue - this is from the pre-loaded Processing examples, which I would guess are cloned from here:
In NOC_9_04_Faces_interactiveselection, in the Population class, in the comment above the function getMaxFitness(), the word "fitness" is spelled "fintess".
Trivial, but FWIW.
Hey,
the structure of this repo lets me assume, that it is planned to add ports of the examples in different languages.
I just took care of porting chapter 4 to openFrameworks yesterday and would like to share my findings. So i was wondering, if a PR would be appreciated here.
Doing a bit of research, i found that @firmread and @emmanuelgeoffray have already started rewriting chapter 1, 2, 3 and 6 in openFrameworks. Maybe they would contribute their code too.
best,
/m
I would really like to find the time to make a set of examples for chapter 6 that implement the braitenberg vehicles.
The two sketches at the start of 4.13 Image Textures and Additive Blending do not show.
I think this is because of the missing image file used for the particle effects:
http://natureofcode.com/book/chapter-4-particle-systems/processingjs/chapter04/_4_08_ParticleSystemSmoke/data/texture.png
how horribly wrong is this example given that it does not consider surface area?
Request to have an example where the Box2D elements are drawn as images rather than shapes.
examples doesn't shows cause of that
http://natureofcode.com/book/chapter-4-particle-systems/#413-image-textures-and-additive-blending
array out of bounds exception, need to check into this.
use an ArrayList
The solution to Exercise 3.13/14 is missing, which I find quite important since this is also a nice recap of chapter 2.7.
in The-Nature-of-Code-Examples/chp06_agents/NOC_6_06_PathFollowing/ we get the following error:
The variable "predictpos" does not exist.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.