[work 5] chase
![[work 5] chase](https://i0.wp.com/tsukuru.hayato-works.com/wp-content/uploads/2019/02/outFrameImg0077.png?fit=1024%2C576&ssl=1)
Movie
Source code
about
- 時計回りに逃げる赤いボールをその他のボールが追う
- 赤いボールの移動角度はパーリンノイズを使って決める(マイナスもあり)
- 赤いボールは進行方向に対して左右に揺れる
- 追いかける側は、赤いボールの中心から半径200の範囲内にある点を目指す(およその方角に向かって追いかける。赤いボールの中心を目指すとつまらない動きになるため。)
- ofRotateで座標系を回転させる
file
- 上部にあるファイル名が表示されているボタンを押すと、表示されるファイルが切り替わります
- 別ウィンドウ表示したい時や行番号などが無いRawMode表示したい時は、コード内右上のボタンを押してください(ボタンはマウスオーバーすると表示されます)
#include "ofMain.h"
#include "ofApp.h"
//========================================
int main( ){
// 4K:4096x2160
// 2K:2048x1080
// FullHD:1920x1080
// HD:1440x1080
// HD720p:1280x720
// DVD:720x480
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 "Walker.hpp"
#include "Mover.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:
ofEasyCam cam;
Walker *walker;
Mover *m[MOVER_MAX];
};
#include "ofApp.h"
ofApp::ofApp(){
walker = new Walker();
for (int i = 0; i < MOVER_MAX; i++) {
m[i] = new Mover();
}
}
ofApp::~ofApp(){
for (int i = 0; i < MOVER_MAX; i++) {
delete m[i];
}
delete walker;
}
//--------------------------------------------------------------
void ofApp::setup(){
double fps = 30;
ofBackground(255, 255, 255);
ofSetBackgroundAuto(true); // clear frame:true
ofSetFrameRate(fps);
walker->setup();
}
//--------------------------------------------------------------
void ofApp::update(){
ofVec2f target;
walker->update();
target = walker->getPos();
for (int i = 0; i < MOVER_MAX; i++) {
m[i]->update(target);
m[i]->checkEdges();
}
}
//--------------------------------------------------------------
void ofApp::draw(){
walker->draw();
for (int i = 0; i < MOVER_MAX; i++) {
m[i]->display();
}
}
#ifndef Walker_hpp
#define Walker_hpp
#include <stdio.h>
#include "ofMain.h"
class Walker {
public:
Walker();
~Walker();
void setup();
void update();
void draw();
ofVec2f getPos();
private:
ofVec2f location;
int radius;
int minRadius;
int maxRadius;
int deg;
int minStepDeg;
int maxStepDeg;
float radian;
float tx1;
float tx2;
};
#endif /* Walker_hpp */
#include "Walker.hpp"
Walker::Walker()
{
location = ofVec2f(ofGetWidth() / 2, ofGetHeight() / 2);
radius = 0;
minRadius = 50;
maxRadius = 400;
deg = 0;
minStepDeg = -4;
maxStepDeg = 6;
tx1 = 0;
tx2 = 10000;
}
Walker::~Walker()
{
}
void Walker::setup()
{
}
void Walker::update()
{
radius = ofMap(ofNoise(tx1), 0, 1, minRadius, maxRadius);
tx1 += 0.1;
deg += (int)ceil(ofMap(ofSignedNoise(tx2), -1, 1, minStepDeg, maxStepDeg));
tx2 += 0.1;
radian = (TWO_PI / 360) * (deg % 360);
location.x = (ofGetWidth() / 2) + radius * cos(radian);
location.y = (ofGetHeight() / 2) + radius * sin(radian);
}
void Walker::draw()
{
ofSetColor(255, 0, 0, 128);
ofDrawCircle(location.x, location.y, 15);
}
ofVec2f Walker::getPos()
{
return ofVec2f(location.x, location.y);
}
#ifndef Mover_hpp
#define Mover_hpp
#include <stdio.h>
#include "ofMain.h"
#define MOVER_MAX (8)
class Mover {
public:
Mover();
~Mover();
void update(ofVec2f &target);
void checkEdges();
void display();
private:
ofVec2f location;
ofVec2f velocity;
ofVec2f acceleration;
float topspeed;
float distance;
};
#endif /* Mover_hpp */
#include "Mover.hpp"
Mover::Mover()
{
location = ofVec2f(ofRandom(ofGetWidth()), ofRandom(ofGetHeight()));
velocity = ofVec2f(0, 0);
acceleration = ofVec2f(0, 0);
topspeed = 10;
}
Mover::~Mover()
{
}
void Mover::update(ofVec2f &target)
{
ofVec2f t = ofVec2f(target.x + ofRandom(-200, 200), target.y + ofRandom(-200, 200));
ofVec2f dir = t - location;
distance = dir.length();
acceleration = dir.normalize();
acceleration *= 0.5;
velocity += acceleration;
velocity.limit(topspeed);
location += velocity;
}
void Mover::checkEdges()
{
if (location.x > ofGetWidth()) {
location.x = 0;
} else if (location.x < 0) {
location.x = ofGetWidth();
}
if (location.y > ofGetHeight()) {
location.y = 0;
} else if (location.y < 0) {
location.y = ofGetHeight();
}
}
void Mover::display()
{
ofSetColor(0, 255, 0, 60);
ofDrawCircle(location.x, location.y, 20);
}
Link to the API reference page
ソースコードで使用したAPIの中から要点になりそうなものをいくつか選んでリストアップしました。
| category | API |
| openframeworks | ofMath ofRandom |