2017-06-08 39 views
0

所以我試圖讓多個形狀使用PVector值在相同的方向上移動。我面臨的問題是,爲了讓我想要的物體(在這種情況下是一個蒼蠅的粗略插圖),我需要旋轉物體。這樣做會使向量數學在物體的旋轉方向上添加。是否有一些函數或數學可以用來規範每個形狀的角速度?處理:在相同的方向上移動多個形狀

class tFly{ 

boolean count=false; 
boolean countTwo=false; 
boolean move=false; 
PVector location; 
PVector velocity; 




    tFly(){ 
    location= new PVector(0,0); 
    velocity = new PVector(1,1); 
    } 
void flap(){//flap wings if moving, else keep them still 

    if(move==false){ 
    pushMatrix(); //wings 
noStroke(); 

translate(235,285); 
rotate(radians(25)); 
ellipse(location.x,location.y,25,50); 

popMatrix(); 


pushMatrix(); //wings 
noStroke(); 

translate(263,285); 
rotate(radians(340)); 
ellipse(location.x,location.y,25,50); 


popMatrix(); 


    }else{ 
pushMatrix(); //wings 
noStroke(); 
translate(235,285); 
if(count==false){ 
rotate(radians(49)); 
ellipse(location.x,location.y,25,50); 
count=true; 
}else{ 
rotate(radians(21)); 
ellipse(location.x,location.y,25,50); 
count=false; 
} 

popMatrix(); 


pushMatrix(); //wings 
noStroke(); 
translate(263,285); 
if(countTwo==false){ 
rotate(radians(320)); 
ellipse(location.x,location.y,25,50); 
countTwo=true; 
}else{ 
rotate(radians(340)); 
ellipse(location.x,location.y,25,50); 
countTwo=false; 
} 

popMatrix(); 
} 
} 
void display(){ 
    fill(0); 
ellipse(location.x+250,location.y+265,20,50); 

ellipse(location.x+250,location.y+280,25,34); 
pushMatrix(); 

translate(240,230); 
rotate(radians(70)); 
rect(location.x,location.y,15,.5); 
popMatrix(); 

pushMatrix(); 
translate(262,232); 
rotate(radians(110)); 
rect(location.x,location.y,15,.5); 
popMatrix(); 


pushMatrix(); 
translate(230,260); 
rotate(radians(40)); 
rect(location.x,location.y,15,.5); 
popMatrix(); 

pushMatrix(); 
translate(270,262); 
rotate(radians(140)); 
rect(location.x,location.y,15,.5); 
popMatrix(); 

pushMatrix(); 
translate(240,285); 
rotate(radians(120)); 
rect(location.x,location.y,15,.5); 
popMatrix(); 

pushMatrix(); 
translate(260,285); 
rotate(radians(50)); 
rect(location.x,location.y,15,.5); 
popMatrix(); 

fill(255,0,0); 

ellipse(location.x+240,location.y+250,20,18); 
ellipse(location.x+260,location.y+250,20,18); 
fill(128,128,128,127); 

flap(); 

} 
void update(){ 
location.add(velocity); 

} 

} 

這裏是我在安裝使用和借鑑

tFly fly; 


void setup(){ 
size(500,500); 
background(255); 
fly=new tFly(); 
} 

void draw(){ 
background(255); 
    ellipseMode(CENTER); 

fly.display(); 
fly.update(); 

} 
+0

您可以發佈[mcve]而不是從整個草圖中斷開連接的片段嗎?一個由兩個硬編碼形狀組成的簡單例子就足夠了。 –

+0

我投票結束這個問題作爲題外話,因爲這個問題似乎是關於使用的數學,這實際上不是一個編程問題。一個編程問題將是如何實現(已知)數學,而不是數學是什麼。 – Makyen

回答

1

你已經得到的pushMatrix()/popMatrix() ...掛起爲什麼從display()內的獨立不能嵌套將所有的繪製調用pushMatrix()/popMatrix()通話和簡單翻譯整個組?

class tFly { 

    boolean count=false; 
    boolean countTwo=false; 
    boolean move=true; 
    PVector location; 
    PVector velocity; 




    tFly() { 
    location= new PVector(0, 0); 
    velocity = new PVector(1, 1); 
    } 
    void flap() {//flap wings if moving, else keep them still 

    if (move==false) { 
     pushMatrix(); //wings 
     noStroke(); 

     translate(235, 285); 
     rotate(radians(25)); 
     ellipse(0, 0, 25, 50); 

     popMatrix(); 


     pushMatrix(); //wings 
     noStroke(); 

     translate(263, 285); 
     rotate(radians(340)); 
     ellipse(0, 0, 25, 50); 


     popMatrix(); 
    } else { 
     pushMatrix(); //wings 
     noStroke(); 
     translate(235,285); 
     if (count==false) { 
     rotate(radians(49)); 
     ellipse(0, 0, 25, 50); 
     count=true; 
     } else { 
     rotate(radians(21)); 
     ellipse(0, 0, 25, 50); 
     count=false; 
     } 

     popMatrix(); 


     pushMatrix(); //wings 
     noStroke(); 
     translate(263, 285); 
     if (countTwo==false) { 
     rotate(radians(320)); 
     ellipse(0, 0, 25, 50); 
     countTwo=true; 
     } else { 
     rotate(radians(340)); 
     ellipse(0, 0, 25, 50); 
     countTwo=false; 
     } 

     popMatrix(); 
    } 
    } 
    void display() { 
    pushMatrix(); 
    translate(location.x, location.y); 

    fill(0); 
    ellipse(250, 265, 20, 50); 

    ellipse(250, 280, 25, 34); 
    pushMatrix(); 

    translate(240, 230); 
    rotate(radians(70)); 
    rect(0, 0, 15, .5); 
    popMatrix(); 

    pushMatrix(); 
    translate(262, 232); 
    rotate(radians(110)); 
    rect(0, 0, 15, .5); 
    popMatrix(); 


    pushMatrix(); 
    translate(230, 260); 
    rotate(radians(40)); 
    rect(0, 0, 15, .5); 
    popMatrix(); 

    pushMatrix(); 
    translate(270, 262); 
    rotate(radians(140)); 
    rect(0, 0, 15, .5); 
    popMatrix(); 

    pushMatrix(); 
    translate(240, 285); 
    rotate(radians(120)); 
    rect(0, 0, 15, .5); 
    popMatrix(); 

    pushMatrix(); 
    translate(260, 285); 
    rotate(radians(50)); 
    rect(0, 0, 15, .5); 
    popMatrix(); 

    fill(255, 0, 0); 

    ellipse(240, 250, 20, 18); 
    ellipse(260, 250, 20, 18); 
    fill(128, 128, 128, 127); 

    flap(); 

    popMatrix(); 
    } 
    void update() { 
    location.add(velocity); 
    } 
} 


tFly fly; 


void setup() { 
    size(500, 500); 
    background(255); 
    fly=new tFly(); 
} 

void draw() { 
    background(255); 
    ellipseMode(CENTER); 

    fly.display(); 
    fly.update(); 
} 
相關問題