【问题标题】:PIXI.js particles clumping in the corner. Can't figure it outPIXI.js 粒子聚集在角落里。想不通
【发布时间】:2020-08-09 12:26:07
【问题描述】:

所以,我正在尝试在 Angular 9 中使用 PIXI.js 创建一个粒子网络。 这是我注入根的服务:

import { Injectable } from '@angular/core';
import * as PIXI from 'pixi.js'

class Particle {
  x: number;
  y: number;
  xDirection: number;
  yDirection: number;
  radius: number;
  graphicCorespondent: PIXI.Graphics;
  
  constructor(){
    this.x = Math.random() * window.innerWidth;
    this.y = Math.random() * window.innerHeight;
    this.xDirection = Math.random();
    this.xDirection *= Math.ceil(Math.random()*2) == 2 ? 1 : -1;
    this.yDirection = Math.random();
    this.yDirection *= Math.ceil(Math.random()*2) == 2 ? 1 : -1;
    // this.xDirection = 1;
    // this.yDirection = 1;
    this.radius = 1 + Math.random() * 3;
  }
}

@Injectable({
  providedIn: 'root'
})

export class ParticlesService {
  app = new PIXI.Application({width: window.innerWidth, height: window.innerHeight});
  density = 3000;
  particles = [];
  velocity = 1;

  constructor() {
    window.onload = () => {
      document.body.appendChild(this.app.view);
      this.app.renderer.backgroundColor = 0xFFFFFF;
      this.app.renderer.view.style.position = "fixed";
      this.app.renderer.view.style.display = "block";
      this.app.renderer.view.style.left = '0';
      this.app.renderer.view.style.top = '0';
      this.app.renderer.view.style.zIndex = '-1';
      // window.onresize = () => {
      //   this.app.renderer.resize(window.innerWidth, window.innerHeight)
      // }
    }
  }

  init(){
    for(let i=0; i<this.density; i++){
      const particle = new Particle();
      const gr = new PIXI.Graphics();
      gr.beginFill(0x000000);
      gr.drawCircle(particle.x, particle.y, particle.radius);
      gr.endFill();

      particle.graphicCorespondent = gr;
      this.particles.push(particle)

      this.app.stage.addChild(gr)
    }
    this.app.ticker.add((delta) => {
      this.animate(delta);
    });
    this.app.ticker.start();
  }

  animate(delta){
    this.particles.forEach((particle, index) => {
      
      if(particle.x > window.innerWidth) particle.x = 0;  
      if(particle.y > window.innerHeight) particle.y = 0;  
      if(particle.x < 0) particle.x = window.innerWidth;  
      if(particle.y < 0) particle.y = window.innerHeight; 

      particle.graphicCorespondent.x = particle.x + delta * particle.xDirection;
      particle.graphicCorespondent.y = particle.y + delta * particle.yDirection;

      particle.x = particle.graphicCorespondent.x;
      particle.y = particle.graphicCorespondent.y;
      
    })
    this.app.render()
  }
}

我设法做到了这一点,它可以工作....但粒子往往每次都聚集在右下角。

如果我去掉“边缘碰撞检测”:

if(particle.x > window.innerWidth) particle.x = 0;  
if(particle.y > window.innerHeight) particle.y = 0;  
if(particle.x < 0) particle.x = window.innerWidth;  
if(particle.y < 0) particle.y = window.innerHeight; 

它似乎工作正常,但粒子会飞到无穷远。 否则......它们往往会聚集在右下角,我不明白为什么。

Screenshot of the clumping

我在这个上浪费了一整天,但我仍然不明白为什么会这样。 我使用 CLI 创建了一个全新的 Angular 应用程序,我创建的唯一东西就是这个服务。结果相同。

&lt;meta name="viewport" content="width=device-width, initial-scale=1"&gt; 存在。

重新缩放似乎有效。

宽度和高度是正确的。

particle.x 和particle.graphicCorespondent.x 在刻度结束时相等。

此外,粒子似乎在大部分时间到达边缘之前消失并重新出现。

如果有人能看一下这个,我将非常感激。快把我逼疯了。

我尝试在粒子离开屏幕后对其进行切片并创建另一个粒子。它会创建另一个,但之后似乎是静态的。

我尝试直接循环通过stage.children,结果相同。

还尝试通过访问particle.graphicCorespondent 上的getBounds() 方法来读取粒子的x 和y 值。相同的值,不相关。

编辑:我在 Chrome 和 Firefox 中得到相同的结果。

PIXI.js 版本:5.2.3

Angular 版本:9.1.1

【问题讨论】:

  • 你可以尝试将粒子数(“密度”)减少到 1 并观察这个单个粒子的行为吗?
  • 就这么做了。画布似乎是空的,而只有一个存在。只有当我将密度提高到 10 时,才会出现一些粒子。所以也许画布比它显示的要大?但为什么?怎么样?
  • 另外,我让应用程序在一夜之间运行。似乎它们都会留在屏幕上,只是右下角会出现更多的粒子。
  • 好的 - 所以我认为你应该专注于分析单个粒子的行为。如果它没有在屏幕上显示 1 个粒子,那么确实可能设置不正确 - 例如画布与窗口大小等。
  • 也尝试从简单的东西开始 - 例如:尝试在屏幕中间和每个角落显示静态(不移动)粒子 - 这应该可以指示屏幕/画布的​​情况。

标签: javascript angular typescript pixi.js


【解决方案1】:

您的错误在于您绘制精灵的方式。具体来说......
gr.drawCircle(particle.x, particle.y, particle.radius);
您的说法是在 x 和 y 处以这样的半径画一个圆。此绘制操作将相对于 gr 的 x,y 进行。因此,稍后当您在画布上绘制精灵时,该点将从 gr 的 x 和 y 偏移。
所以你真正想要的是....
gr.drawCircle(0, 0, particle.radius);
这是一个工作示例供您查看....
https://codesandbox.io/s/so-pixijs-particles-clumping-in-the-corner-cant-figure-it-out-tsccz?file=/index.js

【讨论】:

  • 请在未来制作工作示例。你很幸运,它让我发疯了,当一切看起来都正确时,我看不到错误,我必须做一个工作示例来解决它;)但我不会总是依赖我的强迫性:P 希望它能给你带来一些平静,因为好吧。
  • 但是 x 和 y 不是圆的位置吗?我需要在哪里绘制?我还以为是圆的起源呢。
  • codesandbox.io/s/… 我让它只在 0,0 处绘制一个图形,但在 200,200 处绘制了圆,并注意圆在 200,200 处。现在您将 x,y 更改为 100,0 并注意圆圈向右移动 100。这表明您给出的绘制命令是相对于其父级(图形)x,y。我刚刚检查了文档(我只学习了几天),它并没有说明我能看到的任何地方。可能会看看这是否可以添加到文档中,因为它也会混淆其他人。
  • 我回答这个问题的假设是它渲染了图形并显示了它,而事实是它相对于 x,y 的每一帧都绘制它。我会尽快更新我的答案以反映这一点,我只想先看几件事。哦,我更新了这个例子,甚至添加了一些调整大小的东西,因为它在那里但被注释掉了,以防万一它有帮助。
猜你喜欢
  • 2017-06-30
  • 1970-01-01
  • 1970-01-01
  • 2014-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-10
  • 1970-01-01
相关资源
最近更新 更多