- Mastering openFrameworks:Creative Coding Demystified
- Denis Perevalov
- 583字
- 2021-08-06 16:54:15
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:
- Copy the
emptyExample
project's folder into the folder intended for holding your applications (likeapps/myApps
), and rename it toPendulum
. - Go inside the
Pendulum
folder and open this project in your development environment (emptyExample.sln
for Visual Studio,emptyExample.xcodeproj
for Xcode, oremptyExample.workspace
for Code::Blocks). - Open the file
testApp.h
in the development environment, and in thetestApp
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). - Open the file
testApp.cpp
, and fill the body of thetestApp::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. - 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
andpos
, 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:
- 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
topos
, and also draw a ball as a red circle. Note that we use theofFill()
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. - 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.
- scikit-learn Cookbook
- 深度學習經典案例解析:基于MATLAB
- Leap Motion Development Essentials
- 樂高機器人設計技巧:EV3結構設計與編程指導
- Python高效開發實戰:Django、Tornado、Flask、Twisted(第3版)
- 數據結構案例教程(C/C++版)
- Nginx Lua開發實戰
- 零基礎趣學C語言
- Mastering Unity 2D Game Development(Second Edition)
- Advanced Express Web Application Development
- 速學Python:程序設計從入門到進階
- Extreme C
- Unity&VR游戲美術設計實戰
- Go語言從入門到精通
- Appcelerator Titanium:Patterns and Best Practices