处理三角函数代码不起作用

Processing Trigonometry Code Not Working

我的处理代码基于 Daniel Shiffman 的 Nature Of Code 的 Oscillation 章节,主要是他根据前进方向(鼠标). 它不起作用,尽管我很确定它在某处有一个小错误。矩形刚飞出屏幕。 书中的例子(不完整):

void display() {
    float angle = velocity.heading2D;
    stroke(0); fill(175); pushMatrix(); rectMode(CENTER);              
    translate(location.x,location.y);
    rotate (angle); 
    rect(0,0,30,10);
    popMatrix();
}

我的代码:

void show() {
    angle = velocity.heading2D();
    rectMode(CENTER);
    pushMatrix();
    translate(location.x,location.y);
    rotate(angle);

    fill(255,20,20,150);
    rect(location.x,location.y,carSize,carSize);
    popMatrix();
}

设置和绘制:

Car car ;
void setup() {
    car = new Car();
}

void draw() {
    mouse = new PVector(mouseX,mouseY);
    background(255);
    car.show();
    car.move();
    car.update();
}

我终于明白了。而不是

 translate(location.x,location.y);  
 rect(location.x,location.y,40,40);  

必须是:

 translate(location.x,location.y);  
 rect(0,0,40,40);    

因为翻译已经将原点带到对象的位置,这就是我要绘制它的位置 (0,0)。