转向力

2022-06-28  本文已影响0人  大龙10

书名:代码本色:用编程模拟自然系统
作者:Daniel Shiffman
译者:周晗彬
ISBN:978-7-115-36947-5
第6章目录

6.3 转向力

1、转向力的概念

  为了更好地理解自治智能体,我们要先了解转向力的概念。
  思考以下场景:一辆移动的小车正在寻找一个目标。

图6-1
转向力 = 所需速度 - 当前速度
PVector steer = PVector.sub(desired, velocity);

在上面的公式中,当前速度是已知的,但所需速度仍要通过计算得到。
参考图6-2,如果小车的最终目的是“寻找目标位置”,那么所需速度就是由当前位置指向目标位置的向量。

图6-2
PVector desired = PVector.sub(target, location);

但这并不符合实际情况,如果屏幕的分辨率非常高,两者之间的距离为几千像素,那么小车的移动速度会非常快,最后无法得到合理的动画效果。

class Vehicle {
      PVector location;
      PVector velocity;
      PVector acceleration;
      float maxspeed; 最大速率
     PVector desired = PVector.sub(target,location);
     desired.normalize();
     desired.mult(maxspeed);
图6-3
void seek(PVector target) {
      PVector desired = PVector.sub(target,location);
      desired.normalize();
      desired.mult(maxspeed); 计算所需速度,让它的大小等于最大速率
      PVector steer = PVector.sub(desired, velocity); Reynolds的转向力公式
      applyForce(steer); 使用我们之前的物理模型,将力变为对象的加速度
}

2、为什么上面的代码能正常工作?

让我们结合小车的自身状态和目标位置分析转向力的原理,请看图6-4。


图6-4
class Vehicle {
      PVector location;
      PVector velocity;
      PVector acceleration;
      float maxspeed; 最大速率
      float maxforce; 还有一个最大转向力
      void seek(PVector target) {
          PVector desired = PVector.sub(target,location);
          desired.normalize();
          desired.mult(maxspeed);
          PVector steer = PVector.sub(desired,velocity);
          steer.limit(maxforce); 限制转向力的大小
          applyForce(steer);
      }

3、示例代码6-1 寻找目标

Vehicle v;

void setup() {
  size(640, 360);
  v = new Vehicle(width/2, height/2);
}

void draw() {
  background(255);

  PVector mouse = new PVector(mouseX, mouseY);

  // Draw an ellipse at the mouse position
  fill(200);
  stroke(0);
  strokeWeight(2);
  ellipse(mouse.x, mouse.y, 48, 48);

  // Call the appropriate steering behaviors for our agents
  v.seek(mouse);
  v.update();
  v.display();
}

Vehicle.pde

class Vehicle {
  
  PVector position;
  PVector velocity;
  PVector acceleration;
  float r;
  float maxforce;    // Maximum steering force
  float maxspeed;    // Maximum speed

  Vehicle(float x, float y) {
    acceleration = new PVector(0,0);
    velocity = new PVector(0,-2);
    position = new PVector(x,y);
    r = 6;
    maxspeed = 4;
    maxforce = 0.1;
  }

  // Method to update position
  void update() {
    // Update velocity
    velocity.add(acceleration);
    // Limit speed
    velocity.limit(maxspeed);
    position.add(velocity);
    // Reset accelerationelertion to 0 each cycle
    acceleration.mult(0);
  }

  void applyForce(PVector force) {
    // We could add mass here if we want A = F / M
    acceleration.add(force);
  }

  // A method that calculates a steering force towards a target
  // STEER = DESIRED MINUS VELOCITY
  void seek(PVector target) {
    PVector desired = PVector.sub(target,position);  // A vector pointing from the position to the target
    
    // Scale to maximum speed
    desired.setMag(maxspeed);

    // Steering = Desired minus velocity
    PVector steer = PVector.sub(desired,velocity);
    steer.limit(maxforce);  // Limit to maximum steering force
    
    applyForce(steer);
  }
    
  void display() {
    // Draw a triangle rotated in the direction of velocity
    float theta = velocity.heading2D() + PI/2;
    fill(127);
    stroke(0);
    strokeWeight(1);
    pushMatrix();
    translate(position.x,position.y);
    rotate(theta);
    beginShape();
    vertex(0, -r*2);
    vertex(-r, r*2);
    vertex(r, r*2);
    endShape(CLOSE);
    popMatrix();
    
    
  }
}

4、运行结果

上一篇 下一篇

猜你喜欢

热点阅读