2017-04-09 1 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

[처리 중!] Java : (https://meta.stackoverflow.com/questions/321127/processing-java) – Pshemo

+0

두 태그가 모두 들어 있는지 확인합니다. 그러나 문제는 자바 일 수도 있습니다. one –

+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 프레임 워크와 모든 작품을 사용하여이 테스트 한

.

당신의 clickMethod에서 다음
private float sineTimer = 50f; //initially 50f as 0 would start wave 
private final int PARTICLES = 40; // the amount of particles in your bridge 

추가 : 메인 루프 추가에서 지금

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. Processing 버전이 추가되었습니다. 지금은 파도가 아래에서 위로 지나가고 있습니다. 그것의 방향을 바꿔서 어떻게 위에서 아래로 움직일 수 있습니까? –

관련 문제