gpt4 book ai didi

javascript - 将 p5js 草图转换为处理的问题

转载 作者:行者123 更新时间:2023-11-30 00:04:28 25 4
gpt4 key购买 nike

我将一个 p5js scetch 翻译成 processing 因为我想生成一个 mov 文件。 scetch 基于 daniel shiffman 在 kadenze 上的教程,但我现在遇到了 boolean 输出的问题。 processing中这段p5js代码怎么写?

function draw() {
for( var i = 0; i < particles.length; i++) {
if(particles[i].isDead()) {
//code
}
}
}

function Particles() {
this.isDead = function() {
var distance = p5.Vector.sub(attractor.pos, this.pos);
var d = distance.mag();

if(d < 5) {
return true;
} else {
return false;
}
}
}

首先我用 void 试了一下,但是 void 没有输出。然后我用 boolean 值尝试了类似的东西,但它也不起作用。

void setup() {
//code
}

void draw () {
for (int i = 0; i < particles.length; i++) {
if(particles[i].isDead()) {
//code
}
}
}

Class Particle {
Particle() {
//code
}

boolean isDead() {
PVector distance = PVector.sub(a.location, location);
float d = distance.mag();

if(d < 5) {
return true;
} else {
return false;
}
}
}

如果有人能帮助我,那就太好了。

问候马蒂亚斯

最佳答案

第一个问题是,如果isDead(),你实际上并没有做任何事情。返回 true .您需要实际将代码放入 if 中声明,或者可能移动 if声明到某处具有逻辑意义。

例如,您可以修改 display()在你的内部运行 Mover Mover 时以绿色绘制的类死了:

  void display() {
if (isDead()) {
fill(0, 255, 0);
} else {
fill(255, 0, 0);
}
stroke(0);
strokeWeight(2);

ellipse(location.x, location.y, 10, 10);
}

这只是一个例子,您实际做什么取决于您希望在 Mover 时发生什么。死了。

但即使您进行了更改,您也会注意到 Mover只有当它到达灰色圆圈的中间时才会死亡。那是因为这个 if在你的 isDead() 里面声明功能:

PVector distance = PVector.sub(a.location, location);
float d = distance.mag();
if (d < 5) {
return true;
} else {
return false;
}

您正在测量灰色圆圈的中心与每个红色小圆圈的中心之间的距离。但你只返回 true如果d < 5 .问题在于灰色圆圈的直径为 50,因此如果您希望红色小圆圈在进入灰色圆圈时消失,则必须将该直径计入您的计算中。尝试使用 if(d < 30)反而。我用直径除以 30 50通过 2然后添加 5对于小圆圈大小。您可能必须使用它才能获得您想要的效果。

顺便说一下,您可能对 dist() 感兴趣函数,返回两点之间的距离。更多信息可以在 the reference 中找到.

关于javascript - 将 p5js 草图转换为处理的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39047288/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com