2013-07-30 1 views
0

누구나 봐요,Box2D via libGDX : 본체 당 하나의 MouseJoint를 작성하여 별도의 Android를 터치하는 방법은 무엇입니까?

감사합니다.

나는 퐁 복제본을 만들고 있는데 Box2D를 두 개의 MouseJoints로 제한하려고합니다. 패들 당 최대 MouseJoint 한 개. 마우스 조인트는 사용자의 두 건 드리 중 하나가 두 개의 패들 경계 중 하나에있는 경우에만 만들어야합니다.

코드에 이상한 결과가 나타납니다. 첫 번째 터치가 왼쪽 패들에 도착하고 두 번째 터치가 패들 외부에있는 경우 두 번째 MouseJoint가 왼쪽 패들에 생성됩니다 (첨부 된 이미지 참조).

참고 : 왼쪽 패들의 두 개의 마우스 조인트 외에 이미지에 두 개의 프리즘 조인트가 있습니다. 하나는 각 패들에 붙어있다. 아무 소용

enter image description here

, 나는 내가 생각할 또는 다른 사람의 코드에서 적응할 수있는 알고리즘을 모두 시도했습니다.

코드 솔루션 예제 또는 링크를 게시 할 수 있으면 많은 의무가 있습니다. 나는 tempBody를 볼 수있는에서

public class MyScreen implements Screen, InputProcessor{ 

/*========some variables and methods omitted for clarity========*/ 


/*multiple mouse joint experiment*/ 
public MouseJoint mouseJoint[] = new MouseJoint[2];  
Body hitBody[] = new Body[2]; 
Body tempBody; 

public MyScreen(Pong game) { 
    this.game = game; 
}   

/*---------------------Screen interface methods--------------------------*/ 
//methods omitted for clarity 
/*---------------------end Screen interface methods----------------------*/ 

/*---------------------InputProcessor interface methods------------------*/ 
@Override 
public boolean keyDown(int keycode) { 
    return false; 
} 

@Override 
public boolean keyUp(int keycode) { 
    return false; 
} 

@Override 
public boolean keyTyped(char character) { 
    return false; 
} 

@Override 
public boolean touchDown(int screenX, int screenY, int pointer, int button) {          
       testPoint.set(screenX, screenY, 0); 
       camera.unproject(testPoint); 

       // ask the world which bodies are within the given 
       // bounding box around the mouse pointer 
       hitBody[pointer] = null; 

       world.QueryAABB(callback, testPoint.x - 1.0f, testPoint.y - 1.0f, testPoint.x + 1.0f, testPoint.y + 1.0f); 
       hitBody[pointer] = tempBody; 

       // if we hit something we create a new mouse joint 
       // and attach it to the hit body. 
       if (hitBody[pointer] != null) { 

        MouseJointDef def = new MouseJointDef(); 
        def.bodyA = groundBody; 
        def.bodyB = hitBody[pointer]; 
        def.collideConnected = true;             
        def.target.set(hitBody[pointer].getPosition().x, hitBody[pointer].getPosition().y); 
        def.maxForce = 3000.0f * hitBody[pointer].getMass(); 

        mouseJoint[pointer] = (MouseJoint)world.createJoint(def); 
        hitBody[pointer].setAwake(true); 
       } else { 

       } 
       return false;     
} 

@Override 
public boolean touchUp(int screenX, int screenY, int pointer, int button) { 
    if (mouseJoint[pointer] != null) { 
     world.destroyJoint(mouseJoint[pointer]); 
     mouseJoint[pointer] = null; 
    } 
    return false; 
} 

/**a temporary vector for delta target destination during touchDragged() method**/ 
Vector2 target = new Vector2(); 

@Override 
public boolean touchDragged(int screenX, int screenY, int pointer) { 
    if (mouseJoint[pointer] != null) { 
     camera.unproject(testPoint.set(screenX, screenY, 0)); 
     mouseJoint[pointer].setTarget(target.set(testPoint.x, testPoint.y)); 
    } 
    return false; 
} 

@Override 
public boolean mouseMoved(int screenX, int screenY) { 
    return false; 
} 

@Override 
public boolean scrolled(int amount) { 
    return false; 
} 
    /*----------------end InputProcessor interface methods------------------*/ 

/*------------------------helper methods------------------------------------*/ 
    /*android screen touch vector for a mouse joint*/ 
    Vector3 testPoint = new Vector3(); //we instantiate this vector and the callback here so we don't irritate the GC 
    QueryCallback callback = new QueryCallback() { 
     @Override public boolean reportFixture (Fixture fixture) { 
      // if the hit fixture's body is the ground body 
      // we ignore it 
      if (fixture.getBody() == groundBody) return true; 

      if (fixture.testPoint(testPoint.x, testPoint.y)) { 
       tempBody = fixture.getBody(); 
       return false; 
      } else 
       return true; 
     } 
    };   
/*------------------------end helper methods-------------------------------*/ 
} 

답변

2

NULL로 재설정 결코 :

여기 내 코드입니다. 그 의미는 패드를 처음 만질 때 접촉 패드에 tempBody을 설정 한 다음 본문 외부를 누르면 콜백이 새로운 본문을 찾지 못하지만 testBody을 null로 재설정하지 않기 때문에 testBodyhitBody[pointer] 첫 번째 패들로 설정합니다. 같이한다

방식 터치 다운 기능은 다음과 같습니다

@Override 
public boolean touchDown(int screenX, int screenY, int pointer, int button) {          
      testPoint.set(screenX, screenY, 0); 
      camera.unproject(testPoint); 

      // ask the world which bodies are within the given 
      // bounding box around the mouse pointer 
      hitBody[pointer] = null; 

      world.QueryAABB(callback, testPoint.x - 1.0f, testPoint.y - 1.0f, testPoint.x + 1.0f, testPoint.y + 1.0f); 
      hitBody[pointer] = tempBody; 

      // if we hit something we create a new mouse joint 
      // and attach it to the hit body. 
      if (hitBody[pointer] != null) { 

       MouseJointDef def = new MouseJointDef(); 
       def.bodyA = groundBody; 
       def.bodyB = hitBody[pointer]; 
       def.collideConnected = true;             
       def.target.set(hitBody[pointer].getPosition().x, hitBody[pointer].getPosition().y); 
       def.maxForce = 3000.0f * hitBody[pointer].getMass(); 

       mouseJoint[pointer] = (MouseJoint)world.createJoint(def); 
       hitBody[pointer].setAwake(true); 
      } else { 

      } 
      tempBody = null; 

      return false;     

}

이 식으로 tempBody은 항상 사용 후에 null로 재설정됩니다.

+0

그것은 매력처럼 작동했습니다. 고맙습니다. – HACKQ3R