加工中的弹球
Bouncing ball in processing
正在尝试制作一个简单的弹跳球程序在处理中,但是无法去除球的重影。任何帮助将不胜感激,这是我的代码:
float g=-9.81;
float k=0.2; // Spring constant
float m=0.25; // Mass kg
float dt=0.01; // Time step 50ms
float vy=0; // Initial velocity
float y=0.95; // Initial position
float t=0; // Initial time
void setup()
{
size(500, 500);
}
void draw()
{
vy=vy+(g-((k/m)*vy))*dt;
if(y<0)
{
vy=-vy;
}
y=y+(vy*dt);
t=t+dt;
float sx=map(0.5,0,1,0,width);
float sy=map(y,0,1,height-1,0);
fill(255,0,0);
ellipse(sx,sy,10,10);
}
您永远不会清除旧框架,因此您绘制的任何内容都只是绘制在您之前绘制的任何内容之上。
如果您想清除旧帧,请在 draw()
函数的第一行添加对 background()
函数的调用。
更多信息可以在 the reference or in this tutorial 中找到关于 Processing 中的动画(免责声明:我写了那个教程)。
正在尝试制作一个简单的弹跳球程序在处理中,但是无法去除球的重影。任何帮助将不胜感激,这是我的代码:
float g=-9.81;
float k=0.2; // Spring constant
float m=0.25; // Mass kg
float dt=0.01; // Time step 50ms
float vy=0; // Initial velocity
float y=0.95; // Initial position
float t=0; // Initial time
void setup()
{
size(500, 500);
}
void draw()
{
vy=vy+(g-((k/m)*vy))*dt;
if(y<0)
{
vy=-vy;
}
y=y+(vy*dt);
t=t+dt;
float sx=map(0.5,0,1,0,width);
float sy=map(y,0,1,height-1,0);
fill(255,0,0);
ellipse(sx,sy,10,10);
}
您永远不会清除旧框架,因此您绘制的任何内容都只是绘制在您之前绘制的任何内容之上。
如果您想清除旧帧,请在 draw()
函数的第一行添加对 background()
函数的调用。
更多信息可以在 the reference or in this tutorial 中找到关于 Processing 中的动画(免责声明:我写了那个教程)。