# 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:

# Box2D into Codea, Java into Lua

Several recent translations of Box2D examples (from Daniel Shiffman’s book The Nature of Code) into Codea, that is Processing (Java) into Lua. Both Box2D and Codea are physics libraries.

Surface:

Revolute Joint (Windmill):

Car:

# Plinko game translated into Processing (Java) and Box2D

Simon translated Daniel Shiffman’s Plinko game coding challenge from JavaScript and Matter.JS into Processing (Java) and Box2D:

Simon adds that he didn’t actually do part 4 of the coding challenge, because part 3 is recorded after part 4.

# Box2D, Matter.JS and ToxicLibs

One more thing I’d like to share from this weekend is Simon’s lecture comparing three Physics libraries: Box2D, Matter.JS and ToxicLibs. He partially borrowed the lecture from Daniel Shiffman, but added extra info and even rated the libraries:

And here are two projects involving translating from library examples. The Bridge (http://brm.io/matter-js/demo/#bridge) from Matter.JS into Box2D:

And from Box2D into JavaScript:

# Simple Physics. Translating from Lua (the language in the Codea app) into Java

Simon came up with an idea to translate a physics visualization from the Codea app (using a language called Lua) into Processing (Java) by applying Box2D. The first two videos below show him working on that project.

As he got stuck later, Simon decided to do the same project in p5.js instead, applying Matter.js:

This turned out to be quite difficult as well: he got stuck while trying to calculate the exact coordinates for all the shapes involved in the visualization (it was supposed to resemble bricks falling down and rolling down two slopes). So what Simon did as the next step in this “exercise” was switch back to Processing and create a grid with numbered coordinates. When I asked him, why he didn’t make the same grid in p5, he answered that he just loved Processing so much. Eventually though, this grew into a different project:

Confusing as these sidesteps may seem I’m sure Simon got a lot of practice out of this.

Here is a game Simon tried translating from Lua into Processing, it’s called Brickout:

# MotorAttractor with Box2D in Processing (Java)

In this project Simon combined two exercises and one example from Daniel Shiffman’s Nature of Code book, Chapter 5 – Physic Libraries. The specific physics library used here is Box2D. Simon combined exercises 5.6 (Bridge) and 5.10 (Attraction Apply Force), and example 5.7 (Create a windmill) to create a motor that catapults particles towards an attractor. If the particles fly past the attractor, “bricks” can be added to the canvas by clicking the mouse – the weight of the bricks helps regulate the motor in the right direction. For this project, both gravitational attraction and global gravity force were applied:

# Gravitational Attraction (World Generator in Java). Simon’s new own code!

Simon has come up with a new code of his own! It’s about gravitational attraction (particles attracted to targets or moons attracted to planets) and partially based upon the Box2D library but he wrote most of the code himself. Simon used the gravitational constant and Newton’s law of universal gravitation to build this project. The law states that every point mass (m1) attracts every single other point mass (m2by a force (F) pointing along the line intersecting both points. The force is proportional to the product of the two masses and inversely proportional to the square of the distance between them (r):

Simon had come across this formula before in Daniel Shiffman’s tutorials and just like Daniel he changed r to d (for distance):

float strength = (G * 1 * p.mass) / (distance * distance);

Simon pushed his full code to GitHub at:

https://github.com/simon-tiger/gravitational-attraction/blob/master/WorldGenerator.pde

Here Simon added some new features, like pressing keys for adding new targets and particles:

Here the force is becoming stronger with more targets, Simon explains:

We’ve also got a video of Simon talking about this project in Dutch (showing it to his math teacher):