<< back to Examples page

Example 2: Nesting
Nesting: This example demonstrates the use of the nestIn() command, which allows nesting (embedding) of one environment into the other.
A nested environment is dependant upon the transformations of its containing (parent) environment. This is somewhat similar to the manner in which the position of your fingers are dependent upon the general location of your hand. By moving your hand, you are also moving all the fingers. One can say that your fingers are nested in your hand, and your hand is the containing environment for your fingers.
//NESTING BSPACE
BSpace s1,s2,s3;
float angle; 
void setup(){
           size(200,200);
           background(200);
           smooth();
// Create three BSpace objects
           s1 = new BSpace(this);
           s2 = new BSpace(s1);  // nest s2 enviornment into s1.
                                          // An alterantive is using the nestIn() command: s2.nestIn(s1);
s3 = new BSpace(s2); // nest s3 enviornment into s2. s2.setAxis(20,20); // s2 Axis coordinate system is now relative to the s1 axis which is affected by the mouse. s3.setAxis(20,20); }
void loop(){
           angle += 0.04;
           
           s1.setAxis(mouseX,mouseY); // move the BSpace (and all nested BSpaces) to mouse cursor.
           s1.rotateZ(angle);
           s1.rect(0,0,20,20);
           
           s2.rotateZ(angle);
           s2.rect(0,0,20,20);
           
           s3.rotateX(angle);
           s3.rect(0,0,20,20);
}