2013-07-17 2 views
4

각도에서 점 (벡터 2)을 이동하고 싶습니다. 나는 내 각을 가지고있다. 하지만 저는 수학이나 libgdx에서 좋지 않습니다. 각도를 얻기 위해서 나는 이것을 사용합니다 :각도에서 점 (벡터) 이동 + Libgdx

degrees = (float) ((Math.atan2(touchPoint.x - x, 
       -(touchPoint.y - y)) * 180.0d/Math.PI) + 240.0f); 

이제 벡터를 이동하고 싶습니다. 그러나 나는 정말로 내가해야하는 것을 모른다. .. 나는 질문을 보았다. 그러나 각도를 바꾸는 것, 옮기지 않는 것에 관해 단지 somethings가 있었다. libgdx에서이 기능이 있어야한다고 생각합니다. 도와주세요.

업데이트 : 내가 제대로 질문을 이해하고있는 경우

public class Games implements ApplicationListener { 

SpriteBatch spriteBatch; 
Texture texture; 
float x = 160; 
float y = 5; 

Vector2 touch; 
Vector2 movement; 
Vector2 position; 
Vector2 dir; 
Vector2 velocity; 

float speed; 
float deltaTime; 

@Override 
public void create() { 
    spriteBatch = new SpriteBatch(); 
    texture = new Texture(Gdx.files.internal("images/1.png")); 

    touch = new Vector2(); 
    movement = new Vector2(); 
    position = new Vector2(); 
    dir = new Vector2(); 
    velocity = new Vector2(); 

    speed = 5; 
    deltaTime = Gdx.graphics.getDeltaTime(); 
} 

public void render() { 
    Gdx.gl.glClear(Gdx.gl10.GL_COLOR_BUFFER_BIT); 
    deltaTime += 0.5f; 

    spriteBatch.begin(); 

    begin(deltaTime); 
    spriteBatch.draw(texture, position.x, position.y); 

    spriteBatch.end(); 
} 

private void begin(float deltaTime) { 
    touch.set(Gdx.input.getX(), Gdx.input.getY()); 

    position.set(x, y); 
    dir.set(touch).sub(position).nor(); 
    velocity.set(dir).scl(speed); 
    movement.set(velocity).scl(deltaTime); 
    position.add(movement); 
} 

답변

10

, 당신이 방향 벡터 찾는거야?

// Declared as fields, so they will be reused 
Vector2 position = new Vector2(); 
Vector2 velocity = new Vector2(); 
Vector2 movement = new Vector2(); 

Vector2 touch = new Vector2(); 
Vector2 dir = new Vector2(); 

// On touch events, set the touch vector, then do this to get the direction vector 
dir.set(touch).sub(position).nor(); 

터치 지점 위치에서 당신에게 표준화 방향 벡터를 제공한다 : 즉 올바른 이해 경우

, 당신은 같은 것을 할 수 있습니다.

그런 다음 이동하려는 속도로 축척 한 다음 이동하여 위치를 업데이트 할 수 있습니다.

class Game extends ApplicationAdapter { 

    Vector2 position = new Vector2(); 
    Vector2 velocity = new Vector2(); 
    Vector2 movement = new Vector2(); 
    Vector2 touch = new Vector2(); 
    Vector2 dir = new Vector2(); 

    Vector3 temp = new Vector3(); 

    float speed = 100; 

    OrthographicCamera camera; 

    SpriteBatch batch; 
    Texture texture; 
    Sprite sprite; 

    @Override 
    public void create() { 
     camera = new OrthographicCamera(); 
     camera.setToOrtho(false); 

     batch = new SpriteBatch(); 

     texture = new Texture(Gdx.files.internal("data/badlogicsmall.jpg")); 

     sprite = new Sprite(texture); 

     Gdx.input.setInputProcessor(new InputAdapter() { 
      @Override 
      public boolean touchDown (int screenX, int screenY, int pointer, int button) { 
       camera.unproject(temp.set(screenX, screenY, 0)); 
       touch.set(temp.x, temp.y); 
       return true; 
      } 
     }); 
    } 

    @Override 
    public void render() { 
     Gdx.gl10.glClear(GL10.GL_COLOR_BUFFER_BIT); 
     update(Gdx.graphics.getDeltaTime()); 
     batch.begin(); 
     sprite.draw(batch); 
     batch.end(); 
    } 

    @Override 
    public void dispose() { 
     texture.dispose(); 
     batch.dispose();    
    } 
    public void update (float deltaTime) { 
     position.set(sprite.getX(), sprite.getY()); 
     dir.set(touch).sub(position).nor(); 
     velocity.set(dir).scl(speed); 
     movement.set(velocity).scl(deltaTime); 
     if (position.dst2(touch) > movement.len2()) { 
      position.add(movement); 
     } else { 
      position.set(touch); 
     }    
     sprite.setX(position.x); 
     sprite.setY(position.y); 
    } 
} 
:
velocity = new Vector2(dir).scl(speed); 

그리고 각 프레임에

movement.set(velocity).scl(deltaTime); 
position.add(movement); 

업데이트

다음

는 전체 클래스처럼 어떻게 보일지처럼 뭔가를 할

참고로 Vector2 위치를 없애고 x와 y를 직접 사용할 수는 있지만 조금 깔끔하기 때문에 Vector2를 사용하고 싶습니다.

+0

나는 당신이 말한 것을 할 수 있지만 문제가 있습니다. 나는'scl (...)'메소드에 접근 할 수 없다. 이 방법에 대한 도서관의 이름은 무엇입니까? – Hosein

+0

내 libs했다. 방금 최신 버전을 다운로드하고 수정했습니다. 그러나 그것은 움직이지 않는다. 내 코드를 업데이트합니다. – Hosein

+0

그래서 내 문제는 deltaTime에있었습니다. 나는 그것을 업데이트하지 않았다. 업데이트 후 잘 작동했습니다. tnx. p.s :하지만 더 나은 방법으로 업데이트하는 경우 나에게 말하십시오. D – Hosein

0
float dy = touchPoint.y - y; 
float dx = touchPoint.x - x; 

degree = Math.atan2(dy, dx) * MathUtils.radToDegree;