Created
September 26, 2013 07:44
-
-
Save tado/6711018 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Particle.h" | |
void Particle::setup(ofVec2f _position, ofVec2f _velocity){ | |
// 位置を設定 | |
position = _position; | |
// 初期速度を設定 | |
velocity = _velocity; | |
} | |
// 力をリセット | |
void Particle::resetForce(){ | |
force.set(0, 0); | |
} | |
// 力を加える | |
void Particle::addForce(ofVec2f _force){ | |
force = _force; | |
} | |
// 力の更新 | |
void Particle::updateForce(){ | |
force -= velocity * friction; | |
} | |
// 位置の更新 | |
void Particle::updatePos(){ | |
velocity += force; | |
position += velocity; | |
} | |
void Particle::checkBounds(float xmin, float ymin, float xmax, float ymax){ | |
// 画面の端でバウンドする | |
if (position.x < xmin || position.x > xmax) { | |
velocity.x *= -1; | |
} | |
if (position.y < ymin || position.y > ymax) { | |
velocity.y *= -1; | |
} | |
// 枠内に収める | |
if (position.x < xmin) { | |
position.x = xmin + (xmin - position.x); | |
} | |
if (position.x > xmax) { | |
position.x = xmax - (position.x - xmax); | |
} | |
if (position.y < ymin) { | |
position.y = ymin + (ymin - position.y); | |
} | |
if (position.y > ymax) { | |
position.y = ymax - (position.y - ymax); | |
} | |
} | |
// 描画 | |
void Particle::draw(){ | |
ofSetHexColor(0x3399cc); | |
ofCircle(position, radius); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include "ofMain.h" | |
class Particle { | |
public: | |
// 初期設定 | |
void setup(ofVec2f position, ofVec2f velocity); | |
// 力をリセット | |
void resetForce(); | |
// 力を加える | |
void addForce(ofVec2f force); | |
// 力を更新 | |
void updateForce(); | |
// 位置の更新 | |
void updatePos(); | |
// 画面からはみ出たらバウンドさせる | |
void checkBounds(float xmin, float ymin, float xmax, float ymax); | |
// 描画 | |
void draw(); | |
// 位置ベクトルの配列 | |
ofVec2f position; | |
// 速度ベクトルの配列 | |
ofVec2f velocity; | |
// 力ベクトルの配列 | |
ofVec2f force; | |
// 摩擦係数 | |
float friction; | |
// パーティクルの半径 | |
float radius; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment