我想把一个点(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);
}
scl(...)
方法。这个方法所在的库叫什么名字? - Hosein