检查圆周运动的边缘
checking edge for circular movement
我想让我的点程序在到达边缘时转向
所以基本上我只是简单地计算
x = width/2+cos(a)*20;
y = height/2+sin(a)*20;
它做圆周运动。所以我想通过检查边缘来扭转局面。我也已经使用 println 命令确保 y 达到了 if 条件
class particles {
float x, y, a, r, cosx, siny;
particles() {
x = width/2; y = height/2; a = 0; r = 20;
}
void display() {
ellipse(x, y, 20, 20);
}
void explode() {
a = a + 0.1;
cosx = cos(a)*r;
siny = sin(a)*r;
x = x + cosx;
y = y + siny;
}
void edge() {
if (x>width||x<0) cosx*=-1;
if (y>height||y<0) siny*=-1;
}
}
//setup() and draw() function
particles part;
void setup(){
size (600,400);
part = new particles();
}
void draw(){
background(40);
part.display();
part.explode();
part.edge();
}
他们只是忽略了 if 条件
你的支票没有问题,问题在于这样一个事实,大概是下一次通过 draw()
你忽略了你通过重置 [=12= 的值来响应支票所做的事情] 和 siny
.
我建议创建两个新变量,dx
和 dy
("d" 代表 "direction"),它们将始终为 +1 和 -1,并更改 这些变量响应你的边缘检查。这是一个最小的例子:
float a,x,y,cosx,siny;
float dx,dy;
void setup(){
size(400,400);
background(0);
stroke(255);
noFill();
x = width/2;
y = height/2;
dx = 1;
dy = 1;
a = 0;
}
void draw(){
ellipse(x,y,10,10);
cosx = dx*20*cos(a);
siny = dy*20*sin(a);
a += 0.1;
x += cosx;
y += siny;
if (x > width || x < 0)
dx = -1*dx;
if (y > height || y < 0)
dy = -1*dy;
}
当您运行此代码时,您会观察到圆圈从边缘弹起:
我想让我的点程序在到达边缘时转向
所以基本上我只是简单地计算
x = width/2+cos(a)*20;
y = height/2+sin(a)*20;
它做圆周运动。所以我想通过检查边缘来扭转局面。我也已经使用 println 命令确保 y 达到了 if 条件
class particles {
float x, y, a, r, cosx, siny;
particles() {
x = width/2; y = height/2; a = 0; r = 20;
}
void display() {
ellipse(x, y, 20, 20);
}
void explode() {
a = a + 0.1;
cosx = cos(a)*r;
siny = sin(a)*r;
x = x + cosx;
y = y + siny;
}
void edge() {
if (x>width||x<0) cosx*=-1;
if (y>height||y<0) siny*=-1;
}
}
//setup() and draw() function
particles part;
void setup(){
size (600,400);
part = new particles();
}
void draw(){
background(40);
part.display();
part.explode();
part.edge();
}
他们只是忽略了 if 条件
你的支票没有问题,问题在于这样一个事实,大概是下一次通过 draw()
你忽略了你通过重置 [=12= 的值来响应支票所做的事情] 和 siny
.
我建议创建两个新变量,dx
和 dy
("d" 代表 "direction"),它们将始终为 +1 和 -1,并更改 这些变量响应你的边缘检查。这是一个最小的例子:
float a,x,y,cosx,siny;
float dx,dy;
void setup(){
size(400,400);
background(0);
stroke(255);
noFill();
x = width/2;
y = height/2;
dx = 1;
dy = 1;
a = 0;
}
void draw(){
ellipse(x,y,10,10);
cosx = dx*20*cos(a);
siny = dy*20*sin(a);
a += 0.1;
x += cosx;
y += siny;
if (x > width || x < 0)
dx = -1*dx;
if (y > height || y < 0)
dy = -1*dy;
}
当您运行此代码时,您会观察到圆圈从边缘弹起: