2013-05-01 126 views
1

我在Android OpenGL中有一個精靈。這精靈(小beetlebug)總是在前進的方向前進,我用:基於旋轉的雪碧運動

sprite.setPosition(posX, posY); 

現在我有一個旋轉的方法,當用戶手勢向左或向右的bug旋轉:

private void applyRotation() { 
    for(int i=0;i<beetleBug.size;i++) { 
     Sprite s = beetleBug.get(i); 
     s.setOrigin(s.getWidth()/2, s.getHeight()/2); 
     s.setRotation(angle); 
    } 
} 

現在,當錯誤向前移動時,他總是會根據旋轉角度計算新的x和y座標,以便錯誤始終向前。有沒有人有算法來計算旋轉角度的方向?

這裏是整個錯誤級:

public class Bug { 

    private SpriteBatch    spriteBatch = null; 
    private TextureAtlas   spriteSheet; 
    private Array<Sprite>   beetleBug; 
    private int      currentFrame = 0; 
    private final float    frameLength = 0.10f; //in seconds, how long a frame last 
    private float     animationElapsed = 0.0f; 
    private float     angle = 0.0f; 
    private float     posX = 0.0f; 
    private float     posY = 0.0f; 
    private float     sizeX = 100.0f; 
    private float     sizeY = 100.0f; 
    private float     offSet = 50.0f; 

    public Bug() { 
     spriteBatch = new SpriteBatch(); 
     spriteSheet = new TextureAtlas("assets/data/bug.txt"); 
     beetleBug = spriteSheet.createSprites("bug"); 

     // dont forget to set the size of your sprites! 
     for(int i=0; i<beetleBug.size; i++){ 
      beetleBug.get(i).setSize(sizeX, sizeY); 

     } 

     applyPosition(); 
    } 



    public void handleInput() { 

     boolean leftKey = Gdx.input.isKeyPressed(Input.Keys.LEFT); 
     boolean rightKey = Gdx.input.isKeyPressed(Input.Keys.RIGHT); 

     if(rightKey) { 
      if(angle <= 0) { 
       angle = 360; 
      } 
      angle -= 2f; 
      applyRotation(); 
     } 

     if(leftKey) { 
      if(angle >= 360) { 
       angle = 0; 
      } 
      angle += 2f; 
      applyRotation(); 
     } 


     applyPosition(); 
    } 


    private void applyPosition() { 
     float x = (float) Math.cos(angle); 
     float y = (float) Math.sin(angle); 

     posX = posX + x; 
     posY = posY + y; 

     for(int i=0; i<beetleBug.size; i++){ 
      beetleBug.get(i).setPosition(posX - offSet, posY -offSet); // optional: center the sprite to screen 
     } 
    } 


    private void applyRotation() { 
     for(int i=0;i<beetleBug.size;i++) { 
      Sprite s = beetleBug.get(i); 
      s.setOrigin(s.getWidth()/2, s.getHeight()/2); 
      s.setRotation(angle); 
     } 
    } 

    public void render(OrthographicCamera cam) { 

     float dt = Gdx.graphics.getDeltaTime(); 
     animationElapsed += dt; 
     while(animationElapsed > frameLength){ 
      animationElapsed -= frameLength; 
      currentFrame = (currentFrame == beetleBug.size - 1) ? 0 : ++currentFrame; 
     } 

     spriteBatch.setProjectionMatrix(cam.combined); 

     spriteBatch.begin(); 
     beetleBug.get(currentFrame).draw(spriteBatch); 

     spriteBatch.end(); 
    } 
} 

回答

0

這可能會實現:

int currentX = 100; //beetleCurrentX 
int currentY = 100; //beetleCurrentY 
int angle = 200; //beetleAngle 
int len = 2;  //Step that the beetle makes (jumps 2 in this case) 

int x2Pos = sin(angle)*len + currentX; 
int y2Pos = cos(angle)*len + currentY; 

sprite.setPosition(x2Pos,y2Pos); 

如果執行此每一幀你將有你的甲蟲在角方向移動。

+0

請參閱上面的整個Bug類,它不以這種方式工作,beetlebug正在旋轉,但每次按鍵輸入後總是移動n個錯誤的方向。 – illvoid 2013-05-03 20:37:45

+0

請注意,這將用於指向正y方向的零度,隨着順時針方向角度增加。例如指南針點 – Maple 2013-05-06 13:35:09

1

創建一個歸一化的向量由速度來代表甲蟲的方向,然後乘以。將該矢量添加到甲蟲的當前位置,並且您已獲得新的位置。

  1. 使用你的角度創建歸一化的矢量(即長度爲1)。 vx = cos(angle), vy = sin(angle)
  2. 乘以甲蟲的速度。 vx = vx*speed, vy = vy*speed
  3. 將其添加到當前位置。 x = x + vx, y = y + vy
  4. 重複

的一些陷阱:小心,你的精靈的圖形旋轉和旋轉自己的內部表示同路。一些框架翻轉它們旋轉圖形的方式。上述[cos(角度),sin(角度)]是指向正x軸的零角度。 cos/sin/tan的許多實現都使用弧度而不是度來計算,所以可以根據需要進行轉換。

[cos angle, sin angle]是逆時針爲零向右(正x)。 [-sin angle, cos angle]用於零向上(正y),逆時針。

+0

是的,它似乎是這樣的:當我按下右鍵時,我必須減小角度,我怎樣才能擺脫這個原因,設置方向與罪,cos無法正常工作。 – illvoid 2013-05-03 20:23:18

+0

需要注意的兩大問題:1)確保你的精靈在零度時所面對的方向是你編碼爲0度的方向。是這樣,是嗎,等等?2)你的框架是旋轉度(0-360)還是弧度(0-2pi)?這包括特別是罪惡/ cos調用。他們可能是基於弧度的。 – Maple 2013-05-04 02:19:15

+0

1)給出。 0度是開始旋轉。 2)角度的度數不是弧度。感謝這一點,我認爲我有轉換爲弧度。 – illvoid 2013-05-04 14:10:46

1

完美的作品現在:

  1. 轉換度弧度
  2. 組X coordintae到 -

    私人無效applyPosition(){

    float radians = (float) Math.toRadians(angle); 
    float x = -(float) Math.sin(radians); 
    float y = (float) Math.cos(radians); 
    
    posX = posX + x; 
    posY = posY + y; 
    
    for(int i=0; i<beetleBug.size; i++){ 
        beetleBug.get(i).setPosition(posX - offSet, posY -offSet); 
    } 
    

    }

+0

我真的很難從C++複製我的功能,認爲sin會自動轉換爲弧度。 – 2013-05-04 19:29:16

+0

你必須反轉x座標的原因是因爲你的0度被指出,而不是正確的。即x軸的方向。 '[-sin a,cos a]'是零向上(正y),逆時針。 '[cos a,sin a]'爲逆時針向右(零x)。 – Maple 2013-05-06 13:32:25