Collision Detection Functions for Processing Released!

UPDATE: these functions have been updated and moved to GitHub
Released today, version 0.9 of my 2d collision detection functions for Processing!
While some tools already exist, like the excellent Box2D, the source code is not easy to understand and the implementation is a bit complex. Other examples (equally great), like the line-line collision in the toxiclibs collection, use vector math which isn’t great for us “creative programmers” (ie: those like me who got bad grades in high school math).
Instead, these tools can be used as simple, one-line commands to determine whether two objects have collided. Designed for simple games and interactive systems, they are intended to be building blocks for larger projects.
Functions include:
- Point/Point
- Point/Rect
- Point/Ball
- Rect/Rect
- Ball/Ball
- Ball/Rect
- Point/Line
- Line/Line
- Ball/Line
Maxwell’s Demon Game


Screenshots from the games I made yesterday for games++, the 12-hour game jam we hosted at Drift Station.
Inspired by Maxwell’s Demon, a thought-experiment with a box, separated by a wall with a hole in it and one side filled with gas. The experiment asks the question of why does the gas “know” to go through the other hole and fill the second chamber so that it is dispersed evenly?
Written in Processing, the source code is here.
Brownian Triangles – Animation
One more for today (best seen in HD on Vimeo)…
Brownian Triangles – Zoomed In

Another image of triangles in 3d space following Brownian motion, this time zoomed way in.
Brownian Triangles
Ride The Lightning – Sorted
Unix Commands Within Processing
I keep finding myself needing to be able to run Unix commands within Processing sketches (maybe too much time spent writing Bash scripts lately), but no one seems to have a good solution. Today I was able to get the Java exec() command to work within Processing… hooray!
EDIT: want to do something more complex? You can run a Bash script too!
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
/* * RUN UNIX COMMANDS IN PROCESSING * Jeff Thompson * Adapted and annotated from code by Wharfie * http://www.computing.net/answers/unix/run-unix-command-thr-java-program-/5887.html * July 2011 * * Runs Unix commands from within Processing. This can be *super* helpful for * doing complex operations on folders such as copying or compressing multiple * files. A simple command can make quick work of what would otherwise be a * cumbersome task in Processing and will likely be much quicker than any Java * implementation of the same process. * * You will likely need to specify the full path where you want to work, unless your * location is relative to your sketch. For example: /Users/jeffthompson/Desktop * * Suggestions to try: * whoami prints username currently logged on * ls this lists all files in the particular location * wc -w filename.extension this counts words in a particular file * cp sourcefile.ext destfile.txt makes a copy of a file to a new one * ./yourBashScript.sh run a Bash script * * For more ideas, look at the excellent SS64 site: http://ss64.com/bash * * www.jeffreythompson.org */ void setup() { // what command to run String commandToRun = "whoami"; // String commandToRun = "ls"; // String commandToRun = "wc -w sourcefile.extension"; // String commandToRun = "cp sourcefile.extension destinationfile.extension"; // String commandToRun = "./yourBashScript.sh" File workingDir = new File(sketchPath("")); // where to do it - should be full path String returnedValues; // value to return any results // give us some info: println("Running command: " + commandToRun); println("Location: " + workingDir); println("---------------------------------------------\n"); // run the command! try { // complicated! basically, we have to load the exec command within Java's Runtime // exec asks for 1. command to run, 2. null which essentially tells Processing to // inherit the environment settings from the current setup (I am a bit confused on // this so it seems best to leave it), and 3. location to work (full path is best) Process p = Runtime.getRuntime().exec(commandToRun, null, workingDir); // variable to check if we've received confirmation of the command int i = p.waitFor(); // if we have an output, print to screen if (i == 0) { // BufferedReader used to get values back from the command BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream())); // read the output from the command while ( (returnedValues = stdInput.readLine ()) != null) { println(returnedValues); } } // if there are any error messages but we can still get an output, they print here else { BufferedReader stdErr = new BufferedReader(new InputStreamReader(p.getErrorStream())); // if something is returned (ie: not null) print the result while ( (returnedValues = stdErr.readLine ()) != null) { println(returnedValues); } } } // if there is an error, let us know catch (Exception e) { println("Error running command!"); println(e); } // when done running command, quit println("\n---------------------------------------------"); println("DONE!"); exit(); } |
Sample-Tracing

The above image is the result of a Processing sketch that takes an audio file, sorts its sample values, and visualizes the current and original position of each sample. At 44,100 samples per second, the above 10-second audio file has 441,000 arcs. Height and line color are proportionate to the distance the line travels, otherwise the image would basically be a big block.
Still in refinement, source code forthcoming…
Sample-Tracing Glitch

Working on a Processing project that visualizes sorted sample values, this lovely mistake happened…






