[work 48] Pinball

[work 48] Pinball

Movie

Source code

about

  • ピンにあたると方向転換するボール

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 "Line.hpp"
#include "Field.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<std::shared_ptr<Line>> lines;
    std::shared_ptr<Field> field;
};
#include "ofApp.h"


ofApp::ofApp(){
    
}

ofApp::~ofApp(){
    
}

//--------------------------------------------------------------
void ofApp::setup(){
    double fps = 30;
    
    
    ofSetFrameRate(fps);
    ofBackground(255);
    ofSetBackgroundAuto(true);
    ofSetVerticalSync(true);
    
    lines.push_back(make_shared<Line>());
    lines.back()->setup();
    
    field = make_shared<Field>();
    field->setup();
}

//--------------------------------------------------------------
void ofApp::update(){
    if ((int)ofRandom(1, 10) == 1) {
        lines.push_back(make_shared<Line>());
        lines.back()->setup();
    }
    
    auto itr = lines.begin();
    while (itr != lines.end()) {
        itr->get()->update(field->conflict(*(itr->get())));
        
        if (itr->get()->isDead()) {
            itr = lines.erase(itr);
        } else {
            ++itr;
        }
        
        if (lines.empty()) {
            break;
        }
    }
    
    field->update();
}

//--------------------------------------------------------------
void ofApp::draw(){
    field->display();
    
    for (shared_ptr<Line> l : lines) {
        l->display();
    }
}
#ifndef Field_hpp
#define Field_hpp

#include <stdio.h>

#include "ofMain.h"
#include "Line.hpp"

struct Circle {
    ofVec2f center;
    int radius;
    bool conflict;
};

class Field {
public:
    Field();
    ~Field();
    void setup();
    void update();
    void display();
    bool conflict(Line l);
private:
    std::vector<Circle> circles;
};
#endif /* Field_hpp */
#include "Field.hpp"


Field::Field()
{
    
}


Field::~Field()
{
    
}


void Field::setup()
{
    for (int i = 0; i < 200; i++) {
        Circle c;
        float w = ofMap(ofRandom(1), 0, 1, ofGetWidth() * 0.1, ofGetWidth() * 0.9);
        float h = ofMap(ofRandom(1), 0, 1, ofGetHeight() * 0.1, ofGetHeight() * 0.9);
        c.center = ofVec2f(w, h);
        c.radius = 0;
        c.conflict = false;
        circles.push_back(c);
    }
}


void Field::update()
{
    for (Circle &c : circles) {
        if (c.conflict != false) {
            if (c.radius < 30) {
                c.radius += 1;
            } else {
                c.radius = 0;
                c.conflict = false;
            }
        }
    }
}


void Field::display()
{
    for (Circle c : circles) {
        ofNoFill();
        ofSetColor(0);
        ofDrawCircle(c.center, 4);
        
        if (c.conflict != false) {
            ofNoFill();
            ofSetColor(255, 0, 0);
            ofDrawCircle(c.center, c.radius);
        }
    }
}


bool Field::conflict(Line l)
{
    bool conf = false;
    ofVec2f loc = l.getLocation();
    
    for (Circle &c : circles) {
        if ((c.center - loc).length() < 4) {
            conf = true;
            c.conflict = conf;
            break;
        }
    }
    return conf;
}
#ifndef Line_hpp
#define Line_hpp

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

struct Dot {
    ofVec2f location;
    ofVec2f direction;
};


class Line {
public:
    Line();
    ~Line();
    void setup();
    void update(bool conf);
    void display();
    ofVec2f getLocation();
    bool isDead();
private:
    ofVec2f direction;
    float speed;
    int length;
    std::list<Dot> dots;
    ofColor color;
};
#endif /* Line_hpp */
#include "Line.hpp"


Line::Line()
{
    
}


Line::~Line()
{
    
}


void Line::setup()
{
    Dot d;
    int cellSize = 50;
    length = 200;
    speed = ofRandom(1, 4);
    
    if ((int)ofRandom(0, 2) == 0) {
        int row = (int)ofRandom(1, ofGetHeight() / cellSize);
        d.direction = ofVec2f(1, 0) * speed;
        d.location = ofVec2f(0, row * cellSize);
        dots.push_back(d);
    } else {
        int col = (int)ofRandom(1, ofGetWidth() / cellSize);
        d.direction = ofVec2f(0, 1) * speed;
        d.location = ofVec2f(col * cellSize, 0);
        dots.push_back(d);
    }
    
//    color = ofColor(ofRandom(255),ofRandom(255),ofRandom(255));
    color = ofColor(0);
}


void Line::update(bool conf)
{
    Dot d = dots.back();
    if (conf) {
        if ((int)ofRandom(0, 2) == 0) {
            d.direction.rotate(45);
        } else {
            d.direction.rotate(-45);
        }
    }
    d.location += d.direction;
    dots.push_back(d);
    
    if (dots.size() > length) {
        dots.pop_front();
    }
}


void Line::display()
{
    int index = 0;
    
    for (auto itr = dots.rbegin(); itr != dots.rend(); ++itr) {
        color.a = (int)ofMap(index, 0, length, 255, 0);
        ofSetColor(color);
        ofFill();
        ofDrawCircle((*itr).location, 2);
        index++;
    }
}


ofVec2f Line::getLocation()
{
    return dots.back().location;
}


bool Line::isDead()
{
    bool dead = true;
    ofRectangle r = ofRectangle(0, 0, ofGetWidth(), ofGetHeight());
    
    for (Dot d : dots) {
        if (r.inside(d.location)) {
            dead = false;
            break;
        }
    }
    return dead;
}

Link to the reference page

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

categoryAPI/Lib
openframeworksofVec2f

Development environment

  • openframeworks 0.10.1
  • c++
  • macOS
  • Xcode