Creating a Creature- Heading and Oscillation

In order to create the pigeons I talked about last post I had to learn some more ways maths can be used in Programming to provide some of the fundamentals of building a bird-like object.

Sketch by Daniel Shiffman. The rectangles follow the direction they are travelling in.

As a starting point, I decided to look into how to make an object face in the direction it is travelling. Thankfully Processing has an inbuilt function PVector.heading(); which calculates the inverse tangent of a PVector, which gives the direction the object should face if travelling in the direction of the PVector.

In the example above Shiffman uses the same basic sketch as the Gravity example seen previously, except here the Mover objects are rectangles and have a heading function. The rectangles can then be rotated by this angle. However the lines pushMatrix(); and popMatrix(); are used so that the whole sketch is not rotated.  Below are the lines in the code:

   //open "Matrix Stack" allows rectangle to be rotateed without
    //the wholse sketch doing  the same
    //angle of rotation
    float theta = velocity.heading();
    //rotate from middle of rectangle
    //move rectanlge
    //rotate by heading angle
    //draw rectangle
    rect(0, 0, 30, 10);
    //end Matrix Stack so other things are rotated etc


Adding shapes to the heading function

Next I experimented with adding more shapes to the heading sketch above. By adding to circle together I was able to create an approximation of a head and a body. I could use this as a base and add other shapes on top of it to create my bird creatures.

Diagram of forces acting on birds in flight by Hutchinson (1995)

By far one of the most important features of a bird are the wings. As described by Hutchinson, (1995) the flapping of wings in birds is critical to the motion of flight. By rapidly flapping their wings birds create the lift needed to overcome their weight and air drag and allows them to  move through the air. Although many birds use soaring motions, where their wings are fairly static, all flying birds need a rapid flapping motion to create lift.

The pattern of sine waves.

Oscillation is one of the ways in physics up and down motion is simulated, for example the movement of a spring. The pattern comes from the values calculated by running values through a sine function (y=sin(x).)This pattern is similar to the pattern that wing tips trace as they move through the air. I decided to look  into how sine waves are used in Processing to see if I could use them to simulate wing motion.

Sketch by Daniel Shiffman

In the above sketch by Shiffman creates an array of objects that oscillate around a singular point. Like in the previous Gravity example, a simplified version of real-world physics is used to simulate the motion. To work they need three PVectors, angle, velocity and amplitude.
The angle is set at zero, whilst the other two Pvectors are given random values. The position of the oscillation is given by adding the velocity to the angle each frame. The product of the sine value of the angle is then times by the amplitude. This gives the position of the oscillation. As a programmer by manipulating the value of the amplitude, you can control the “peaks” of the oscillation and by manipulating the velocity you can control the speed.

Manipulation of the above sketch to create a flapping motion.

By manipulating the parameters of the PVectors in Shiffman’s sketch I was able to create a flapping motion. By setting the x and y values of the velocity to 0.1 I was able to create a flapping motion. By then copying the object with the negative of these values I could create oscillating object that move opposite to each other.

Substituting the lines in the previous sketch with rectangles

Next I added in shapes where the lines used to be. First I experimented with rectangles to see if the oscillation motion could be seen as wings. I think this experiment went quite well, though I will need further experimentation to make it more believable.

Trying the same thing with ellipses.

I also played with using ellipses instead of using rectangles. I tihnk this was more effective as the the circles look somewhat like feathers or panes in a butterfly’s wings.

Move the mouse in the sketch. A butterfly will be placed at the mouse’s position

I also experimented with how the oscillating sketch will look by moving by substituting the center of the area of oscillation with the position of the mouse. Surprisingly the movement looks much better when the whole object is moving. However in this sketch I am still using two separate but “opposite” objects and ideally I would need to put all these functions into one object for the sketch to be less cluttered.

Hopefully I can use these experiments to build a bird by putting these heading and oscillation techniques together. Although I will need to carry out more experimentation with the exact nature of the oscillation functions before I am happy with the result. Next I am going to try to combine the different functions I learned in this post together and try prototype some pigeon like creatures.

Hutchinson, J., 1995. Vertebrate Flight: The Physics of Flight Available at: [Accessed 21.12.2014]

Shiffman, D., 2012. The Nature of Code [online] Mountain View: Creative Commons