2017-01-09 94 views
1

我的處理代碼基於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(); 
} 
+0

你可以請張貼[mcve]嗎?如果沒有任何'setup()'或'draw()'函數(這意味着我們無法運行你的代碼),而不是僅僅發佈你的整個類,那麼將問題簡化爲幾行代碼行。 –

+0

對不起,我會改變它。 – udbhavs

+0

另外,我不知道translate(location.x,location.y)是什麼 – udbhavs

回答

1

我終於想通了。取而代之的

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

它必須是:

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

因爲翻譯已經採取原點到對象的位置,而這也正是我想提請它(0,0)。