[work 22] Moving Square

[work 22] Moving Square

Movie

Source code

about

  • 四角形を移動させる
  • 上下・左右の平行移動
  • 前後の平行移動
  • z軸周りの回転
  • ランダムなベクトルを軸とした回転

file

  • 上部にあるファイル名が表示されているボタンを押すと、表示されるファイルが切り替わります
  • 別ウィンドウ表示したい時や行番号などが無いRawMode表示したい時は、コード内右上のボタンを押してください(ボタンはマウスオーバーすると表示されます)
#include "ofMain.h"
#include "ofApp.h"

//================================
int main( ){

    // 4K:4096x2160
    // 2K:2048x1080
    // FullHD:1920x1080
    // HD:1440x1080
    // HD720p:1280x720
    // DVD:720x480
    // setup the GL context
	ofSetupOpenGL(1280,720, OF_WINDOW);

	// this kicks off the running of my app
	// can be OF_WINDOW or OF_FULLSCREEN
	// pass in width and height too:
	ofRunApp( new ofApp());

}
#pragma once

#include "ofMain.h"

#include "Square.hpp"


class ofApp : public ofBaseApp{
	public:
    ofApp();
    ~ofApp();
    
		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 mouseEntered(int x, int y);
		void mouseExited(int x, int y);
		void windowResized(int w, int h);
		void dragEvent(ofDragInfo dragInfo);
		void gotMessage(ofMessage msg);
    
private:
    std::vector<unique_ptr<Square>> sqs;
};
#include "ofApp.h"


ofApp::ofApp(){
    
}

ofApp::~ofApp(){
}

//--------------------------------------------------------------
void ofApp::setup(){
    double fps = 30;
    
    ofSetFrameRate(fps);
    ofBackground(0);
    ofSetBackgroundAuto(true);
    
    // upper left
    sqs.push_back(unique_ptr<Square>(new Square(S_PARALLEL_2D, ofVec3f(ofGetWidth()/2-150, ofGetHeight()/2-150, 0))));
    // upper right
    sqs.push_back(unique_ptr<Square>(new Square(S_ROTATION_3D, ofVec3f(ofGetWidth()/2+150, ofGetHeight()/2-150, 0))));
    // lower left
    sqs.push_back(unique_ptr<Square>(new Square(S_PARALLEL_3D, ofVec3f(ofGetWidth()/2-150, ofGetHeight()/2+150, 0))));
    // lower right
    sqs.push_back(unique_ptr<Square>(new Square(S_ROTATION_2D, ofVec3f(ofGetWidth()/2+150, ofGetHeight()/2+150, 0))));
}

//--------------------------------------------------------------
void ofApp::update(){
    for (unique_ptr<Square> &s : sqs) {
        s->update();
    }
}

//--------------------------------------------------------------
void ofApp::draw(){
    for (unique_ptr<Square> &s : sqs) {
        s->display();
    }
}
#ifndef Square_hpp
#define Square_hpp

#include <stdio.h>
#include "ofMain.h"

#define LOCATION_MAX (20)

typedef enum {
    S_PARALLEL_2D = 0,
    S_PARALLEL_3D,
    S_ROTATION_2D,
    S_ROTATION_3D,
} S_PATTERN;

typedef struct {
    ofVec3f location;
    float rotateAngle;
} squareSet;

class Square {
public:
    Square(S_PATTERN ptn, ofVec3f org);
    ~Square();
    void update();
    void display();
    
private:
    void parallel2d();
    void parallel3d();
    void rotation2d();
    void rotation3d();
    
    S_PATTERN pattern;
    ofVec3f origin;
    float angle;
    float angleVelocity;
    float amp;
    float rotateAngleVelo;
    ofVec3f rotateAxis;
    std::list<squareSet> settings;
    float length;
};
#endif /* Square_hpp */
#include "Square.hpp"



Square::Square(S_PATTERN ptn, ofVec3f org)
{
    pattern = ptn;
    origin = org;
    angle = 0;
    angleVelocity = 0.05;
    rotateAngleVelo = 0.1;
    rotateAxis = ofVec3f(0,0,1);
    amp = 120;
    length = 100;
}


Square::~Square()
{
    
}


void Square::update()
{
    switch (pattern) {
        case S_PARALLEL_2D:
            parallel2d();
            break;
        case S_PARALLEL_3D:
            parallel3d();
            break;
        case S_ROTATION_2D:
            rotation2d();
            break;
        case S_ROTATION_3D:
            rotation3d();
            break;
        default:
            break;
    }
}


void Square::display()
{
    ofColor c(255);
    float a = c.limit();
    for (squareSet s : settings) {
        a -= (c.limit() / LOCATION_MAX);
        
        ofPushMatrix();
        ofTranslate(glm::vec3(s.location.x, s.location.y, s.location.z));
        ofRotateRad(s.rotateAngle, rotateAxis.x, rotateAxis.y, rotateAxis.z);
        ofSetColor(c, a);
        ofNoFill();
        ofSetRectMode(OF_RECTMODE_CENTER);
        ofDrawRectangle(0, 0, 0, length, length);
        ofPopMatrix();
    }
}


void Square::parallel2d()
{
    float offset = std::sin(angle) * amp;
    squareSet set;
    
    if (angle < TWO_PI * 2) {
        set.location.x = origin.x + offset;
        set.location.y = origin.y;
        set.location.z = 0;
    } else {
        set.location.x = origin.x;
        set.location.y = origin.y + offset;
        set.location.z = 0;
    }
    set.rotateAngle = 0;
    settings.push_front(set);
    
    if (settings.size() > LOCATION_MAX) {
        settings.pop_back();
    }
    
    angle += angleVelocity;
    
    if (angle > TWO_PI * 4) {
        angle = 0;
    }
}


void Square::parallel3d()
{
    float offset = std::sin(angle) * amp;
    squareSet set;
    
    set.location.x = origin.x;
    set.location.y = origin.y;
    set.location.z = origin.z + offset;
    set.rotateAngle = 0;
    settings.push_front(set);
    
    if (settings.size() > LOCATION_MAX) {
        settings.pop_back();
    }
    
    angle += angleVelocity;
}


void Square::rotation2d()
{
    squareSet set;
    
    set.location.x = origin.x;
    set.location.y = origin.y;
    set.location.z = origin.z;
    set.rotateAngle = settings.front().rotateAngle + rotateAngleVelo;
    if (set.rotateAngle < -TWO_PI * 2 || set.rotateAngle > TWO_PI * 2) {
        set.rotateAngle = 0;
        rotateAngleVelo *= -1;
    }
    settings.push_front(set);
    
    if (settings.size() > LOCATION_MAX) {
        settings.pop_back();
    }
}


void Square::rotation3d()
{
    squareSet set;
    
    set.location.x = origin.x;
    set.location.y = origin.y;
    set.location.z = origin.z;
    set.rotateAngle = settings.front().rotateAngle + rotateAngleVelo;
    if (set.rotateAngle < -TWO_PI * 2 || set.rotateAngle > TWO_PI * 2) {
        set.rotateAngle = 0;
        rotateAngleVelo *= -1;
        rotateAxis = ofVec3f(ofRandom(1), ofRandom(1), ofRandom(1));
    }
    settings.push_front(set);
    
    if (settings.size() > LOCATION_MAX) {
        settings.pop_back();
    }
}

Link to the reference page

ソースコードで使用したAPIの中から要点になりそうなものをいくつか選んでリストアップしました。

categoryAPI/Lib
openframeworksofGraphics ofRotateRad

Development environment

  • openframeworks 0.10.1
  • c++
  • macOS
  • Xcode