我正在按照here和on this video的描述实现路径遍历/跟随。
唯一的区别是我已将其转换为使用Scene2D
和Stage
代替。
除以下部分外,它工作正常:
当下一个点的Y小于上一个点的Y时,精灵基本上从前一个点直接跳到下一个点。
例如:
在下图中,精灵从点A直接跳到B,从点C跳到D。
这是实现Screen
的完整类。
import java.util.ArrayList;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.utils.viewport.StretchViewport;
public class WayPointsTutorial implements Screen {
private Stage stage;
private SpriteBatch batch;
private ShapeRenderer renderer;
private Sprite sprite;
private TestPlayer player;
public static ArrayList<Vector2> pathPoints;
@Override
public void show() {
stage = new Stage(new StretchViewport(800,1280));
InputMultiplexer inputM = new InputMultiplexer();
inputM.addProcessor(stage);
Gdx.input.setInputProcessor(inputM);
renderer = new ShapeRenderer();
batch = new SpriteBatch();
sprite = new Sprite(new Texture("data/bt_comecar.png"));
sprite.setSize(50, 50);
sprite.setOrigin(0, 0);
pathPoints = new ArrayList<Vector2>();
pathPoints.add(new Vector2(170,550));
pathPoints.add(new Vector2(40,40));
pathPoints.add(new Vector2(150,40));
pathPoints.add(new Vector2(608,1000));
pathPoints.add(new Vector2(400,208));
player = new TestPlayer(sprite, pathPoints);
player.setPosition(400, 640);
stage.addActor(player);
}
@Override
public void render(float delta) {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
//Update the stage
stage.draw();
stage.act(delta);
if(Gdx.input.isTouched()){show();}
stage.getCamera().update();
batch.setProjectionMatrix(stage.getCamera().combined);
renderer.setProjectionMatrix(batch.getProjectionMatrix());
renderer.setTransformMatrix(batch.getTransformMatrix());
renderer.setColor(Color.CYAN);
renderer.begin(ShapeType.Line);
renderer.line(new Vector2(player.getX(), player.getY()), player.getPath().get(player.getWaypoint()));
renderer.end();
Vector2 previous = player.getPath().get(0);
for(Vector2 waypoint : player.getPath()) {
renderer.setColor(Color.WHITE);
renderer.begin(ShapeType.Line);
renderer.line(previous, waypoint);
renderer.end();
renderer.begin(ShapeType.Filled);
renderer.circle(waypoint.x, waypoint.y, 15);
renderer.end();
previous = waypoint;
}
}
@Override
public void resize(int width, int height) {}
@Override
public void dispose() {
stage.dispose();
renderer.dispose();
}
@Override
public void hide() {dispose();}
@Override
public void pause() {}
@Override
public void resume() {}
public class TestPlayer extends Actor {
private Vector2 velocity = new Vector2();
private float speed = 300, tolerance = 50;
private ArrayList<Vector2> path;
private int waypoint = 0;
Sprite sprite;
public TestPlayer(Sprite sprite, ArrayList<Vector2> pathPoints) {
//super(sprite);
this.path = pathPoints;
this.sprite = sprite;
}
@Override
public void draw(Batch batch, float parentAlpha) {
update(Gdx.graphics.getDeltaTime());
sprite.setPosition(this.getX(), this.getY());
sprite.setRotation(this.getRotation());
sprite.draw(batch);
}
public void update(float delta) {
float angle = (float) Math.atan2(path.get(waypoint).y - getY(), path.get(waypoint).x - getX());
velocity.set((float) Math.cos(angle) * speed, (float) Math.sin(angle) * speed);
setPosition(getX() + velocity.x * delta, getY() + velocity.y * delta);
setRotation(angle * MathUtils.radiansToDegrees);
if(isWaypointReached(waypoint)) {
setPosition(path.get(waypoint).x, path.get(waypoint).y);
if(waypoint + 1 >= path.size()){
waypoint=0;
}else{
waypoint++;
}
}
}
public boolean isWaypointReached(int waypoint) {
return path.get(waypoint).x - getX() <= speed / tolerance * Gdx.graphics.getDeltaTime() && path.get(waypoint).y - getY() <= speed / tolerance * Gdx.graphics.getDeltaTime();
}
public ArrayList<Vector2> getPath() {
return path;
}
public int getWaypoint() {
return waypoint;
}
}
}
最佳答案
我认为(我没有检查)您的isWaypointReached()
方法已损坏。如果您使用新的waypoint.x < x
和新的waypoint.y < y
,则在公式中,您将在waypoint).x - getX()
和waypoint).y - getY()
部分中得到负值,这肯定比总的正speed / tolerance * Gdx.graphics.getDeltaTime()
小。在这种情况下,方法将立即返回到达的航路点。
尝试将Math.abs()
函数添加到您的公式中:
return Math.abs(path.get(waypoint).x - getX()) <= speed / tolerance * Gdx.graphics.getDeltaTime() && Math.abs(path.get(waypoint).y - getY()) <= speed / tolerance * Gdx.graphics.getDeltaTime();
关于java - 路径遍历/跟踪并非在所有情况下都有效,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27650491/