質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Processing

Processingは、オープンソースプロジェクトによるCGのためのプログラミング言語です。Javaをベースにしており、グラフィック機能に特化しています。イメージの生成やアニメーションなど、視覚的なフィードバックを簡単に得ることが可能です。

Q&A

解決済

2回答

2427閲覧

【Processing】Boids(Flocking)プログラムにマウスクリックで方向ベクトルを与える

kyousukelin

総合スコア30

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Processing

Processingは、オープンソースプロジェクトによるCGのためのプログラミング言語です。Javaをベースにしており、グラフィック機能に特化しています。イメージの生成やアニメーションなど、視覚的なフィードバックを簡単に得ることが可能です。

0グッド

0クリップ

投稿2020/06/14 05:11

編集2020/06/14 05:22

Daniel Shiffmanによる,Processingで鳥の群れ行動をシミュレートするプログラムがあります(https://processing.org/examples/flocking.html).
このプログラムに「マウスでクリックした場所に群れを向かわせる」といった機能を搭載したいのですが,どうすれば良いのかわかりません.どのように改良すれば,実現することができるでしょうか.ヒントでも良いので教えていただきたいです.

java

1Flock flock; 2 3void setup() { 4 size(640, 360); 5 flock = new Flock(); 6 // Add an initial set of boids into the system 7 for (int i = 0; i < 150; i++) { 8 flock.addBoid(new Boid(width/2,height/2)); 9 } 10} 11 12void draw() { 13 background(50); 14 flock.run(); 15} 16 17// Add a new boid into the System 18void mousePressed() { 19 flock.addBoid(new Boid(mouseX,mouseY)); 20} 21 22 23 24// The Flock (a list of Boid objects) 25 26class Flock { 27 ArrayList<Boid> boids; // An ArrayList for all the boids 28 29 Flock() { 30 boids = new ArrayList<Boid>(); // Initialize the ArrayList 31 } 32 33 void run() { 34 for (Boid b : boids) { 35 b.run(boids); // Passing the entire list of boids to each boid individually 36 } 37 } 38 39 void addBoid(Boid b) { 40 boids.add(b); 41 } 42 43} 44 45 46 47 48// The Boid class 49 50class Boid { 51 52 PVector position; 53 PVector velocity; 54 PVector acceleration; 55 float r; 56 float maxforce; // Maximum steering force 57 float maxspeed; // Maximum speed 58 59 Boid(float x, float y) { 60 acceleration = new PVector(0, 0); 61 62 // This is a new PVector method not yet implemented in JS 63 // velocity = PVector.random2D(); 64 65 // Leaving the code temporarily this way so that this example runs in JS 66 float angle = random(TWO_PI); 67 velocity = new PVector(cos(angle), sin(angle)); 68 69 position = new PVector(x, y); 70 r = 2.0; 71 maxspeed = 2; 72 maxforce = 0.03; 73 } 74 75 void run(ArrayList<Boid> boids) { 76 flock(boids); 77 update(); 78 borders(); 79 render(); 80 } 81 82 void applyForce(PVector force) { 83 // We could add mass here if we want A = F / M 84 acceleration.add(force); 85 } 86 87 // We accumulate a new acceleration each time based on three rules 88 void flock(ArrayList<Boid> boids) { 89 PVector sep = separate(boids); // Separation 90 PVector ali = align(boids); // Alignment 91 PVector coh = cohesion(boids); // Cohesion 92 // Arbitrarily weight these forces 93 sep.mult(1.5); 94 ali.mult(1.0); 95 coh.mult(1.0); 96 // Add the force vectors to acceleration 97 applyForce(sep); 98 applyForce(ali); 99 applyForce(coh); 100 } 101 102 // Method to update position 103 void update() { 104 // Update velocity 105 velocity.add(acceleration); 106 // Limit speed 107 velocity.limit(maxspeed); 108 position.add(velocity); 109 // Reset accelertion to 0 each cycle 110 acceleration.mult(0); 111 } 112 113 // A method that calculates and applies a steering force towards a target 114 // STEER = DESIRED MINUS VELOCITY 115 PVector seek(PVector target) { 116 PVector desired = PVector.sub(target, position); // A vector pointing from the position to the target 117 // Scale to maximum speed 118 desired.normalize(); 119 desired.mult(maxspeed); 120 121 // Above two lines of code below could be condensed with new PVector setMag() method 122 // Not using this method until Processing.js catches up 123 // desired.setMag(maxspeed); 124 125 // Steering = Desired minus Velocity 126 PVector steer = PVector.sub(desired, velocity); 127 steer.limit(maxforce); // Limit to maximum steering force 128 return steer; 129 } 130 131 void render() { 132 // Draw a triangle rotated in the direction of velocity 133 float theta = velocity.heading2D() + radians(90); 134 // heading2D() above is now heading() but leaving old syntax until Processing.js catches up 135 136 fill(200, 100); 137 stroke(255); 138 pushMatrix(); 139 translate(position.x, position.y); 140 rotate(theta); 141 beginShape(TRIANGLES); 142 vertex(0, -r*2); 143 vertex(-r, r*2); 144 vertex(r, r*2); 145 endShape(); 146 popMatrix(); 147 } 148 149 // Wraparound 150 void borders() { 151 if (position.x < -r) position.x = width+r; 152 if (position.y < -r) position.y = height+r; 153 if (position.x > width+r) position.x = -r; 154 if (position.y > height+r) position.y = -r; 155 } 156 157 // Separation 158 // Method checks for nearby boids and steers away 159 PVector separate (ArrayList<Boid> boids) { 160 float desiredseparation = 25.0f; 161 PVector steer = new PVector(0, 0, 0); 162 int count = 0; 163 // For every boid in the system, check if it's too close 164 for (Boid other : boids) { 165 float d = PVector.dist(position, other.position); 166 // If the distance is greater than 0 and less than an arbitrary amount (0 when you are yourself) 167 if ((d > 0) && (d < desiredseparation)) { 168 // Calculate vector pointing away from neighbor 169 PVector diff = PVector.sub(position, other.position); 170 diff.normalize(); 171 diff.div(d); // Weight by distance 172 steer.add(diff); 173 count++; // Keep track of how many 174 } 175 } 176 // Average -- divide by how many 177 if (count > 0) { 178 steer.div((float)count); 179 } 180 181 // As long as the vector is greater than 0 182 if (steer.mag() > 0) { 183 // First two lines of code below could be condensed with new PVector setMag() method 184 // Not using this method until Processing.js catches up 185 // steer.setMag(maxspeed); 186 187 // Implement Reynolds: Steering = Desired - Velocity 188 steer.normalize(); 189 steer.mult(maxspeed); 190 steer.sub(velocity); 191 steer.limit(maxforce); 192 } 193 return steer; 194 } 195 196 // Alignment 197 // For every nearby boid in the system, calculate the average velocity 198 PVector align (ArrayList<Boid> boids) { 199 float neighbordist = 50; 200 PVector sum = new PVector(0, 0); 201 int count = 0; 202 for (Boid other : boids) { 203 float d = PVector.dist(position, other.position); 204 if ((d > 0) && (d < neighbordist)) { 205 sum.add(other.velocity); 206 count++; 207 } 208 } 209 if (count > 0) { 210 sum.div((float)count); 211 // First two lines of code below could be condensed with new PVector setMag() method 212 // Not using this method until Processing.js catches up 213 // sum.setMag(maxspeed); 214 215 // Implement Reynolds: Steering = Desired - Velocity 216 sum.normalize(); 217 sum.mult(maxspeed); 218 PVector steer = PVector.sub(sum, velocity); 219 steer.limit(maxforce); 220 return steer; 221 } 222 else { 223 return new PVector(0, 0); 224 } 225 } 226 227 // Cohesion 228 // For the average position (i.e. center) of all nearby boids, calculate steering vector towards that position 229 PVector cohesion (ArrayList<Boid> boids) { 230 float neighbordist = 50; 231 PVector sum = new PVector(0, 0); // Start with empty vector to accumulate all positions 232 int count = 0; 233 for (Boid other : boids) { 234 float d = PVector.dist(position, other.position); 235 if ((d > 0) && (d < neighbordist)) { 236 sum.add(other.position); // Add position 237 count++; 238 } 239 } 240 if (count > 0) { 241 sum.div(count); 242 return seek(sum); // Steer towards the position 243 } 244 else { 245 return new PVector(0, 0); 246 } 247 } 248} 249

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

ベストアンサー

面白がってやってみた。Boid::flock()をちょっといじる。

Processing

1 void flock(ArrayList<Boid> boids) { 2 PVector sep = separate(boids); // Separation 3 PVector ali = align(boids); // Alignment 4 PVector coh = cohesion(boids); // Cohesion 5 // Arbitrarily weight these forces 6 sep.mult(1.5); 7 ali.mult(1.0); 8 coh.mult(1.0); 9 // Add the force vectors to acceleration 10 applyForce(sep); 11 applyForce(ali); 12 applyForce(coh); 13 if(mousePressed){//マウスボタンが押されているときだけ 14 PVector toMouse=new PVector(mouseX,mouseY); 15 toMouse.sub(position);//方向ベクトルを求めて 16 toMouse.normalize(); 17 toMouse.mult(0.05);//影響の度合いを調整 18 applyForce(toMouse);//影響を与える 19 } 20 }

投稿2020/06/14 09:48

thkana

総合スコア7639

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

kyousukelin

2020/06/14 12:59

ありがとうございます!
guest

0

JVM で動作させてるものを全部 java と呼ぶのはいかがなものだろう。

とりあえず、ヒント

void mousePressed() { // ここをどこか別領域に保管する処理へ変更する flock.addBoid(new Boid(mouseX,mouseY)); }

クリック時点の座標は上記方法で取得できるようなので

最後にクリックした「mouseX,mouseY」をどこかに保管して移動処理を計算してる場所で利用しましょう。

投稿2020/06/14 05:42

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問