gpt4 book ai didi

java - 路径行走/跟随不适用于所有情况

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:02:05 24 4
gpt4 key购买 nike

我正在按照描述实现路径行走/跟随 hereon this video .唯一的区别是我已将其转换为使用 Scene2DStage

除此部分外,它工作正常:当下一个点的 Y 值小于上一个点的 Y 值时, Sprite 基本上从上一个点直接跳到下一个点。

例如:在下图中, Sprite 从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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com