2017-04-09 169 views
0

我在box2d世界中有一系列物體。這些機構被鏈接在一起。我想在按下鼠標時沿着身體創建一個正弦波。我只希望這個波發生一次,它應該以相同的幅度沿着身體的長度繼續,直到它結束,然後停止,直到再次按下鼠標。使用Processing和box2d創建單波

目前,我有這樣的:

float angle = 0.0; 
float scalar = 1.2; 
float speed = 0.01; 
void mousePressed() { 
    for (int j = 0; j < 91; j++) { 
     float x = sin(j+angle)*scalar; 
     float y = 0;  
     Vec2 mov2 = new Vec2(x,y); 
     bridge.particles.get(j).body.setLinearVelocity(mov2); 
     angle+=speed; 
    } 
} 

然而,這將導致機構成爲一個連續波剛剛向外擴展如下(只是在一瞬間嘗試這種對左側股):

enter image description here

如何創建一個向下移動的單波形?

使用從@dfour修改後的代碼,我用:

void mousePressed() { 
    int frequency = 10; // sine frequency (larger for longer wave) 
    double fullCircle = Math.toRadians(180); // get 1 full iteration of a circle in radians; 
    float x=0; 
    float y=0; 
    for(int i = 0; i < 100 ; i++){ 
     if(i > fullCircle*frequency){ 
      // after first wave so output 0 
      //System.out.println(0); 
     }else{ 
      // part of first sinewave so output wave value 
      x =(float)Math.sin(i/frequency); 
      Vec2 mov2 = new Vec2(x,y); 
      print(" x: "+x); 
      System.out.println(Math.sin(i/frequency)); 
      bridge.particles.get(i).body.setLinearVelocity(mov2); 
     } 
    } 
} 

但這也給了我與波沒有實際進展下來機構行:爲了得到一個enter image description here

+1

只是要清楚:[Processing!= Java](https://meta.stackoverflow.com/questions/321127/processing-java) – Pshemo

+0

當然這就是爲什麼兩個標籤都在。但問題可能也是一個java一個 –

+0

如果你想自己控制身體,是否有你使用box2d的原因?如果你知道你想要的身體是什麼,爲什麼不把它們繪製在那裏而不是依靠物理引擎? –

回答

1

來自正弦波的單波只需要循環,一旦第一波完成輸出0;

int frequency = 10; // sine frequency (larger for longer wave) 
    double fullCircle = Math.toRadians(360); // get 1 full iteration of a circle in radians; 
    for(double i = 0; i < 75 ; i++){ 
     if(i > fullCircle*frequency){ 
      // after first wave so output 0 
      System.out.println(0); 
     }else{ 
      // part of first sinewave so output wave value 
      System.out.println(Math.sin(i/frequency)); 
     } 
    } 

編輯:

我已經使用LibGdx框架,所有的工作測試這一點。若要將此你的代碼,你將需要添加一個計時器字段存儲時間:

private float sineTimer = 50f; //initially 50f as 0 would start wave 
private final int PARTICLES = 40; // the amount of particles in your bridge 

然後在您的clickMethod補充:

sineTimer = -35f; // resets timer 

現在,在你的主循環地址:

sineTimer += Gdx.graphics.getDeltaTime() * 10; // increment time since last frame 

    int frequency = 3; // sine frequency (larger for longer wave) 
    float fullCircle = (float) Math.toRadians(360); // get 1 full iteration of a circle in radians; 
    // loop through all particles 
    for(int i = 0; i < PARTICLES ; i++){ 
     float offset = i; // set base offset 
     offset+=sineTimer; // add timer value to offset 
     // if offset is lower than 0 or past first sine wave set particle to default place 
     if(offset > fullCircle*frequency || offset < 0){ 
      bridgeParticles.get(i).setTransform(32,i, 0); 
     }else{ // else apply sine position (I used x*3 here to amplifiy sine on x axis) 
      float x =(float)Math.sin(offset/frequency); 
      bridgeParticles.get(i).setTransform((x *3) + 32, i, 0); 

     } 
    } 

修改代碼處理環境:

private float sineTimer = 50f; //initially 50f as 0 would start wave 
private final int PARTICLES = 40; // the amount of particles in your bridge 

void draw(){ 
    sineTimer += 0.5; // increment time since last frame 

     int frequency = 23; // sine frequency (larger for longer wave) 
     float fullCircle = (float) Math.toRadians(180); 
     for(int i = 0; i < PARTICLES ; i++){ 
      float offset = i; // set base offset 
      offset+=sineTimer; // add timer value to offset 
      if(offset > fullCircle*frequency || offset < 0){ 
       bridge.particles.get(i).body.setTransform(box2d.coordPixelsToWorld(200,i*10), 0); 
      }else{ 
       float x =(float)Math.sin(offset/frequency); 
       bridge.particles.get(i).body.setTransform(box2d.coordPixelsToWorld((x *125) +200, i*10), 0); 
      } 
     } 
    } 

    void mousePressed() { 
    sineTimer = -35f; // resets timer 
    } 
+0

謝謝@dfour ...請參閱修改後的代碼。我所遇到的最大問題之一是浪潮不會沿着機構鏈向下移動 –

+0

@SebastianZeki我已經添加了一個在LibGdx框架中工作的完整示例,希望您能夠將其轉換爲您的需求。 :) – dfour

+0

謝謝@dfour。我已經添加了處理版本。此時此波正由下而上傳遞。我怎樣才能改變它的方向,使其從上到下運行? –