- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图让我的粒子系统一个接一个地生成粒子,而不是同时生成所有粒子。我的代码目前将立即生成所有 100 个粒子。
我没有尝试太多,因为我是编码新手。
我有一个设置,可以调用并更新我的粒子类,以及一个包含粒子系统所有参数的类。
int num = 100;
Particle[] p = new Particle[num];
void setup() {
size(1080, 720);
colorMode(HSB);
for (int i = 0; i < num; i ++) {
p[i] = new Particle(new PVector(random(width), random(height)), 100, 150);
}
stroke(255);
}
void draw() {
background(0);
for (int i = 0; i < num; i ++) {
p[i].update(p, i);
}
}
class Particle {
PVector pos;
PVector vel;
float r, mr;
float spd = 0.1;
float max = 2;
Particle(PVector pos, float r, float mr) {
this.pos = pos;
this.r = r;
this.mr = mr;
vel = new PVector(random(-1, 1), random(-1, 1));
}
void update(Particle[] p, int i) {
float h = map(mouseX, 0, width, 0, 255);
pos.add(vel);
if (pos.x < -10) pos.x = width;
if (pos.x > width + 10) pos.x = 0;
if (pos.y < -10) pos.y = height;
if (pos.y > height + 10) pos.y = 0;
vel.x = constrain(vel.x + random(-spd, spd), -max, max);
vel.y = constrain(vel.y + random(-spd, spd), -max, max);
for (int j = i + 1; j < p.length; j ++) {
float ang = atan2(pos.y - p[j].pos.y, pos.x - p[j].pos.x);
float dist = pos.dist(p[j].pos);
if (dist < r) {
stroke(h, 255, map(dist, 0, r, 255, 0));
strokeWeight(map(dist, 0, r, 3, 0));
line(pos.x, pos.y, p[j].pos.x, p[j].pos.y);
float force = map(dist, 0, r, 4, 0);
vel.x += force * cos(ang);
vel.y += force * sin(ang);
}
}
float ang = atan2(pos.y - mouseY, pos.x - mouseX);
float dist = pos.dist(new PVector(mouseX, mouseY));
if (dist < r) {
stroke(0, 0, map(dist, 0, r, 255, 0));
strokeWeight(map(dist, 0, r, 3, 0));
line(pos.x, pos.y, mouseX, mouseY);
float force = map(dist, 0, r, 30, 0);
vel.x += force * cos(ang);
vel.y += force * sin(ang);
}
noStroke();
fill(h, 255, 255);
ellipse(pos.x, pos.y, 5, 5);
}
}
最佳答案
创建 ArrayList
粒子,但不要在 setup()
中添加任何粒子:
ArrayList<Particle> paticles = new ArrayList<Particle>();
void setup() {
size(400, 400);
colorMode(HSB);
stroke(255);
}
在draw()
中连续添加粒子。函数millis()
用于获取自程序启动以来的时间:
void draw() {
int num = 100;
int interval = 100; // 0.5 seconds
int time = millis(); // milliseconds since starting the program
if (paticles.size() < num && paticles.size()*interval+5000 < time) {
paticles.add(new Particle(new PVector(random(width), random(height)), 100, 150));
}
background(0);
for (int i = 0; i < paticles.size(); i ++) {
Particle p = paticles.get(i);
p.update(paticles, i);
}
}
注意,Particle
类必须进行适配,因为它必须操作可变长度的ArrayList
而不是固定长度的数组:
class Particle {
PVector pos;
PVector vel;
float r, mr;
float spd = 0.1;
float max = 2;
Particle(PVector pos, float r, float mr) {
this.pos = pos;
this.r = r;
this.mr = mr;
vel = new PVector(random(-1, 1), random(-1, 1));
}
void update(ArrayList<Particle> paticles, int i) {
float h = map(mouseX, 0, width, 0, 255);
pos.add(vel);
if (pos.x < -10) pos.x = width;
if (pos.x > width + 10) pos.x = 0;
if (pos.y < -10) pos.y = height;
if (pos.y > height + 10) pos.y = 0;
vel.x = constrain(vel.x + random(-spd, spd), -max, max);
vel.y = constrain(vel.y + random(-spd, spd), -max, max);
for (int j = i + 1; j < paticles.size(); j ++) {
Particle pj = paticles.get(j);
float ang = atan2(pos.y - pj.pos.y, pos.x - pj.pos.x);
float dist = pos.dist(pj.pos);
if (dist < r) {
stroke(h, 255, map(dist, 0, r, 255, 0));
strokeWeight(map(dist, 0, r, 3, 0));
line(pos.x, pos.y, pj.pos.x, pj.pos.y);
float force = map(dist, 0, r, 4, 0);
vel.x += force * cos(ang);
vel.y += force * sin(ang);
}
}
float ang = atan2(pos.y - mouseY, pos.x - mouseX);
float dist = pos.dist(new PVector(mouseX, mouseY));
if (dist < r) {
stroke(0, 0, map(dist, 0, r, 255, 0));
strokeWeight(map(dist, 0, r, 3, 0));
line(pos.x, pos.y, mouseX, mouseY);
float force = map(dist, 0, r, 30, 0);
vel.x += force * cos(ang);
vel.y += force * sin(ang);
}
noStroke();
fill(h, 255, 255);
ellipse(pos.x, pos.y, 5, 5);
}
}
关于java - 加工。粒子系统-如何让粒子一颗一颗进来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58385210/
例如,如果我想记录我按下了 b 键并想记录按下该键的时间长度,我该怎么做? 最佳答案 处理起来 super 简单。看看keyPressed()和 keyReleased() . // create a
我正在为编程课创建一个突破游戏。 import processing.serial.*; //sets the conditions for winning int lose = 1; boolean
有人可以告诉我为什么我会收到此错误吗?基本上这是一个我想模拟基本植物生长的程序。我想以这样的方式做到这一点:花瓣都存储在一个圆圈数组中。 Stem myStem; Circle circles; fl
我想知道如何更改以下代码,以便将形成花瓣的圆圈存储在数组中。所以我可以在另一个函数中使用它。 void setup() { size(400, 400); smooth(
我编写了这段代码来创建速写本。 我确定这是一个简单的错误,但为什么它不会在图像结束时停止播放? 这是代码 import ddf.minim.spi.*; import ddf.minim.si
我的start-yarn.sh无法启动resourcemanager和nodemanager进程 如前所述,我在yarn-env.sh脚本中设置了这些选项。 export YARN_RESOURCEM
我试图让我的粒子系统一个接一个地生成粒子,而不是同时生成所有粒子。我的代码目前将立即生成所有 100 个粒子。 我没有尝试太多,因为我是编码新手。 我有一个设置,可以调用并更新我的粒子类,以及一个包含
我正在尝试为这个程序制作一个重置按钮,我到处都找过了,但找不到有效的方法。我试图在游戏结束时,当你输了时,你按“重新启动”,游戏将从草图的开头开始。该程序工作正常,但一旦按下“重新启动”按钮,我会在控
我有一个移动图像作为背景 PImage background; int x=0; //global variable background location rocket Rocket; void s
我有一个移动图像作为背景 PImage background; int x=0; //global variable background location boolean up; boolean d
我的 Enemy 变量在 GameScene 函数中不“存在”。我正在尝试制作一个正在处理的游戏,但由于某种原因,当我尝试访问一个类时,我收到错误消息“x 无法解析或不是一个字段”,我将在下面发布我的
我正在创建一个程序,通过采用以下输入来计算一顿饭的总费用:meal_cost,tax_rate,tip_rate,number_eating 并通过函数调用将它们打印在字符串中。我查看了 StackO
首先,本文不涉及表单验证(javascript),而是有关数据库约束的验证。 当我通过添加表单添加数据时,由于我在创建数据库时已初始化的数据库限制(例如复制主键),在某些情况下数据库将不会更新。在这种
我是一名优秀的程序员,十分优秀!