gpt4 book ai didi

android - 基于旋转的 Sprite 运动

转载 作者:行者123 更新时间:2023-11-29 21:44:12 33 4
gpt4 key购买 nike

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

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

现在,当 bug 向前移动时,他总是这样做,必须计算新的 x 和 y 坐标,这取决于旋转角度,以便 bug 总是向前移动。有没有人有通过旋转角度计算方向的算法?

这是整个 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();
}
}

最佳答案

现在完美运行:

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

    私有(private)无效 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);
    }

关于android - 基于旋转的 Sprite 运动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16324344/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com