这里是一个程序,在鼠标点击的任何地方都有一个球落下和反弹。谁知道如何改变球下落到重力的速度? 我试图找出正确的解决方案......但我遇到了一点麻烦。非常感谢所有帮助和/或输入。
float x;
float y;
float yspeed = 0;
float xspeed = 0;
float balldiameter = 10;
float ballradius = balldiameter/2;
void setup() {
size (400,400);
background (255);
fill (0);
ellipseMode(CENTER);
smooth();
noStroke();
x = width/2;
y = height/2;
}
void draw() {
mouseChecks();
boundaryChecks();
ballFunctions();
keyFunctions();
}
void mouseChecks() {
if (mousePressed == true) {
x = mouseX;
y = mouseY;
yspeed = mouseY - pmouseY;
xspeed = mouseX - pmouseX;
}
}
void boundaryChecks() {
if (y >= height - ballradius) {
y = height - ballradius;
yspeed = -yspeed/1.15;
}
if (y <= ballradius) {
y = ballradius;
yspeed = -yspeed/1.35;
}
if (x >= width -ballradius) {
x = width -ballradius;
xspeed = -xspeed/1.10;
}
if (x <= ballradius) {
x = ballradius;
xspeed = -xspeed/1.10;
}
}
void ballFunctions() {
if (balldiameter < 2) {
balldiameter = 2;
}
if (balldiameter > 400) {
balldiameter = 400;
}
ballradius = balldiameter/2;
background(255); //should this be in here?
ellipse (x,y,balldiameter,balldiameter);
yspeed = yspeed += 0.2;
xspeed = xspeed/1.005;
y = y + yspeed;
x = x + xspeed;
}
void keyFunctions() {
if (keyPressed) {
if(keyCode == UP) {
balldiameter +=1;
}
if (keyCode == DOWN) {
balldiameter -=1;
}
}
}
最佳答案
地球上的重力加速度为 9.81 m/s^2。因此,如果在单击鼠标时球的速度为 0,则最终速度将是与时间相关的加速度积分。这将是 (9.81 * t)/2。其中 t 以秒为单位。结果单位为米/秒。您必须将米转换为一些屏幕空间单位才能进行绘图。
关于java - 改变落球速度...处理中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13205534/