こんばんは。
初めてのprocessingで困惑しているのですが質問よろしいでしょうか?
int numBalls = 12;
float spring = 0.05;
float gravity = 0.03;
float friction = -0.9;
Ball[] balls = new Ball[numBalls];
void setup() {
size(940, 560);
for (int i = 0; i < numBalls; i++) {
balls = new Ball(random(width), random(height), random(30, 70), i, balls);
}
noStroke();
fill(255, 204);
}
void draw() {
background(0);
for (int i = 0; i < numBalls; i++) {
balls.collide();
balls.move();
balls.display();
}
}
class Ball {
float x, y;
float diameter;
float vx = 0;
float vy = 0;
int id;
Ball[] others;
Ball(float xin, float yin, float din, int idin, Ball[] oin) {
x = xin;
y = yin;
diameter = din;
id = idin;
others = oin;
}
void collide() {
for (int i = id + 1; i < numBalls; i++) {
float dx = others.x - x;
float dy = others.y - y;
float distance = sqrt(dx*dx + dy*dy);
float minDist = others.diameter/2 + diameter/2;
if (distance < minDist) {
float angle = atan2(dy, dx);
float targetX = x + cos(angle) * minDist;
float targetY = y + sin(angle) * minDist;
float ax = (targetX - others.x) * spring;
float ay = (targetY - others.y) * spring;
vx -= ax;
vy -= ay;
others.vx += ax;
others[i].vy += ay;
}
}
}
void move() {
vy += gravity;
x += vx;
y += vy;
if (x + diameter/2 > width) {
x = width - diameter/2;
vx *= friction;
}
else if (x - diameter/2 < 0) {
x = diameter/2;
vx *= friction;
}
if (y + diameter/2 > height) {
y = height - diameter/2;
vy *= friction;
}
else if (y - diameter/2 < 0) {
y = diameter/2;
vy *= friction;
}
}
void display() {
ellipse(x, y, diameter, diameter);
}
}
このコードの丸(オブジェクト)に背景が透明な.pngの画像を当て込むにはどうしたらよいでしょうか?
Processingについて
- softya(ソフト屋)
- 副管理人
- 記事: 11677
- 登録日時: 15年前
- 住所: 東海地方
- 連絡を取る:
Re: Processingについて
codeタグをご利用下さい。コードが読みやすくなります。
http://dixq.net/board/board.html
その他のことも含めてフォーラムルールに目を通しておいて下さい。
http://dixq.net/board/board.html
その他のことも含めてフォーラムルールに目を通しておいて下さい。
しろしろ さんが書きました:こんばんは。
初めてのprocessingで困惑しているのですが質問よろしいでしょうか?
このコードの丸(オブジェクト)に背景が透明な.pngの画像を当て込むにはどうしたらよいでしょうか?int numBalls = 12; float spring = 0.05; float gravity = 0.03; float friction = -0.9; Ball[] balls = new Ball[numBalls]; void setup() { size(940, 560); for (int i = 0; i < numBalls; i++) { balls[i] = new Ball(random(width), random(height), random(30, 70), i, balls); } noStroke(); fill(255, 204); } void draw() { background(0); for (int i = 0; i < numBalls; i++) { balls[i].collide(); balls[i].move(); balls[i].display(); } } class Ball { float x, y; float diameter; float vx = 0; float vy = 0; int id; Ball[] others; Ball(float xin, float yin, float din, int idin, Ball[] oin) { x = xin; y = yin; diameter = din; id = idin; others = oin; } void collide() { for (int i = id + 1; i < numBalls; i++) { float dx = others[i].x - x; float dy = others[i].y - y; float distance = sqrt(dx*dx + dy*dy); float minDist = others[i].diameter/2 + diameter/2; if (distance < minDist) { float angle = atan2(dy, dx); float targetX = x + cos(angle) * minDist; float targetY = y + sin(angle) * minDist; float ax = (targetX - others[i].x) * spring; float ay = (targetY - others[i].y) * spring; vx -= ax; vy -= ay; others[i].vx += ax; others[i].vy += ay; } } } void move() { vy += gravity; x += vx; y += vy; if (x + diameter/2 > width) { x = width - diameter/2; vx *= friction; } else if (x - diameter/2 < 0) { x = diameter/2; vx *= friction; } if (y + diameter/2 > height) { y = height - diameter/2; vy *= friction; } else if (y - diameter/2 < 0) { y = diameter/2; vy *= friction; } } void display() { ellipse(x, y, diameter, diameter); } }
by softya(ソフト屋) 方針:私は仕組み・考え方を理解して欲しいので直接的なコードを回答することはまれですので、すぐコードがほしい方はその旨をご明記下さい。私以外の方と交代したいと思います(代わりの方がいる保証は出来かねます)。
- softya(ソフト屋)
- 副管理人
- 記事: 11677
- 登録日時: 15年前
- 住所: 東海地方
- 連絡を取る:
Re: Processingについて
こういうのはリファレンスをまず見ましょう。
「Processingクイックリファレンス」
http://www.musashinodenpa.com/p5/
ここにあるPImage 画像を扱うクラス で出来るのではないでしょうか?
「Processingクイックリファレンス」
http://www.musashinodenpa.com/p5/
ここにあるPImage 画像を扱うクラス で出来るのではないでしょうか?
by softya(ソフト屋) 方針:私は仕組み・考え方を理解して欲しいので直接的なコードを回答することはまれですので、すぐコードがほしい方はその旨をご明記下さい。私以外の方と交代したいと思います(代わりの方がいる保証は出来かねます)。