Nature of Code Midterm: Genetic Crossing with Verlet Physics

07 Mar

For my Nature of Code midterm, I added a physics world to my genetic crossing project from last semester.

A short video, sped up quite a bit because my MacBook Air + QuickTime screencap choked on it:

YouTube Preview Image

Here’s some past posts on the project:

Professor Shiffman‘s notes from his Nature of Code book were invaluable in understanding vector- and particle- based systems, so by the time we got to Box2D and ToxicLibs, understanding the underlying framework needed to make a world that reacts to physical forces was far easier.

My genetic crossing project, which allowed for people to reproduce and carry characteristics from their parents and their religious and national environments, was a perfect project for last semester but also a perfect transition project for a physics system, because I was trying to study the natural flow and connection between many different objects with differing characteristics.

I chose to use ToxicLibs’ VerletPhysics2D library because all I had to do was have my Person, Nation, and Religion objects to inherit the VerletParticle2D class, and then be added as particles with connecting springs in order to have them react to each other.

class Person extends VerletParticle2D {
  Hashtable trait = new Hashtable();
  String namePerson, gender;
  int parent1, parent2, uniqueID, pWeight, pHeight, age, mbti, lastBaby,
  nationality, religion, children,strength,intelligence,wisdom,charisma,stamina,wit,
  appearance,money,gracefulness,stress,health,luck,talentMath,talentArt,talentSports, happiness, employed;
  int[] colorBaseArray = new int[3]; // need base colors for each person
  float[][] traitPosArray = new float[numCharacteristics][2]; // save x,y for all characteristics
  float[][] traitPosArray_orig = new float[numCharacteristics][2]; // original copy of coords
  boolean alive;

  // constructor
  Person(int _uniqueID, int _mbti, int _stress, int _health, int _gracefulness, int _luck, int _talentMath, int _talentArt, int _talentSports, int _strength,
  int _intelligence, int _wisdom, int _charisma, int _stamina, int _wit, int _humor, int _pWeight, int _pHeight,
  int _education, int _age, int _creativity, int _responsibility, int _discipline, int _honesty, int _religiosity,
  int _entrepreneurialism, int _appearance, int _money, String _namePerson, String _gender, int _parent1,
  int _parent2, int _nationality, int lastBaby, int _religion, int _children, int _employed, float x, float y) {
    super(x, y);

Two things I found out pretty quickly.  First, I made this project with arrays and a hashtable but had I known better, I would have used ArrayList and no hashtables (just variables with integer values.  The other?  I had to rewrite my class xPos, yPos variables as x,y and include them as direct variables instead of references to new variables, in order for the objects to be recognized in the physics world.  Basically, my objects would drop like a stone in a pond because they were not connected to the system but still were “particles” in a world with gravity.

I still have a problem with bizarre array counting for adding the large amount of springs that connect every person, nation, and religion to each other, and more added as each new person is born into the world.  I’m probably wasting tons of memory and overwriting certain springs because I didn’t make entirely unique arrays.

But the simulation is now operating within a 2D world and now I can go through the process of tweaking the relationships between the objects to more clearly match up with the characteristics/heredity/uniqueness that I coded into the simulation last semester.  God is located in the center now, instead of in the bottom right, so he is a fixture within the world, with the nations and religions along the periphery of the map (no meaning behind that!) to space out the particles which are tethered by their springs.

Relevant code:


  // #PeopleChange init all person objects & nations
  for (int i=0;i<numPeople;i++) {

  for (int k=0;k<numPeople;k++) {
    springArray[k] = new VerletConstrainedSpring2D(god, person[k], godRL, godGravity);

  int z=0;
  for (int k=0;k<numPeople-1;k++) {
    if (z < numPeople) {
      for (int l=0;l<numPeople;l++) {
        newPersonSpringArray[z] = new VerletConstrainedSpring2D(person[l], person[k], int(random(250, 650)), random(0.001, 0.003));

Code is on Github.  I recommend running this in Processing since I have it set to something like 1200×700 pixels.