椭圆 p5.js 边界内的粒子

如何解决椭圆 p5.js 边界内的粒子

我是粒子的新手,我已经设置了基本的粒子系统。

我希望它们保持在椭圆的边界内。那可能吗?因为我在网上找不到。

let particles = [];

function setup(){
  createCanvas(600,400);
}

function draw(){
  background(0);
  //how many new articles to add per frame (Now it's 5)
  for(let i=0; i<5;i++){
    let p = new Particle();
    //push will add a new Particle to the array of particles
    particles.push(p);
  }
  //backwards through the array because otherwise the particles will turn on and off because of the finished()
  //this will also keep the same amount of particles in the sketch.
  for(let i = particles.length-1; i>=0; i--){
    particles[i].update();
    particles[i].show();
    if(particles[i].finished()){
      //splice removes this particle from the array on position i.
      particles.splice(i,1);
    }
  }
}

class Particle {
  constructor(){
    //Want the particles to start at the bottom
    this.x = 300;
    this.y = 380;
    // random veLocity
    this.vx = random(-1,1);
    //random veLocity upwards
    this.vy = random(-5,-1);
    //give particles transparancy
    this.alpha = 255;
  }

  update(){
    //change the location to some random amount
    this.x += this.vx;
    this.y += this.vy;
    //it will lose some alpha with each frame
    this.alpha-=5;
  }

  finished(){
    //see if the alpha became 0 or less,then return true or false.
    return this.alpha < 0;
  }

  show() {
    nostroke();
    // stroke(255);
    fill(255,this.alpha);
    //here you can load images which will create a more interesting visual effect
    ellipse(this.x,this.y,16,16);
  }
}

我想学习如何创建与此处 https://www.patrik-huebner.com/ideas/ex8/ 数字 8 上的第二个草图类似的效果(它从一个粒子开始,然后爆发为多个。)我不想复制作品,只是出于个人兴趣了解它是如何工作的。

解决方法

如果您希望粒子在离开圆时消失,您可以使用 update() 语句将其放入 if 方法中:

update(){
  if (dist(startPoint.x,startPoint.y,this.x,this.y) > radius) {
    this.alpha = 0;
  }
  ...
}

finished() 方法将负责将它们从数组中移除。

如果您只想让它们在到达边缘时停止(这具有非常相似的效果),您可以更改 update() 方法,使其仅移动处于适当半径内的粒子:

update(){
  if (dist(startPoint.x,this.y) < radius) {
    //change the location to some random amount
    this.x += this.vx;
    this.y += this.vy;
  }
  //it will lose some alpha with each frame
  this.alpha-=5;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?