2013-05-01 125 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