转向力
书名:代码本色:用编程模拟自然系统
作者:Daniel Shiffman
译者:周晗彬
ISBN:978-7-115-36947-5
第6章目录
6.3 转向力
1、转向力的概念
为了更好地理解自治智能体,我们要先了解转向力的概念。
思考以下场景:一辆移动的小车正在寻找一个目标。
- 如图6-1所示,小车的目的就是找到图中的目标位置。
按照第2章的做法,我们可以让目标位置具有引力作用,让它吸引周围的物体,这样小车就可以朝着它运动。这是一个很好的解决方案,却不是我们想要的方法。
我们并不想简简单单地计算引力,而是想让小车通过对自身状态和环境的感知(比如移动速度有多大,朝着什么方向移动),智能地做出转向决定。
小车应该先计算到达目标的所需速度(指向目标位置的向量),再比较自己当前的移动速度,最后根据下面的公式计算转向力。
转向力 = 所需速度 - 当前速度
- 我们可以用Processing表示这个公式:
PVector steer = PVector.sub(desired, velocity);
在上面的公式中,当前速度是已知的,但所需速度仍要通过计算得到。
参考图6-2,如果小车的最终目的是“寻找目标位置”,那么所需速度就是由当前位置指向目标位置的向量。
- 假设目标位置向量是已知的,我们就有:
PVector desired = PVector.sub(target, location);
但这并不符合实际情况,如果屏幕的分辨率非常高,两者之间的距离为几千像素,那么小车的移动速度会非常快,最后无法得到合理的动画效果。
- 因此我们要将实现方式改为:小车移动到目标位置时有一个最大速率。
换句话说,小车移动的方向指向目标位置,速度的大小等于预先设置的最大值(以尽可能快的速度移向目标位置)。 - 首先,我们需要在Vehicle类中添加一个最大速率变量(maxspeed)。
class Vehicle {
PVector location;
PVector velocity;
PVector acceleration;
float maxspeed; 最大速率
- 其次,在计算所需速度时,我们应该将它的大小设为最大速率。
PVector desired = PVector.sub(target,location);
desired.normalize();
desired.mult(maxspeed);
图6-3
- 把以上的代码放在一起,我们可以得到一个seek()函数,该函数的作用是计算移动到目标位置需要的转向力,它的参数是目标位置向量。
void seek(PVector target) {
PVector desired = PVector.sub(target,location);
desired.normalize();
desired.mult(maxspeed); 计算所需速度,让它的大小等于最大速率
PVector steer = PVector.sub(desired, velocity); Reynolds的转向力公式
applyForce(steer); 使用我们之前的物理模型,将力变为对象的加速度
}
- 在以上代码中,我们最后把转向力传入applyForce()函数。applyForce()函数建立在2.1.3节内容的基础之上。你也可以在Box2D的applyForce()函数或toxiclibs的addForce()函数中传入这个转向力。
2、为什么上面的代码能正常工作?
让我们结合小车的自身状态和目标位置分析转向力的原理,请看图6-4。
图6-4
-
转向力和地球引力有所不同。
自治智能体有一大特点:它对外部环境的感知能力是有限的。
Reynolds的转向力公式已经涵盖了这种感知能力。
根据转向力计算公式,如果小车的起始状态是静止的(当前速度为0),转向力就等于所需速度。
小车对自身的速度有感知能力,它的转向力会根据自身速度进行自动补偿。
小车寻找目标的移动方式取决于它的初始速度,因此转向力公式能够有效地模拟转向行为。 -
兴奋之余,我们遗漏了最后一步。
这是一辆什么样的车?一辆极易操控的超级赛车?还是一辆难以转动的大卡车?
示例代码没有考虑小车的转向能力,我们可以通过限制转向力的大小控制转向能力。
下面,我们引入一个“最大转向力”变量(maxforce)用于限制转向力的大小,代码如下:
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);
}
-
转向力的限制还引入了一个关键点,让小车以最大速率移向目标位置并不是我们的最终目标,否则我们可以直接把小车的位置等于目标位置。
正如Reynolds所说,最终目的是让小车用一种“贴近真实”的方式移动。
我们试图让小车以一种转向的方式移动向目标位置,因此需要借助力和各种系统变量模拟特定的行为。
比如,不同大小的转向力(如图6-5)会造成不一样的运动路径,两种路径没有绝对的好坏,合适与否取决于目标效果。(当然,这些设定值并不是固定的,你可以根据具体的条件更改它们。比如,可以让小车拥有生命值:生命值越高,转向能力越好。)
图6-5
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();
}
}