# Translating examples from The Nature of Code into Lua (Codea)

This morning Simon attempted to make a more difficult translation in the Codea app of an example from Daniel Shiffman’s book The Nature of Code (Java) into Lua. It concerned the Gravitational Attraction example from Chapter 2 of the book, Forces. Simon is happy with Codea because “It’s really readable!”, “You don’t need semicolons and parenthesis!” and all logic operators are actually typed in words (“and”, “or”, “not”).

Unfortunately, the function Simon introduced as substitute for mouse pressed release on the touch screen didn’t seem to work:

Simon did successfully translate the simple harmonic motion example from Chapter 3, Oscillation: “I use trigonometry!”

For this example, he had to look up a complex formula for mapping a range to another range on the internet b1 + (s – a1)*(b2 – b1)/(a2-a1) “because map function doesn’t exist in Codea, so I wrote that function”.

Swapping the axes:

# Playing with examples on Forces and Oscillation from The Nature of Code

Playing with examples from Chapter 3 of The Nature of Code, Oscillation, covering trigonometry and connecting it to forces. The examples included simple harmonic motion, angular velocity and waves, as well as gravitational attraction from Chapter 2, Forces.

Simon also found a way to be like Daniel Shiffman – he programmed a large webcam canvas and shifted his Processing canvas to the side:

# Simon translated Attraction and Repulsion Forces Coding Challenge into Java

Simon translated part of the Attraction and Repulsion Forces Coding Challenge from JavaScript into Processing (Java). In the first video below he explains the formulas for attraction and repulsion forces:

This is the part that Simon successfully translated into Java:

Around minute 30 of the Attraction and Repulsion Forces Coding Challenge video Simon got stuck with his Java code. Although everything seemed right, his particle didn’t trace nice patterns but went off the screen. Simon was very upset about this, it took me a while to console him. This is where Simon got stuck:

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