raven讲了个球运动的例子,但没说怎么停在地上,所以自己琢磨着写了一段很初级的程序,有点繁琐,不过总算是让球停了下来,小有成果
float xPos;
float yPos;
float increx = 5;
float increy = 5;
float gravity = 0.5;
float friction = 0.02;
void setup() {
smooth();
fill(148, 65, 222);
size(500, 500);
frameRate(30);
yPos = height/2;
xPos = width/3;
}
void draw() {
background(255);
ellipse(xPos, yPos, 20, 20);
xPos+=increx;
yPos+=increy;
if (xPos>width-10) {
xPos = width-10;
increx = -increx*0.9;
}
else if (xPos<10) {
xPos = 10;
increx = - increx*0.9;
}
if ( yPos>height-10) {
yPos=height-10;
increy = -increy*0.9;
}
else if (yPos<10) {
yPos = 10;
increy = -increy*0.9;
}
if (yPos == height-10) {
if (increx >0){
increx =increx-friction;
}
if(increx <0){
increx =increx +friction;
}
if (increx*friction == 0)
increx =0;
}
increy += gravity;
}