Java 在角度上移动点(向量)+;Libgdx
我想在一个角度上移动一个点(矢量2)。我有我的角度。但我的数学和libgdx都不好。为了获得角度,我使用以下方法:Java 在角度上移动点(向量)+;Libgdx,java,math,libgdx,transfer,angle,Java,Math,Libgdx,Transfer,Angle,我想在一个角度上移动一个点(矢量2)。我有我的角度。但我的数学和libgdx都不好。为了获得角度,我使用以下方法: degrees = (float) ((Math.atan2(touchPoint.x - x, -(touchPoint.y - y)) * 180.0d / Math.PI) + 240.0f); 现在,我想移动向量。但我真的不知道我必须做什么。。。我看了一些问题,但只是一些关于改变角度的事情,而不是转移。我认为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);
}
如果我正确理解了你的问题,你是在寻找方向向量吗 如果这是正确的理解,您可以这样做:
// 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);
}
}
请注意,您可以取消位置矢量2,直接使用x和y,但我喜欢使用矢量2,因为它更干净。我按您所说的做,但有一个问题。我找不到
scl(…)
方法。这个方法的库名是什么?那是我的libs。我只是下载了最新版本,并修复了。但它不动。我更新了代码,所以我的问题出在deltaTime。我没有更新它。更新后,它运行良好。tnx。p、 s:但如果你有更好的更新方法,就告诉我:复制我的答案,以显示在整个课堂上的效果。你应该更改touch.set(temp.x,temp.y);touch.set(temp.x-sprite.getWidth()/2,temp.y-sprite.getHeight()/2);触摸中心精灵
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);
}
}