[Solved]Replicate pendulum calling its class

Hi, I’m trying to make many pendulums which have the same fulcrum.
I’ve made the project with a pendulum class and called it in ofApp.h.
But only one pendulum has appeared.
Here the code, any help is really appreciated.
thanks in advance

#pragma once

#include "ofMain.h"
#include "pendulum.h"

class ofApp : public ofBaseApp{

	public:
		void setup();
		void update();
		void draw();

		void keyPressed(int key);
		void keyReleased(int key);
		void mouseMoved(int x, int y );
		void mouseDragged(int x, int y, int button);
		void mousePressed(int x, int y, int button);
		void mouseReleased(int x, int y, int button);
		void windowResized(int w, int h);
		void dragEvent(ofDragInfo dragInfo);
		void gotMessage(ofMessage msg);
	
    static const int PEN_NUM = 10;
    
    pendulum Pendulum[PEN_NUM];
    
};

and here’s the ofApp.cpp

#include "ofApp.h"

//--------------------------------------------------------------
void ofApp::setup(){
     for (int i = 0; i < PEN_NUM; i++){
    //Set screen frame rate
    ofSetFrameRate( 60 );
    
    //Set initial values
    ofVec2f pos0;
    pos0= ofPoint( 512, 300 );
    
    ofVec2f pos;
    pos= ofPoint( ofRandom(600), ofRandom(200) );
    
    ofVec2f velocity;
    velocity= ofPoint( ofRandom(100), 0 );
    
   
    Pendulum[i].setup(pos0, pos, velocity);
    }

}

    //--------------------------------------------------------------
    void ofApp::update(){
        for (int i = 0; i < PEN_NUM; i++){
            Pendulum[i].update();
               }
    
    }
    
    //--------------------------------------------------------------
    void ofApp::draw(){
        for (int i = 0; i < PEN_NUM; i++){
            Pendulum[i].draw();
        }
    
    
    }

Hello!
If it’s ok with you, can you show us pendulum class?
And if I were you, I’d write pendulums’ basic information in constructor of pendulum class.

I’ll keep my fingers crossed.

#ifndef __pendulum_arrange2__pendulum__
#define __pendulum_arrange2__pendulum__

#include <stdio.h>

#endif /* defined(__pendulum_arrange2__pendulum__) */

#include "ofMain.h"

class pendulum{
    
public:
    pendulum();
    void setup(ofVec2f pos0, ofVec2f pos, ofVec2f velocity);
    void update();
    void draw();
    
    ofPoint pos0;			//Center of suspension
    ofPoint pos;			//Ball's position
    ofPoint velocity;		//Ball's velocity
    
    void keyPressed(int key);
    void keyReleased(int key);
    void mouseMoved(int x, int y);
    void mouseDragged(int x, int y, int button);
    void mousePressed(int x, int y, int button);
    void mouseReleased(int x, int y, int button);
    void windowResized(int w, int h);
    void dragEvent(ofDragInfo dragInfo);
    void gotMessage(ofMessage msg);
};

and here’s the pendulum.cpp

#include "pendulum.h"

pendulum::pendulum(){
    
}

//--------------------------------------------------------------
void pendulum::setup(ofVec2f _pos0, ofVec2f _pos, ofVec2f _velocity){
    pos0= _pos0;
    pos = _pos;
    velocity = _velocity;
    
}

//--------------------------------------------------------------
void pendulum::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
}

//--------------------------------------------------------------
void pendulum::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
}

((waa I’ve just seen your icon photo at the OFseminar_japan yesterday! Tabata-sensei arigato-gozaimasita :)

((I’ll think of the different ver. project that you mentioned from now.))

Hello.
I am surprised now! And I watched your code.

Please try to conceal ofBackground(255, 255, 255); of pendulum class’s draw function.
Because of calling ofBackground(255) in for statement of ofMain file’s draw function, this program draw fully white color every pendulum.

Truly yours!!

I made it ! Now that you say that, it’s really true…!
Thank you very much. I hope I’ll be able to do many things with codes someday “:)”
regards,
:star: