将点(向量)沿着角度移动 + Libgdx

3

我想把一个点(Vector2)沿着一个角度移动。我已经有了角度,但我不擅长数学或者libgdx库。为了获得角度,我使用以下代码:

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

现在,我想移动向量。但我真的不知道该怎么做......我查看了问题,但只有一些关于更改角度而非转移的内容。我认为在libgdx中必须有一个此类函数。请帮帮我。

更新:

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);
}
2个回答

11

如果我正确理解您的问题,您正在寻找一个方向向量?

如果我的理解是正确的,您可以像这样操作:

// 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,因为它更加简洁。


我按照您说的做了,但是出现了问题。我无法访问scl(...)方法。这个方法所在的库叫什么名字? - Hosein
那是我的库文件。我刚刚下载了最新版本并修复了它,但它没有移动。我更新了我的代码。 - Hosein
所以我的问题在于deltaTime。我没有更新它。更新后,它就正常工作了。谢谢。 附言:但如果您有更好的更新方法,请告诉我 :D - Hosein
1
更新了我的答案,展示了在完整类中如何实现。 - nEx.Software
1
对于居中的精灵触摸,您应该将touch.set(temp.x, temp.y);更改为touch.set(temp.x-sprite.getWidth()/2, temp.y-sprite.getHeight()/2); - Emre Koç
显示剩余2条评论

0
float dy = touchPoint.y - y;
float dx = touchPoint.x - x;

degree = Math.atan2(dy, dx) * MathUtils.radToDegree;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接