我对AI之后的简单路径有一点问题。
如果我使用setRotation()
精灵突然旋转,但我需要以渐进和自然的方式旋转它。
我对三角学知识很少,所以我在这里问。
public class AISprite extends Sprite {
private Vector2 velocity = new Vector2();
private float speed = 100;
private Array<Vector2> path; // it contains viewpoints
private int waypoint = 0; // index of actual waypoint
public AISprite(Sprite sprite, Array<Vector2> path){
super(sprite);
this.path = path;
}
@Override
public void draw(SpriteBatch spriteBatch){
update(Gdx.graphics.getDeltaTime());
super.draw(spriteBatch);
}
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);
rotation(angle);
// if the last waypoint is reached, start again
if(isWaypointReached()){
if(waypoint + 1 >= path.size)
waypoint = 0;
else
waypoint++;
}
}
private void rotazione(float angle){
setRotation(angle*MathUtils.radiansToDegrees);
}
private boolean isWaypointReached() {
return Math.abs(path.get(waypoint).x - getX()) <= speed * Gdx.graphics.getDeltaTime()
&&
Math.abs(path.get(waypoint).y - getY()) <= speed * Gdx.graphics.getDeltaTime();
}
public Array<Vector2> getPath(){
return path;
}
public int getWaypoint(){
return waypoint;
}