2016-06-20 7 views
-1

텍스처 (이미지)를 마우스 커서로 점프 및 센터링하지 않고 드래그하는 방법은 무엇입니까?텍스처를 드래그 (점프없이)

텍스처를 클릭하면 아무 일도 일어나지 않고 드래그하면 마우스 커서가 제자리에 있어야합니다 (텍스처의 가장자리를 기준으로).

예 : https://s31.postimg.org/ojapwbj6j/Untitled_1.jpg

SpriteBatch batch; 
Texture texture; 
OrthographicCamera camera; 
Vector3 spritePosition = new Vector3(); 

float offsetX, offsetY; 
Vector3 input; 

@Override 
public void create() { 

    batch = new SpriteBatch(); 
    texture = new Texture("badlogic.jpg"); 
    camera = new OrthographicCamera(); 
    camera.setToOrtho(false); 

} 

@Override 
public void render() { 
    Gdx.gl.glClearColor(1, 0, 0, 1); 
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 
    batch.begin(); 
    batch.draw(texture, spritePosition.x, spritePosition.y); 
    batch.end(); 

    if (Gdx.input.justTouched()){ 

      int x1 = Gdx.input.getX(); 
      int y1 = Gdx.input.getY(); 
      input = new Vector3(x1, y1, 0); 
      camera.unproject(input); 

      offsetX = x1 - spritePosition.x; 
      offsetY = y1 - spritePosition.y; 

    } 

    if (Gdx.input.isTouched()) { 

     spritePosition.set(Gdx.input.getX() - offsetX, Gdx.input.getY() - offsetY, 0); 

    } 

} 

답변

0

을 당신이 밖으로 작업 이미지에 마우스 위치의 오프셋 (offset), 그보다는 당신이에서 일을하는지 절반 너비/높이를 빼기하기 만하면 무엇 순간.

여기에 무슨 뜻인지 보여주는 매우 거친 의사의 - 당신은 자바로 재 작성해야합니다 ...

if Gdx.input.justTouched { 
    get mouse position from Gdx.input 
    camera.unproject it into cameraX and cameraY 
    offsetX = cameraX - imageX 
    offsetY = cameraY - imageY 
} 

if Gdx.input.isTouched { 
    spritePisition.set(Gdx.input.getX() - offsetX, Gdx.input.getY - offsetY) 
} 
+0

답변에 맞는 코드를 편집했습니다. 하지만 결과의 절반을 얻었습니다.이 업데이트 된 gif를 확인하십시오 : https://s31.postimg.org/lg8nu0u2z/Animation.gif – Beckham

+0

물리적 좌표계가 논리적 좌표와 다르기 때문에 y 축이 반전됩니다 (y 좌표 - 다운 및 다른 y- 업). camera.unproject (수정해야 함)를 사용하거나 화면의 높이에서 끝나는 y 위치를 빼서 반전시킵니다. –

0

왜 camera.unproject을 사용하고 있습니까?

이 시도 :

Override 
public void render() { 
    Gdx.gl.glClearColor(1, 0, 0, 1); 
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 

    batch.setProjectionMatrix(camera.combined); 
    batch.begin(); 
    batch.draw(texture, spritePosition.x, spritePosition.y); 
    batch.end(); 

    if (Gdx.input.isTouched()) { 
     spritePosition.set(Gdx.input.getX() - texture.getWidth()/2, Gdx.input.getY() + texture.getHeight()/2, 0); 
    } 

} 
+0

마우스 위치가 물리적 (즉, 화면 픽셀)에 그래서 그는 카메라를 사용한다 조정합니다. 이를 논리 좌표로 변환하십시오. –

+0

그는 결코 카메라를 움직이지 않습니까? – IronMonkey

+0

예. 다른 좌표계. –