官术网_书友最值得收藏!

Creating your first project – the Pendulum example

Let's create an openFrameworks project, which draws a moving pendulum in 2D, consisting of a ball dangled on a rubber segment. The example is based on the emptyExample project in openFrameworks. Perform the following steps to create the project:

  1. Copy the emptyExample project's folder into the folder intended for holding your applications (like apps/myApps), and rename it to Pendulum.
  2. Go inside the Pendulum folder and open this project in your development environment (emptyExample.sln for Visual Studio, emptyExample.xcodeproj for Xcode, or emptyExample.workspace for Code::Blocks).
  3. Open the file testApp.h in the development environment, and in the testApp class declaration add the declarations for the pendulum's center of suspension and the ball's position and velocity:
    ofPoint pos0;        //Center of suspension
    ofPoint pos;         //Ball's position
    ofPoint velocity;    //Ball's velocity

    Here ofPoint is the openFrameworks' class for holding point coordinates, it has x and y members (we will study it in Chapter 2, Drawing in 2D).

  4. Open the file testApp.cpp, and fill the body of the testApp::setup() function definition:
    void testApp::setup(){
      //Set screen frame rate
      ofSetFrameRate( 60 );
    
      //Set initial values
      pos0 = ofPoint( 512, 300 );
      pos = ofPoint( 600, 200 );
      velocity = ofPoint( 100, 0 );
    }

    In this function we set the frame rate to 60 frames per second, and we also set initial values for all three points.

  5. Now fill the body of the testApp::update() function definition:
    void testApp::update(){
      //Constants
      float dt = 1.0 / 60.0;         //Time step
      float mass = 0.1;              //Mass of a ball
      float rubberLen = 200.0;       //Segment's length
      float k = 0.5;                 //Segment's stiffness
      ofPoint g( 0.0, 9.8 );         //Gravity force
    
      //Compute Hooke's force
      ofPoint delta = pos - pos0;
      float len = delta.length();   //Vector's length
      float hookeValue = k * (len - rubberLen);
      delta.normalize();            //Normalize vector's length
      ofPoint hookeForce = delta * (-hookeValue);
    
      //Update velocity and pos
      ofPoint force = hookeForce + g;  //Resulted force
      ofPoint a = force / mass;        //Second Newton's law
      velocity += a * dt;              //Euler method
      pos += velocity * dt;            //Euler method
    }

    This function updates velocity and pos, using Newton's second law and the Euler method. For such a purpose, we compute the force acting on a ball as a sum of Hooke's force between the ball, suspension point, and gravity force.

    Tip

    The details on the Euler method can be seen in the Defining the particle functions section in Chapter 3, Building a Simple Particle System. The information on the Newton's second law, Hooke's force, and gravity force can be seen at the following links:

  6. Finally, fill the body of the testApp::draw() function definition:
    void testApp::draw(){
      //Set white background
      ofBackground( 255, 255, 255 );  
    
      //Draw rubber as a blue line
      ofSetColor( 0, 0, 255 );                 //Set blue color
      ofLine( pos0.x, pos0.y, pos.x, pos.y );  //Draw line
    
      //Draw ball as a red circle
      ofSetColor( 255, 0, 0 );                 //Set red color
      ofFill();                                //Enable filling
      ofCircle( pos.x, pos.y, 20 );            //Draw circle
    }

    Here we set a white background, draw a rubber as a blue line from pos0 to pos, and also draw a ball as a red circle. Note that we use the ofFill() function, which enables openFrameworks' mode to draw filled primitives (circles, rectangles, and triangles). See more details on these drawing functions in Chapter 2, Drawing in 2D.

  7. Run the project. You will see the animation of a moving ball:

Play with numerical values in the setup() and update() functions and see how it affects the dynamics of the pendulum.

主站蜘蛛池模板: 东明县| 普陀区| 黄冈市| 牟定县| 修文县| 宜都市| 榆树市| 徐闻县| 乌拉特中旗| 宣化县| 连平县| 鄂伦春自治旗| 昌江| 诸城市| 泸水县| 九江县| 台北县| 西城区| 丰原市| 淮南市| 长顺县| 屏边| 平度市| 崇义县| 铜鼓县| 荥阳市| 大同市| 莱州市| 东兰县| 长春市| 肇源县| 金溪县| 黄骅市| 陆良县| 察哈| 桃江县| 杨浦区| 台东市| 筠连县| 蚌埠市| 霍城县|