Translating Car On Terrain project from Phaser.io into Processing

Today Simon spent hours translating this Car On Terrain project from Phaser.io (where it appears in JavaScript) into Processing (Java). He loved doing it in a form of a lesson for me, while I was filming him and asking questions about loops, arrays, fixtures, shapes and bodies (and there are many things I don’t understand). Simon also spoke about “the three most important properties: density, friction and restitution”. The project involved a lot of Physics, using many Box2D sub-libraries and translating between pixels and mm.

In the end, he got tired of writing all the coordinates for the terrain vertices, but he did get quite far.

 

 

 

 

 

Applying Box2D to translate from pixels into mm:

CarOnTerrain translating from pixels into mm

Modulus Counting in Processing

Simon wrote a modulus counting program in Processing after we were discussing why

1 % 2 = 1 and why 2 % 4 = 2. He basically told me he simply knew 1 % 60 equals 1, but he didn’t know why.  We realized later that the strange result comes from the fact that if a number (the divisor) fits into another number (the numerator) zero times then, in modulus counting, that numerator becomes the excess.

 

DSC_0407

Infinite Line in Processing. Simon’s own code.

A beautiful project in Processing (Java), Simon’s own code, resembling  an El Lissitzky painting that you can control and change with the mouse (without Simon knowing El Lissitzky). Resulted from thinking about and playing with infinite line and line segments. Simon used the following formula: slope times x plus yIntercept.

Infinite Speceship 13 Jun 2017 2

Infinite Speceship 13 Jun 2017 1

 

 

 

DSC_0410

Error with Genetic Algorithm. What is wrong?

Simon was almost done translating Smart Rockets example no. 2 (Smart Rockets Superbasic) from Daniel Shiffman’s The Nature of Code, from Processing (Java) into JavaScript in Cloud9, when he got an error using genetic algorithm: the dna seems to be undefined while Simon did define mom and dad dna.

This is Simon’s translation online in Cloud9: https://ide.c9.io/simontiger/smart-rockets#openfile-README.md

This is when he first discovered the bug and tried different solutions:

And this is the same project before he introduced the genetic algorithm:

In the next video Simon boasts he found two errors in his code and hopes that the problem would be solved, but alas, the rockets still vanish from the canvas after a few seconds:

Simon is officially stuck here.

On the positive side, this project did get us to read more about the actual human DNA and the way it works.

 

Thoughts on Fidget Spinner Simulation

DSC_0420-1
In his recent Live Stream, Daniel Shiffman announced he would make a fidget spinner simulation in the near future. Inspired by this idea, Simon has jotted down some code for the fidget spinner project. The code and part of the code description both come from The Nature of Code, Example 2.6. Simon explains that the code below has a non-static radius, because he is dealing with x and y coordinates and not angles (i.e. not spinning but moving to where the mouse is). He says he is going to use that code as a foundation. The second step will be to rely on angles instead of x and y coordinates. The third step will be to rotate have the fidget spinner keep rotating and to apply a friction force to make it stop eventually.
Simon recorded this video, where he ponders on the possibilities for the fidget spinner code:
Simon wrote the rest of this post:
Description:
This is the code for clicking and dragging an object around the screen.
To do that, you need the following data:
  • mass (radius of the object)
  • location (position of the object)
  • dragging (Is the object being dragged?)
  • rollover (Is the mouse over the object?)
  • dragOffset (offset for when the object is clicked on)

And the following functionality:

  • clicked (checks if the object is hovered over and sets dragging to true)
  • hover (checks if the object is hovered over and sets rollover to true)
  • stopDragging (sets dragging to false)
  • drag (moves the object around the screen according to the mouse)
Code and Pseudocode:
Fidget Spinner Simulation
 – Prepare mouse interaction code
  – Data (mass, location, dragging, rollover, dragOffset)
  – Functionality (clicked, hover, stopDragging, drag)
  – Code (Mover)
   – Variables
float mass;
PVector location;
boolean dragging = false;
boolean rollover = false;
PVector dragOffset;
– Functions
void clicked(int mx, int my) {
  float d = dist(mx,my,location.x,location.y);
  if (d < mass) {
    dragging = true;
    dragOffset.x = location.x-mx;
    dragOffset.y = location.y-my;
  }
}

void hover(int mx, int my) {
  float d = dist(mx,my,location.x,location.y);
  if (d < mass) {
    rollover = true;
  }
  else {
    rollover = false;
  }
}

void stopDragging() {
  dragging = false;
}



void drag() {
  if (dragging) {
    location.x = mouseX + dragOffset.x;
    location.y = mouseY + dragOffset.y;
  }
}
  – Code (Main)
   – Variables
Mover m;
   – Functions
void draw() {
  m.drag();
  m.hover(mouseX,mouseY);
}

void mousePressed() {
  m.clicked(mouseX,mouseY);
}

void mouseReleased() {
  m.stopDragging();
}

Preparing to give Dad some Processing Lessons

Simon has been giving Dad some lessons in Processing (Java), following the chapters in Daniel Shiffman’s book The Nature of Code. The have already finished Chapetr 1 (Vectors) and moved on to Chapetr 2 (Forces). I hadn’t realized until recently however that Simon doesn’t simply download all the examples for the lessons from the Processing website but manually writes them over from the html version of the book instead (to style them the way they are in the book, he said). It’s a amazing how organized he is at doing that and saving his files in separate folders.

Testing his examples Simon actually found a mistake in the book and Daniel Shiffman asked him to create an issue about it on the book’s GitHub’s page: https://github.com/shiffman/The-Nature-of-Code/issues/133

DSC_0120

Neural Networks Coding Challenge

Simon completes the Neural Networks Coding Challenge (in Processing, Java) that he had followed in the Intelligence and Learning Livestream last Friday. In the videos below he also talks about what neural networks are and tries to add a line object (something he had suggested in the live chat).

 

Autonomous Agents. Steering Behaviors. Flocking.

Simon’s full-screen take on Flocking, based on Chapter 6 from The Nature of Code (Autonomous Agents). The code comes from Daniel Shiffman’s tutorial Combining Behaviors: Flocking.

In Simon’s words, Autonomous Agents need to have action/ selection, steering (for example, for Flocking you need Separation, Alignment, Cohesion programmed using the neighbours, desired and current velocity) and locomotion (position, velocity, acceleration, “own vector stuff” or a physics library).