2016-08-04 2 views
0

현재 우주선을 이동하고 소행성을 피하려고하는 게임에 대한 프로그래밍입니다. 우주선은 사용자가 손을 댈 때 움직여야하며 사용자의 손가락 움직임을 따라야합니다.Android/Libgdx이 문제를 해결하려면 어떻게해야하나요?

if (Gdx.input.isTouched()) { 

    x = Gdx.input.getX() - width/2; 
    y = -Gdx.input.getY() + height/2; 

} 

지금 당장 갖는 사용자가 화면을 터치하여 우주선 텔레 수 있다는있어 문제 : 우주선 함께 arround를 이동 스프라이트이다. 이 문제를 어떻게 해결할 수 있습니까? 터치 영역을 설정할 수 있습니까?

+0

사용자가 화면을 터치 할 때 먼저 확인하고, 화면에 닿는 위치가 우주선이면 ... 아무 것도하지 않으면 움직임을 따라갑니다. –

답변

2

배에서 터치 포인트까지의 단위 벡터 방향을 계산하고 속도로 곱하십시오. 카메라를 사용하지 않고 터치 좌표를 세계 좌표로 변환해야합니다.

private static final float SHIP_MAX_SPEED = 50f; //units per second 
private final Vector2 tmpVec2 = new Vector2(); 
private final Vector3 tmpVec3 = new Vector3(); 

//... 

if (Gdx.input.isTouched()) { 
    camera.unproject(tmpVec3.set(Gdx.input.getX(), Gdx.input.getY(), 0)); //touch point to world coordinate system. 
    tmpVec2.set(tmpVec3.x, tmpVec3.y).sub(x, y); //vector from ship to touch point 
    float maxDistance = SHIP_MAX_SPEED * Gdx.graphics.getDeltaTime(); //max distance ship can move this frame 
    if (tmpVec2.len() <= maxDistance) { 
     x = tmpVec3.x; 
     y = tmpVec3.y; 
    } else { 
     tmpVec2.nor().scl(maxDistance); //reduce vector to max distance length 
     x += tmpVec2.x; 
     y += tmpVec2.y; 
    } 
} 
관련 문제