Preface
P.1 What is this book?
At ITP, I teach a course entitled Introduction to Computational Media. In this course, the students learn the basics of programming (variables, conditionals, loops, objects, arrays) as well as a survey of applications related to making interactive projects (images, pixels, computer vision, networking, data, 3D). The course mostly follows the material found in my intro book Learning Processing; in many ways, The Nature of Code serves as a follow-up. Once you’ve learned the basics and seen an array of applications, your next step might be to delve deeply into a particular area. For example, you could focus on computer vision (and read a book like Greg Borenstein’s Making Things See). In the most basic sense, this book is one possible next step in a world of many. It picks up exactly where Learning Processing leaves off, demonstrating more advanced programming techniques with Processing that focus on algorithms and simulation.
The goal of this book is simple. We want to take a look at something that naturally occurs in our physical world, then determine how we can write code to simulate that occurrence.
So then what is this book exactly? Is it a science book? The answer is a resounding no. True, we might examine topics that come from physics or biology, but it won’t be our job to investigate these topics with a particularly high level of academic rigor. Instead, we’re going to glance at scientific concepts and grab the parts that we need in the service of building a particular software example.
Is this an art or design book? I would also say no; after all, we are going to focus on algorithms and their affiliated programming techniques. Sure, the results will all be visual in nature (manifested as animated Processing sketches), but they will exist more as demonstrations of the algorithms and programming techniques themselves, drawn only with simple shapes and grayscale. It is my hope, however, that designers and artists can incorporate all of the material here into their practice to make new, engaging work.
In the end, if this book is anything, it is really just a good old-fashioned programming book. While a scientific topic may seed a chapter (Newtonian physics, cellular growth, evolution) or the results might inspire an artistic project, the content itself will always boil down to the code implementation, with a particular focus on object-oriented programming.
P.2 A word about Processing
I am using Processing in this book for a number of reasons. For one, it’s the language and environment with which I am most comfortable, and it’s what I enjoy using for my personal work. Two, it’s free, open-source, and well suited to beginners. There is an active, energetic community of people who program with Processing; for many, it’s the first programming language they’ve learned. In this sense, I hope that I can reach a wide audience and demonstrate the concepts in a friendly manner by using Processing.
All that said, there is nothing that ties what we are doing in this book strictly to Processing. This book could have been written using ActionScript, JavaScript, Java (without Processing), or any number of other open-source “creative coding” environments like openFrameworks, Cinder, or the newly released pocode. It is my hope that after I’ve completed this book, I’ll be able to release versions of the examples that run in other environments. If anyone is interested in helping to port the examples, please feel free to contact me (daniel@shiffman.net).
All of the examples in this book have been tested with Processing 2.0b6, but for the most part, they should also work with earlier versions of Processing. I’ll be keeping them up-to-date with whatever the latest version is. The most recent code can always be found on GitHub.
P.3 What do you need to know?
The prerequisite for understanding the material in this book could be stated as: “one semester of programming instruction with Processing (including familiarity with object-oriented programming).” That said, there’s no reason why you couldn’t read this book having learned programming using a different language or development environment. The key here is that you have experience with programming.
If you’ve never written any code before, you are going to struggle, because this book assumes knowledge of all the basics. I would suggest picking up an introductory book on Processing, a number of which are listed on the Processing website.
If you are an experienced programmer, but haven’t worked with Processing, you can probably pick it up by downloading Processing, poking through the examples, and reading through the Getting Started page.
I should also point out that experience with object-oriented programming is crucial. We’ll review some of the basics in the book’s introduction, but I would suggest reading the Processing tutorial on objects first.
P.4 What are you using to read this book?
Are you reading this book on a Kindle? Printed paper? On your laptop in PDF form? On a tablet showing an animated HTML5 version? Are you strapped to a chair, absorbing the content directly into your brain via a series of electrodes, tubes, and cartridges?
The book you are reading right now was generated with the Magic Book project. The Magic Book is an open-source framework for self-publishing developed at ITP. The idea here is that you only need to write the book once as a simple text file. Once you’ve written your content, you press a magic button, and out comes your book in a variety of formats—PDF, HTML5, printed hardcopy, Kindle, etc. Everything is designed and styled using CSS. As of the first release, the only versions available will be digital PDF, printed hardcopy, and HTML5 (which will include animated versions of the examples using Processing.js). Hopefully over the course of the next year, the book will be available in additional formats. If you’d like to help with this, please contact me (daniel@shiffman.net).
P.5 The “story” of this book
If you glance over the book’s table of contents, you’ll notice there are ten chapters, each one covering a different topic. And in one sense, this book is just that—a survey of ten concepts and associated code examples. Nevertheless, in putting together the material, I had always imagined something of a linear narrative. Before you begin reading the chapters, I’d like to walk you through this story.
Part I: Inanimate objects
A soccer ball lies in the grass. A kick launches it into the air. Gravity pulls it back down. A heavy gust of wind keeps it afloat a moment longer until it falls and bounces off the head of a jumping player. The soccer ball is not alive; it makes no choices as to how it will move throughout the world. Rather, it is an inanimate object waiting to be pushed and pulled by the forces of its environment.
How would we model a soccer ball moving in Processing? If you’ve ever programmed a circle moving across a window, then you’ve probably written the following line of code.
x = x + 1;
You draw some shape at location x. With each frame of animation, you increment the value of x, redraw the shape and voila—the illusion of motion! Maybe you took it a step or two further, and included a y location, as well as variables for speed along the x and y axes.
x = x + xspeed; y = y + yspeed;
Part I of this story will take us one step further. We’re going to take these variables xspeed and yspeed and learn how together they form a vector (Chapter 1), the building block of motion. We won’t get any new functionality out of this, but it will build a solid foundation for the rest of the book.
Once we know a little something about vectors, we’re going to quickly realize that a force (Chapter 2) is a vector. Kick a soccer ball and you are applying a force. What does a force cause an object to do? According to Isaac Newton, force equals mass times acceleration. That force causes an object to accelerate. Modeling forces will allow us to create systems with dynamic motion where objects move according to a variety of rules.
Now, that soccer ball to which you applied a force might have also been spinning. If an object moves according to its acceleration, it can spin according to its angular acceleration (Chapter 3). Understanding the basics of angles and trigonometry will allow us to model rotating objects as well as grasp the principles behind oscillating motion, like a pendulum swinging or a spring bouncing.
Once we’ve tackled the basics of motion and forces for an individual inanimate object, we’ll learn how to make thousands upon thousands of those objects and manage them in a single system called a particle system (Chapter 4). Particle systems will allow us to look at some advanced features of object-oriented programming, namely inheritance and polymorphism.
In Chapters 1 through 4, all of the examples will be written from “scratch”—meaning the code for the algorithms driving the motion of the objects will be written directly in Processing. We’re certainly not the first programmers ever to consider the idea of simulating physics in animation, so next we’ll examine how physics libraries (Chapter 5) can be used to model more advanced and sophisticated behaviors. We’ll look at Box2D and toxiclibs' Verlet Physics package.
Part II: It’s alive!
What does it mean to model life? Not an easy question to answer, but we can begin by building objects that have an ability to perceive their environment. Let’s think about this for a moment. A block that falls off a table moves according to forces, as does a dolphin swimming through the water. But there is a key difference. The block cannot decide to leap off that table. The dolphin can decide to leap out of the water. The dolphin can have dreams and desires. It can feel hunger or fear, and those feelings can inform its movements. By examining techniques behind modeling autonomous agents (Chapter 6), we will breathe life into our inanimate objects, allowing them to make decisions about their movements according to their understanding of their environment.
Through combining the concept of autonomous agents with what we learned about modeling systems in Chapter 4, we’ll look at models of group behavior that exhibit the properties of complexity. A complex system is typically defined as a system that is “more than the sum of its parts.” While the individual elements of the system may be incredibly simple and easily understood, the behavior of the system as a whole can be highly complex, intelligent, and difficult to predict. This will lead us away from thinking purely about modeling motion and into the realm of rule-based systems. What can we model with cellular automata (Chapter 7), a system of cells living on a grid? What types of patterns can we generate with fractals (Chapter 8), the geometry of nature?
Part III: Intelligence
We made things move. Then we gave those things hopes and dreams and fears, along with rules to live by. The last step in this book will be to make our creations even smarter. Can we apply the biological process of evolution to computational systems (Chapter 9) in order to evolve our objects? Taking inspiration from the human brain, can we program an artificial neural network (Chapter 10) that can learn from its mistakes and allow our objects to adapt to their environment?
P.6 This book as a syllabus
While the content in this book certainly makes for an intense and highly compressed semester, I have designed it to fit into a fourteen-week course. Nevertheless, it’s worth mentioning that I find that the book chapters sometimes work better expanded across multiple weeks. For example, the syllabus for my course generally works out as follows:
- Week 1
-
Introduction and Vectors (Chapter 1)
- Week 2
-
Forces (Chapter 2)
- Week 3
-
Oscillations (Chapter 3)
- Week 4
-
Particle Systems (Chapter 4)
- Week 5
-
Physics Libraries Part I (Chapter 5)
- Week 6
-
Physics Libraries Part II & Steering (Chapters 5-6)
- Week 7
-
Present midterm projects about motion
- Week 8
-
Complex Systems: Flocking and 1D Cellular Automata (Chapters 6-7)
- Week 9
-
Complex Systems: 2D Cellular Automata and Fractals (Chapters 7-8)
- Week 10
-
Genetic Algorithms (Chapter 9)
- Week 11
-
Neural Networks (Chapter 10)
- Weeks 12-13
-
Final project workshop
- Week 14
-
Final project presentation
If you are considering using this text for a course or workshop, please feel free to contact me. I hope to eventually release a companion set of videos and slide presentations as supplementary educational materials.
P.7 The Ecosystem Project
As much as I’d like to pretend you could learn everything by curling up in a comfy chair and reading some prose about programming, to learn programming, you’re really going to have to do some programming. You might find it helpful to keep in mind a project idea (or two) to develop as a set of exercises while going from chapter to chapter. In fact, when teaching the Nature of Code course at ITP, I have often found that students enjoy building a single project, step by step, week by week, over the course of a semester.
At the end of each chapter, you’ll find a series of exercises for one such project—exercises that build on each other, one topic at a time. Consider the following scenario. You’ve been asked by a science museum to develop the software for a new exhibit—The Digital Ecosystem, a world of animated, procedural creatures that live on a projection screen for visitors to enjoy as they enter the museum. I don’t mean to suggest that this is a particularly innovative or creative concept. Rather, we’ll use this example project idea as a literal representation of the content in the book, demonstrating how the elements fit together in a single software project. I encourage you to develop your own idea, one that is more abstract and creative in its thinking.
P.8 Where do I find the code online and submit feedback?
For all things book-related, please visit the Nature of Code website. The raw source text of the book and all of the illustrations are on GitHub. Please leave feedback and submit corrections using GitHub issues.
The source code for all of the examples (and exercises) is also available on GitHub. The chapters themselves include code snippets in-line with the text. However, I want to mention that in many cases, I have shortened or simplified the code snippets in order to illustrate a specific point. In all cases, the full code with comments can be found via GitHub.
If you have questions about the code itself, I would suggest posting them on the Processing forum.
Table of Contents
- Welcome
- Dedication
- Acknowledgments
- Preface
- P.1 What is this book?
- P.2 A word about Processing
- P.3 What do you need to know?
- P.4 What are you using to read this book?
- P.5 The “story” of this book
- P.6 This book as a syllabus
- P.7 The Ecosystem Project
- P.8 Where do I find the code online and submit feedback?
- Introduction
- I.1 Random Walks
- I.2 The Random Walker Class
- I.3 Probability and Non-Uniform Distributions
- I.4 A Normal Distribution of Random Numbers
- I.5 A Custom Distribution of Random Numbers
- I.6 Perlin Noise (A Smoother Approach)
- I.7 Onward
- Chapter 1. Vectors
- 1.1 Vectors, You Complete Me
- 1.2 Vectors for Processing Programmers
- 1.3 Vector Addition
- 1.4 More Vector Math
- 1.5 Vector Magnitude
- 1.6 Normalizing Vectors
- 1.7 Vector Motion: Velocity
- 1.8 Vector Motion: Acceleration
- 1.9 Static vs. Non-Static Functions
- 1.10 Interactivity with Acceleration
- Chapter 2. Forces
- 2.1 Forces and Newton’s Laws of Motion
- 2.2 Forces and Processing—Newton’s Second Law as a Function
- 2.3 Force Accumulation
- 2.4 Dealing with Mass
- 2.5 Creating Forces
- 2.6 Gravity on Earth and Modeling a Force
- 2.7 Friction
- 2.8 Air and Fluid Resistance
- 2.9 Gravitational Attraction
- 2.10 Everything Attracts (or Repels) Everything
- Chapter 3. Oscillation
- 3.1 Angles
- 3.2 Angular Motion
- 3.3 Trigonometry
- 3.4 Pointing in the Direction of Movement
- 3.5 Polar vs. Cartesian Coordinates
- 3.6 Oscillation Amplitude and Period
- 3.7 Oscillation with Angular Velocity
- 3.8 Waves
- 3.9 Trigonometry and Forces: The Pendulum
- 3.10 Spring Forces
- Chapter 4. Particle Systems
- 4.1 Why We Need Particle Systems
- 4.2 A Single Particle
- 4.3 The ArrayList
- 4.4 The Particle System Class
- 4.5 A System of Systems
- 4.6 Inheritance and Polymorphism: An Introduction
- 4.7 Inheritance Basics
- 4.8 Particles with Inheritance
- 4.9 Polymorphism Basics
- 4.10 Particle Systems with Polymorphism
- 4.11 Particle Systems with Forces
- 4.12 Particle Systems with Repellers
- 4.13 Image Textures and Additive Blending
- Chapter 5. Physics Libraries
- 5.1 What Is Box2D and When Is It Useful?
- 5.2 Getting Box2D in Processing
- 5.3 Box2D Basics
- 5.4 Living in a Box2D World
- 5.5 Building a Box2D Body
- 5.6 Three’s Company: Bodies and Shapes and Fixtures
- 5.7 Box2D and Processing: Reunited and It Feels So Good
- 5.8 Fixed Box2D Objects
- 5.9 A Curvy Boundary
- 5.10 Complex Forms
- 5.11 Feeling Attached—Box2D Joints
- 5.12 Bringing It All Back Home to Forces
- 5.13 Collision Events
- 5.14 A Brief Interlude—Integration Methods
- 5.15 Verlet Physics with toxiclibs
- 5.16 Particles and Springs in toxiclibs
- 5.17 Putting It All Together: A Simple Interactive Spring
- 5.18 Connected Systems, Part I: String
- 5.19 Connected Systems, Part II: Force-Directed Graph
- 5.20 Attraction and Repulsion Behaviors
- Chapter 6. Autonomous Agents
- 6.1 Forces from Within
- 6.2 Vehicles and Steering
- 6.3 The Steering Force
- 6.4 Arriving Behavior
- 6.5 Your Own Desires: Desired Velocity
- 6.6 Flow Fields
- 6.7 The Dot Product
- 6.8 Path Following
- 6.9 Path Following with Multiple Segments
- 6.10 Complex Systems
- 6.11 Group Behaviors (or: Let’s not run into each other)
- 6.12 Combinations
- 6.13 Flocking
- 6.14 Algorithmic Efficiency (or: Why does my $@(*%! run so slowly?)
- 6.15 A Few Last Notes: Optimization Tricks
- Chapter 7. Cellular Automata
- 7.1 What Is a Cellular Automaton?
- 7.2 Elementary Cellular Automata
- 7.3 How to Program an Elementary CA
- 7.4 Drawing an Elementary CA
- 7.5 Wolfram Classification
- 7.6 The Game of Life
- 7.7 Programming the Game of Life
- 7.8 Object-Oriented Cells
- 7.9 Variations of Traditional CA
- Chapter 8. Fractals
- 8.1 What Is a Fractal?
- 8.2 Recursion
- 8.3 The Cantor Set with a Recursive Function
- 8.4 The Koch Curve and the ArrayList Technique
- 8.5 Trees
- 8.6 L-systems
- Chapter 9. The Evolution of Code
- 9.1 Genetic Algorithms: Inspired by Actual Events
- 9.2 Why Use Genetic Algorithms?
- 9.3 Darwinian Natural Selection
- 9.4 The Genetic Algorithm, Part I: Creating a Population
- 9.5 The Genetic Algorithm, Part II: Selection
- 9.6 The Genetic Algorithm, Part III: Reproduction
- 9.7 Code for Creating the Population
- 9.8 Genetic Algorithms: Putting It All Together
- 9.9 Genetic Algorithms: Make Them Your Own
- 9.10 Evolving Forces: Smart Rockets
- 9.11 Smart Rockets: Putting It All Together
- 9.12 Interactive Selection
- 9.13 Ecosystem Simulation
- Chapter 10. Neural Networks
- 10.1 Artificial Neural Networks: Introduction and Application
- 10.2 The Perceptron
- 10.3 Simple Pattern Recognition Using a Perceptron
- 10.4 Coding the Perceptron
- 10.5 A Steering Perceptron
- 10.6 It’s a “Network,” Remember?
- 10.7 Neural Network Diagrams
- 10.8 Animating Feed Forward
- Further Reading
- Index
Comments
Post a Comment