java - 在一个角度上移动一个点( vector )+ Libgdx

标签 java math libgdx transfer angle

我想在一个角度上移动一个点 (Vector2)。我有我的角度。但我不擅长数学或 libgdx。为了获得角度,我使用这个:

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

现在,我想移动 vector 。但我真的不知道我必须做什么......我看了问题,但只是改变角度,而不是转移。我认为 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);
}

最佳答案

如果我没有正确理解您的问题,您是在寻找方向 vector 吗?

如果这是正确的理解,你可以这样做:

// 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();

这将为您提供从位置到触摸点的归一化方向 vector 。

然后您可以将它缩放到您想要移动的速度,然后用它来更新您的位置。

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

关于java - 在一个角度上移动一个点( vector )+ Libgdx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17694076/

相关文章:

algorithm - 计算离散对数

java - 错误: JAVA_HOME is set to an invalid directory in libGDX

java.lang.NoClassDefFoundError : android/os/Build$VERSION 错误

带有 libGDX 的 Holo 主题的 Android Dialog 看起来很奇怪

java - Libgdx - 加载字体会减慢游戏速度

java - 从 java 转换为 C++

java - Java 的同步会更新整个缓存,还是只更新我同步的对象?

algorithm - 按素因数的字典顺序对数字进行排序

c - 在 C 中,如何计算 unsigned long 的模数?

java - 权限对话框未显示 Android 6.0.1 (Marshmallow)