美文网首页
processing的力模拟-随机弹跳的球

processing的力模拟-随机弹跳的球

作者: 思求彼得赵 | 来源:发表于2022-05-03 23:57 被阅读0次

2022-05-03

铺垫:加速度、速度、行程的关系基本的原理,在这个例子里展示完整。

  • Shiffman, Daniel. Learning Processing: a beginner's guide to programming images, animation, and interaction. Morgan Kaufmann, 2009. P89
float x = 100; // x location of square
float y = 0; // y location of square
float speed = 0; // speed of square
float gravity = 0.1;
void setup() {
size(200, 200);
}
void draw() {
background(255);
// Draw the ball
fill(0);
noStroke();
ellipse(x, y, 10, 10);
y = y + speed;
speed = speed + gravity;
// Bounce back up!
if (y > height) {
speed = speed * -0.85;
y = height;
}
}
image.png

以下是在其代码本色的书的例子。

  1. 制作一个弹球的类Mover:
class Mover {
PVector location;
PVector velocity;
PVector acceleration;
float mass;

Mover(float m, float x,float y){
 mass=m;
 location=new PVector(x, y);
 velocity=new PVector(0,0);
 acceleration=new PVector(0,0);
}

void applyForce(PVector force){
  PVector f=PVector.div(force, mass);
  acceleration.add(f);
}

void update(){
  velocity.add(acceleration);
  location.add(velocity);
  
  acceleration.mult(0); // clear everytime!!
}


void display(){
  stroke(0);
  fill(175);
  ellipse(location.x, location.y, mass*16, mass*16);
}

void checkEdges(){
  if(location.x>width){
    location.x=width;
    velocity.x*=-1;
  }else if (location.x<0){
    velocity.x*=-1;
    location.x=0;
  
  }
  
  if(location.y>height){
    velocity.y*=-1;
    location.y=height;
  }

}
}
  1. 在主程序中调用这个类生成的对象。注意是多个对象。
Mover[] movers=new Mover[10];

void setup(){
  size(600,400);
  for (int i=0; i<movers.length; i++){
    movers[i]= new Mover(random(0.1,5),0,0);
  }
}

void draw(){
  background(255);
  PVector wind= new PVector(0.01,0);
  PVector gravity= new PVector(0,0.1);
  
  for(int i=0; i<movers.length; i++){
    movers[i].applyForce(wind);
    movers[i].applyForce(gravity);
    movers[i].update();
    movers[i].display();
    movers[i].checkEdges();
  }
}

生成的效果这里就不展示了。可参考the nature of code第二章。

相关文章

  • processing的力模拟-随机弹跳的球

    2022-05-03 铺垫:加速度、速度、行程的关系基本的原理,在这个例子里展示完整。 Shiffman, Dan...

  • 桌球小游戏项目3.0

    运行结果: 随机动,碰壁会回头。模拟真球的效果。

  • Processing——练习记录,转动的线段

    为了编写”小贱钟“PlotClock的模拟程序,使用了Processing。学习到了Processing的另一种领...

  • 美国著名弹跳训练计划

    跳跃力&跳高训练计划 一、 弹跳力训练 美国最著名弹跳训练计划 , 练成预计弹跳能力可以提高 20 到 30 公分...

  • 看的见的音乐——Processing生成艺术

    今天无意间看到一个Processing的视频,感觉将Processing的一些基本图形功能旋转、随机、图像处理、生...

  • 随机模拟

    我们为什么要进行模拟? 一般情况下样本都是从真实世界中采样得到的,虽然我们事先不知道真实世界的概率分布是多少,但是...

  • Flink Time

    Flink 时间语义 Processing Time: 模拟我们真实世界的时间Process Time 是通过直接...

  • 乒乓球弹跳实验

    实验材料:乒乓球、卷尺 实验步骤:把乒乓球放在不同高度(卷尺测量),然后松手放下,实验乒乓球弹起高度。 实验现象:...

  • MCMC和Gibbs Sampling

    MCMC和Gibbs Sampling 1.随机模拟 随机模拟又名蒙特卡罗方法,蒙特卡罗方法的源头就是当年用...

  • 坚持比努力可怕

    做梦了。梦到校园,篮球场上,比赛。 我高高跳起来,接到球投篮! 其实自己的弹跳力还是很好,身体灵活,动作敏捷。这是...

网友评论

      本文标题:processing的力模拟-随机弹跳的球

      本文链接:https://www.haomeiwen.com/subject/aqfwyrtx.html