是什么导致 Mover 从屏幕上掉下来?
What is causing the Mover to fall off the screen?
添加 "force of gravity" 会导致对象最终消失
老实说我找不到错误。
推动者class
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();
}
我预计球最终会停在屏幕底部
我得到的是它最终消失了。
此外,使发帖更容易的新帮手让我对这个问题添加了更多内容,但我所说的就是我要说的全部内容
当检测到与地面或天花板的碰撞时,您必须将球的位置限制在 window:
的范围内
class Mover {
// [...]
void bounce() {
// [...]
if ((loc.y > height) || (loc.y < 0)) {
vel.y *= -1;
acc.y *= -1;
loc.y = loc.y > height ? height : 0; // limit y in the range [0, height]
}
}
}
由于重力不断加到加速度矢量上(b.gravity();
),如果位置在地面以下,球会略微去不到哪里。请注意,如果速度矢量 (vel
) 太小,无法将球举离地面,则条件 loc.y > height
再次满足,加速度再次转向 acc.y *= -1
。
一个选项是修复方法边缘:
class Mover {
// [...]
void edges() {
if (loc.x > width) {
loc.x = width;
} else if (loc.x < 0) {
loc.x = 0;
}
if (loc.y > height) {
loc.y = height;
} else if (loc.y < 0) {
loc.y = 0;
}
}
并在draw()中调用b.edges()
来限制小球的位置:
void draw() {
background(255);
b.wind(0.5);
b.gravity();
b.update();
b.bounce();
b.edges();
b.display();
}
添加 "force of gravity" 会导致对象最终消失
老实说我找不到错误。
推动者class
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();
}
我预计球最终会停在屏幕底部
我得到的是它最终消失了。
此外,使发帖更容易的新帮手让我对这个问题添加了更多内容,但我所说的就是我要说的全部内容
当检测到与地面或天花板的碰撞时,您必须将球的位置限制在 window:
的范围内class Mover {
// [...]
void bounce() {
// [...]
if ((loc.y > height) || (loc.y < 0)) {
vel.y *= -1;
acc.y *= -1;
loc.y = loc.y > height ? height : 0; // limit y in the range [0, height]
}
}
}
由于重力不断加到加速度矢量上(b.gravity();
),如果位置在地面以下,球会略微去不到哪里。请注意,如果速度矢量 (vel
) 太小,无法将球举离地面,则条件 loc.y > height
再次满足,加速度再次转向 acc.y *= -1
。
一个选项是修复方法边缘:
class Mover {
// [...]
void edges() {
if (loc.x > width) {
loc.x = width;
} else if (loc.x < 0) {
loc.x = 0;
}
if (loc.y > height) {
loc.y = height;
} else if (loc.y < 0) {
loc.y = 0;
}
}
并在draw()中调用b.edges()
来限制小球的位置:
void draw() {
background(255);
b.wind(0.5);
b.gravity();
b.update();
b.bounce();
b.edges();
b.display();
}