互动媒体技术-代码本色-第2章

1.概述

力是一个向量,它使有质量的物体产生加速。

牛顿三大定律:
牛顿第一定律:物体有保持静止或运动的趋势。
牛顿第二定律:力等于质量乘以加速度。
牛顿第三定律:力总是成对出现,并且这两个力大小相等方向相反。

力的作用基于这三个定理实现。

2.拓展与应用

(1)纸飞机:风力+重力+摩擦力
纸飞机运动过程中受到风力、重力、摩擦力的作用。
重力大小由物体质量决定,风力大小统一,收到的摩擦力大大小受物体质量影响。
定义纸飞机:

class PaperPlane {
  PImage img;
  PVector position;
  PVector velocity;
  PVector acceleration;
  float mass;
  PaperPlane(float m, float x , float y) {
    mass = m;
    position = 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);
    position.add(velocity);
    acceleration.mult(0);
  }
  void display() {
    stroke(0);
    strokeWeight(2);
    fill(0,127);
    drawplane(position.x,position.y);
  } 
  void drawplane(float posx,float posy)
  {
    pushMatrix();
    img = loadImage("飞机.png");
    image(img,posx,posy,50,50);
    popMatrix();
  }
}

施加风力、重力、摩擦力

for (int i = 0; i < paperplanes.length; i++) {
    PVector wind = new PVector(0.5, -0.3);
    PVector gravity = new PVector(0, 0.1*paperplanes[i].mass);
    float c = 0.1;
    PVector friction = paperplanes[i].velocity.get();
    friction.mult(-1); 
    friction.normalize();
    friction.mult(c);
    paperplanes[i].applyForce(friction);
    paperplanes[i].applyForce(wind);
    paperplanes[i].applyForce(gravity);
    paperplanes[i].update();
    paperplanes[i].display();
  }

(2)小球:重力+流体阻力+弹力
受力分析:小球下落过程中持续受到重力,小球落入水中之后受到重力及流体阻力,小球接触底部是受到重力、流体阻力和弹力。
重力、流体阻力对位置的影响都是由牛顿第二定律计算加速度进而改变速度,从而改变小球位置,控制小球运动。
物体通过流体或者气体时同样会受摩擦力的作用,这种摩擦力有很多名字,如粘滞
力、阻力和流体阻力。
在这里插入图片描述
计算流体阻力:

//计算阻力
  PVector drag(Mover m) {
    //大小是系数*速度平方
    float speed = m.velocity.mag();
    float dragMagnitude = c * speed * speed;
    // 方向与速度相反
    PVector dragForce = m.velocity.get();
    dragForce.mult(-1);  
    dragForce.normalize();
    dragForce.mult(dragMagnitude);
    return dragForce;
  }

定义小球:

class Mover {
  PVector position;
  PVector velocity;
  PVector acceleration;
  float mass;//质量由大小决定
  Mover(float m, float x, float y) {
    mass = m;
    position = 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);
    position.add(velocity);
    // 我们必须清除每帧的加速度
    acceleration.mult(0);
  } 
  void display() {
    stroke(0);
    strokeWeight(2*2.25);
    fill(0,127,200);
    ellipse(position.x, position.y, mass*16, mass*16);
  }
  // 窗口底部弹性
  void checkEdges() {
    if (position.y > height) {
      velocity.y *= -0.7;  
      position.y = height;
    }
  }
}

施加重力、流体阻力

for (int i = 0; i < movers.length; i++) {  
    //是否在水里
    if (liquid.contains(movers[i])) {
      // 计算流体阻力
      PVector dragForce = liquid.drag(movers[i]);
      // 对物体施加流体阻力
      movers[i].applyForce(dragForce);
    }
    // 重力按质量缩放的
    PVector gravity = new PVector(0, 0.2*movers[i].mass);
    //施加重力
    movers[i].applyForce(gravity);  
    // 更新并显示
    movers[i].update();
    movers[i].display();
    movers[i].checkEdges();
  }

3.效果

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值