1

我在 Android OpenGL 中有一个精灵。这个精灵(一只小甲虫)总是向前移动,我使用:

sprite.setPosition(posX, posY);

现在我有一个旋转方法,当用户向左或向右手势时,错误会旋转:

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 坐标,这取决于旋转角度,以便虫子总是向前移动。有没有人有一个算法来计算旋转角度的方向?

这是整个 Bug 类:

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

3 回答 3

1

现在完美运行:

  1. 将度数转换为弧度
  2. 将 x 坐标设置为 -

    私人无效应用位置(){

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

    }

于 2013-05-04T18:28:59.877 回答
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(angle), sin(angle)] 是指向正 x 轴的零角度。cos/sin/tan 的许多实现使用弧度而不是度数进行计算,因此请酌情转换。

[cos angle, sin angle]是向右零(正 x),逆时针。[-sin angle, cos angle]是零指向上(正 y),逆时针。

于 2013-05-01T18:49:02.130 回答
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);

如果您在每一帧都执行此操作,您将让您的甲虫沿角度方向移动。

于 2013-05-01T18:54:15.720 回答