Nature of Code book: PVector Spring example

I’m working this week on Chapter 3 of my upcoming Nature of Code book. For the most part, if you are looking to connect particles with springs, I recommend the wonderful verlet physics of toxiclibs, and I have some examples for that here. Nevertheless, I am including an elementary implementation of a single “bob” connected to an “anchor” point via a “spring” force. The example implements Hooke’s Law (Spring Force = -k * displacement) with the PVector class, using the Euler integration model of all my other examples. Here it is below.

Source: _03spring.zip

Box2D ContactListener in Processing

view applet, download source

Above is a new Box2D Processing example that demonstrates two key aspects of working with Box2D:

1) Though tempting as it may be, you cannot set the location manually of an object in the Box2D world and expect the physics to continue to work. Box2D doesn’t understand teleportation (which is the equivalent of telling an object to disappear and then reappear at a different pixel). Rather, if you want to move an object manually, you can attach a joint to the object and tug it around. This way, you control its motion and yet it still lives within the world of Box2D physics. The example demonstrates how this is done with a MouseJoint. The object moves according to an perlin noise algorithm (unless the mouse is pressed in which case it follows the mouse).

2) The example also demonstrates how to use Box2D’s ContactListener to know when objects have collided. The circles turn red when they encounter the square.

It’s my intention to eventually add this example with further explanation to the Box2D tutorial.

Box2D and Processing

 

I’m pleased to announce I’ve published a first draft of a tutorial about using Box2D in Processing.

Tutorial: http://www.shiffman.net/teaching/nature/box2d-processing/
Google code repository: http://code.google.com/p/pbox2d/

I’m struggling here to figure out whether I’m (a) creating a Processing Box2D library or (b) simply creating a tutorial and set of examples piggybacking off of JBox2D. For now, I’m doing a little bit of both. The library is just a few helper functions, but the examples require you to dig into actual Box2D code. These examples aren’t nearly as comprehensive as what you’ll find in the JBox2D demos. It’s my goal, however, to make the material accessible and easy to use. Hopefully, with some feedback and more time, I’ll be able to publish a more sophisticated library and thorough suite of example. Who knows, maybe no one will ever need any of my previous Nature of Code tutorials any more!

Next up, I’m planning on creating a few simple examples that use the fantastic and awe-inspiring toxiclibs.

Nature of Code Book Chapter 1 Draft Available

Ok, so I may very well be one of the slowest writers ever, but I am pleased to finally announce that I have completed a draft chapter for what I hope will become my next book: The Nature of Code. Based on my experience getting Learning Processing out into the world I’ve decided to go ahead and experiment with self-publishing. I’m not sure what service I’ll ultimately use or exactly how I’ll distribute the text (most likely as a PDF for sale online as well as print-on-demand physical book) so feel free to write me with suggestions, etc.

Let’s take a moment to go over some of the finer points as to why I am doing this.

Dollars and cents

Learning Processing retails for $49.95 (amazon’s discount is 10%: $44.95). When the publisher sells a copy of the book, I get some money (yay for me!). Based on my first royalty statement, this works out to approximately $3.73 per copy. Sure, I’m not writing books about programming with Processing to get rich, but I did spend a couple years working hard on the project and every little bit counts.

Let’s assume for the moment that I could sell the same exact book via lulu.com. The actual cost for printing the book would be ~$14.00. Ok, so let’s say I choose to sell the book at $25.00 (half the actual current cost.) That’s $11 of profit for every book sold, lulu takes 20%, leaving me with ~$8.80 per book sold. The book costs half as much and I get more than double the revenue! Now, this is just one scenario. I haven’t decided what service to use, how much of a mark-up is appropriate, etc. But you get the idea. There’s no reason a no color, no frills, beginner programming text needs to be $50.00.

Release early, Release often

As an author, it’s just nice to have a lot of flexibility with the process. With self-publishing, I can do things like release early drafts of PDFs online for feedback (see below). This is not something I could have easily done with a traditional publishing house. Instead of spending months or years writing a book before anyone sees anything, the idea is that I can just put stuff out there (for cheap) as I type and then iterate. And there are no limits of how I choose to distribute the book (excerpts published as tutorials on Processing.org? Free on my site? PDF for a million dollars? Audio book? It’s all fair game.).

Once the book is done, I can easily continue to make changes and update. Now, Processing has a fairly stable API, one that is not going to undergo massive changes anytime soon. And sure, how gravity works, the formula for the mandelbrot set, these aren’t concepts that are going to change that often. Nevertheless, anytime you write a technical book, technology changes faster than you can write, and no matter how careful you are, there’s no way to avoid making a serious amount of mistakes. With self-publishing and print-on-demand, I don’t have to wait (possibly years) for a print run to finish selling in order to make changes. I could make them daily if I wanted to. And that Chapter on PHP that I realized I really should have included in Learning Processing, well, I could just add it whenever I so choose.

Downfalls

There are certainly some pitfalls to self-publishing. One major issue, of course, is deadlines. Without a publisher I’ve got very little pushing me forward other than myself. In fact, getting this first chapter done took me twice as long as I intended. And other projects are getting in the way, I’m not sure how fast I will actually get to chapter #2.

The other main issue is distribution. I don’t care if I don’t get my book in Barnes and Noble, I mean who is really buying Processing books at Barnes and Noble?! I do need Amazon.com, but looks like there are plenty of print-on-demand options that can be distributed via Amazon. The major question for me is university bookstores. I don’t have any numbers, but it does seem to me that Learning Processing gets stocked in a lot of school bookstores because it is being used as a text for classes. So this is something I need to figure out, how can I get a self-published book to stores.

Oh yeah, an index. The publisher made an index for me. There’s got to be a way I don’t need them for that, though.

In the end, I could be wrong. This could be a failed experiment. Maybe no one will buy it, maybe I won’t finish it. The nice thing, however, is that if I’m really headed in the wrong direction here, I can always change my mind and start sending out proposals to a publisher. But the other way around, going from a publisher to self-published, well, that wouldn’t be so easy.

So, if you’re interested in checking out what I’ve started so far, for now (subject to change), you can purchase the PDF on lulu.com. I’m selling draft chapters for small amounts with the idea that I could raise a little bit of money to pay for design, typesetting, technical editing, etc. once I’ve got a more finished draft.

http://stores.lulu.com/dshiffman

UPDATE: I’m also looking for a service that would let users buy early drafts of a book and then upgrade to the final version at a discount or for free. Suggestions for how to do this welcome.

Nature of Code Book

This semester, I’ve started working on expanding my nature of code tutorials into a book. My plan is to self-publish (looking into a few options) and have drafts available for download / purchase as early as this summer. I’ll also be publishing excerpts from the book as tutorials on processing.org (the first will be a PVector tutorial) and on this site as well.

sign up for e-mail updates about the book

Here is a PDF of the draft table of contents for those who are curious. Feedback is welcome!

More Steering Examples

I’ve added three new steering examples (based, of course, off of Craig Reynolds’ Steering Behaviors for Autonomous Characters) to the nature of code tutorials. Ultimately, it’s my goal to build out all of Reynolds’ algorithms into a Processing library (much like Open Steer), so stay tuned. . .

   

Path Following
Flow Field
Crowd Path Following

I hope to have a new tutorial about the use of the PVector dot product in the path following examples posted in the next day or two as well.

Art collides with Code

Two announcements.

I am incredibly honored and excited to be participating in the upcoming Art and Code symposium, organized by Golan Levin at Carnegie Mellon University.


Visit Art and Code

In addition, I’m finally working on a new tutorial page for the Nature of Code site. The tutorial will be about resolving collisions and I’m using the excellent book Mathematics and Physics for Programmers as a basis. I’ve posted the very first example, a simple implementation (using PVector) of two circles (equal mass) colliding. Note the collision is an idealized elastic collision. And the example isn’t terribly sophisticated and needs some improvements in order to work with more than two objects.