1つのボールがふらふら動く
提供:kuhalaboWiki
(版間での差分)
(→参考) |
|||
1行: | 1行: | ||
+ | ;ボールが1つの場合 | ||
+ | |||
<pre> | <pre> | ||
#include "ofApp.h" | #include "ofApp.h" | ||
68行: | 70行: | ||
</pre> | </pre> | ||
+ | ;ボールが複数の場合(配列の利用) | ||
+ | |||
+ | <pre> | ||
+ | #include "ofApp.h" | ||
+ | #define NUM 100 //円の数を表す定数NUMを1000と定義 | ||
+ | |||
+ | float loc_x[NUM]; | ||
+ | float loc_y[NUM]; | ||
+ | |||
+ | float speed_x[NUM]; | ||
+ | float speed_y[NUM]; | ||
+ | float acc_x[NUM]; | ||
+ | float acc_y[NUM]; | ||
+ | |||
+ | |||
+ | //-------------------------------------------------------------- | ||
+ | void ofApp::setup(){ | ||
+ | |||
+ | ofBackground(0, 0, 0); //背景色の設定 | ||
+ | ofSetColor(255, 255, 0); //描画色の設定 | ||
+ | ofSetCircleResolution(64);//円の解像度 | ||
+ | ofEnableAlphaBlending(); //アルファチャンネルを有効にする | ||
+ | ofSetBackgroundAuto(false); //背景色での塗りつぶしなし | ||
+ | ofSetFrameRate(30); //フレームレイト設定 | ||
+ | |||
+ | for( int i = 0; i < NUM; i++){ | ||
+ | |||
+ | loc_x[i] = 500; | ||
+ | loc_y[i] = 500; | ||
+ | speed_x[i] = 3; | ||
+ | speed_y[i] = 2; | ||
+ | |||
+ | acc_x[i] = 0; | ||
+ | acc_y[i] = 0; | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | //-------------------------------------------------------------- | ||
+ | void ofApp::update(){ | ||
+ | |||
+ | for( int i = 0; i < NUM; i++){ | ||
+ | acc_x[i] = ofRandom(-0.5,0.5); | ||
+ | acc_y[i] = ofRandom(-0.5,0.5); | ||
+ | |||
+ | speed_x[i] = speed_x[i] + acc_x[i]; | ||
+ | speed_y[i] = speed_y[i] + acc_y[i]; | ||
+ | |||
+ | if( loc_x[i] > ofGetWidth() ){ | ||
+ | speed_x[i] = speed_x[i] * -1; | ||
+ | } | ||
+ | if( loc_x[i] < 0 ){ | ||
+ | speed_x[i] = speed_x[i] * -1; | ||
+ | } | ||
+ | |||
+ | if( loc_y[i] > ofGetHeight() ){ | ||
+ | speed_y[i] = speed_y[i] * -1; | ||
+ | } | ||
+ | if( loc_y[i] < 0 ){ | ||
+ | speed_y[i] = speed_y[i] * -1; | ||
+ | } | ||
+ | |||
+ | loc_x[i] = loc_x[i] + speed_x[i]; | ||
+ | loc_y[i] = loc_y[i] + speed_y[i]; | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | //-------------------------------------------------------------- | ||
+ | void ofApp::draw(){ | ||
+ | |||
+ | //全画面を半透明の黒でフェード | ||
+ | ofSetColor(0, 0, 0, 100); | ||
+ | ofRect(0, 0, ofGetWidth(), ofGetHeight()); | ||
+ | |||
+ | for( int i = 0; i < NUM; i++){ | ||
+ | ofSetColor(31, 63, 255); | ||
+ | ofCircle(loc_x[i],loc_y[i], 5); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | </pre> | ||
== 参考 == | == 参考 == |
2015年10月27日 (火) 08:26時点における最新版
- ボールが1つの場合
#include "ofApp.h" float loc_x; float loc_y; float speed_x; float speed_y; float acc_x; float acc_y; //-------------------------------------------------------------- void ofApp::setup(){ ofBackground(0, 0, 0); //背景色の設定 ofSetColor(255, 255, 0); //描画色の設定 ofSetCircleResolution(64);//円の解像度 ofEnableAlphaBlending(); //アルファチャンネルを有効にする ofSetBackgroundAuto(false); //背景色での塗りつぶしなし ofSetFrameRate(30); //フレームレイト設定 loc_x = 500; loc_y = 500; speed_x = 7; speed_y = 4; acc_x = 0; acc_y = 0; } //-------------------------------------------------------------- void ofApp::update(){ acc_x = ofRandom(-0.8,0.8); acc_y = ofRandom(-0.8,0.8); speed_x = speed_x + acc_x; speed_y = speed_y + acc_y; if( loc_x > ofGetWidth() ){ speed_x = speed_x * -1; } if( loc_x < 0 ){ speed_x = speed_x * -1; } if( loc_y > ofGetHeight() ){ speed_y = speed_y * -1; } if( loc_y < 0 ){ speed_y = speed_y * -1; } loc_x = loc_x + speed_x; loc_y = loc_y + speed_y; } //-------------------------------------------------------------- void ofApp::draw(){ //全画面を半透明の黒でフェード ofSetColor(0, 0, 0, 10); ofRect(0, 0, ofGetWidth(), ofGetHeight()); ofSetColor(31, 63, 255); ofCircle(loc_x,loc_y, 40); }
- ボールが複数の場合(配列の利用)
#include "ofApp.h" #define NUM 100 //円の数を表す定数NUMを1000と定義 float loc_x[NUM]; float loc_y[NUM]; float speed_x[NUM]; float speed_y[NUM]; float acc_x[NUM]; float acc_y[NUM]; //-------------------------------------------------------------- void ofApp::setup(){ ofBackground(0, 0, 0); //背景色の設定 ofSetColor(255, 255, 0); //描画色の設定 ofSetCircleResolution(64);//円の解像度 ofEnableAlphaBlending(); //アルファチャンネルを有効にする ofSetBackgroundAuto(false); //背景色での塗りつぶしなし ofSetFrameRate(30); //フレームレイト設定 for( int i = 0; i < NUM; i++){ loc_x[i] = 500; loc_y[i] = 500; speed_x[i] = 3; speed_y[i] = 2; acc_x[i] = 0; acc_y[i] = 0; } } //-------------------------------------------------------------- void ofApp::update(){ for( int i = 0; i < NUM; i++){ acc_x[i] = ofRandom(-0.5,0.5); acc_y[i] = ofRandom(-0.5,0.5); speed_x[i] = speed_x[i] + acc_x[i]; speed_y[i] = speed_y[i] + acc_y[i]; if( loc_x[i] > ofGetWidth() ){ speed_x[i] = speed_x[i] * -1; } if( loc_x[i] < 0 ){ speed_x[i] = speed_x[i] * -1; } if( loc_y[i] > ofGetHeight() ){ speed_y[i] = speed_y[i] * -1; } if( loc_y[i] < 0 ){ speed_y[i] = speed_y[i] * -1; } loc_x[i] = loc_x[i] + speed_x[i]; loc_y[i] = loc_y[i] + speed_y[i]; } } //-------------------------------------------------------------- void ofApp::draw(){ //全画面を半透明の黒でフェード ofSetColor(0, 0, 0, 100); ofRect(0, 0, ofGetWidth(), ofGetHeight()); for( int i = 0; i < NUM; i++){ ofSetColor(31, 63, 255); ofCircle(loc_x[i],loc_y[i], 5); } }