2013-07-17 39 views
4

我想移動一個角度(Vector2)。我有我的角度。但我不擅長數學或libgdx。爲獲得角度我使用此:在一個角度上移動一個點(矢量)+ Libgdx

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

現在,我想要移動向量。但我真的不知道我必須做什麼......我看了一些問題,但只是關於改變角度,而不是轉移。我認爲在libgdx中必須有一個這樣的函數。請幫忙。

UPDATE:

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(); 

,這將給你從位置觸摸點的歸一化方向矢量。

然後,您可以將其縮放到想要移動的速度,然後使用它來更新您的位置。

velocity = new Vector2(dir).scl(speed); 

而且每一幀,這樣做

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

更新

下面是它會是什麼樣子在一個滿級:

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); 
    } 
} 

注意,你可以取消Vector2的位置,直接使用x和y,但是我喜歡使用Vector2,因爲它比較乾淨。

+0

我做你曾經說過的,但有一個問題。我無法達到'scl(...)'方法。該方法的圖書館名稱是什麼? – Hosein

+0

這是我的庫。我只是下載最新版本,並修復。但它不動。我更新我的代碼。 – 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;