【发布时间】:2019-05-20 19:52:21
【问题描述】:
添加“重力”会导致物体最终消失
老实说,我找不到错误。
移动类
class Mover {
PVector acc;
PVector loc;
PVector vel;
Mover() {
loc = new PVector(width/2, height/2);
vel = new PVector(0, 0);
acc = new PVector(0, 0);
}
void update() {
// Mouse
//PVector mouse = new PVector(mouseX, mouseY);
//mouse.sub(loc);
//mouse.setMag(0.5);
//F = M * A
vel.add(acc);
loc.add(vel);
vel.limit(2);
}
void gravity() {
PVector grav = new PVector(0, 9.8);
acc.add(grav);
}
void wind(float wind_){
PVector wind = new PVector(wind_,0);
acc.add(wind);
}
void display() {
stroke(0);
fill(0, 255, 0);
ellipse(loc.x, loc.y, 20, 20);
}
void bounce() {
if ((loc.x > width) || (loc.x < 0)) {
vel.x *= -1;
acc.x *= -1;
}
if ((loc.y > height) || (loc.y < 0)) {
vel.y *= -1;
acc.y *= -1;
}
}
void edges() {
if (loc.x > width) {
loc.x = 0;
} else if (loc.x < 0) {
loc.x = width;
}
if (loc.y > height) {
loc.y = 0;
} else if (loc.y < 0) {
loc.y = height;
}
}
}
主文件
Mover b;
void setup() {
size(800, 600);
b = new Mover();
}
void draw() {
background(255);
b.gravity();
b.wind(0.5);
b.update();
b.bounce();
//b.edges();
b.display();
}
我希望球最终会停留在屏幕底部
我得到的是它最终会消失。
另外,使发帖更容易的新助手让我在这个问题上添加了更多内容,但我所说的实际上就是我要说的全部
【问题讨论】:
标签: java processing