所以当你有3分时,我想出了如何在曲线中移动东西。 我将我的精灵移动成这样的曲线:
以下代码位于render方法中,它循环每个tick。
if (ship.isMoving()){
// Normalized direction vector towards target
Vector2 dir = ship.getEndPoint().cpy().sub(ship.getLinearVector()).nor();
// Move towards target by adding direction vector multiplied by speed and delta time to linearVector
ship.getLinearVector().add(dir.scl(2 * Gdx.graphics.getDeltaTime()));
// calculate step based on progress towards target (0 -> 1)
float step = 1 - (ship.getEndPoint().dst(ship.getLinearVector()) / ship.getDistanceToEndPoint());
if (ship.getCurrentPerformingMove() != MoveType.FORWARD) {
// step on curve (0 -> 1), first bezier point, second bezier point, third bezier point, temporary vector for calculations
Bezier.quadratic(ship.getCurrentAnimationLocation(), step, ship.getStartPoint().cpy(),
ship.getInbetweenPoint().cpy(), ship.getEndPoint().cpy(), new Vector2());
}
else {
Bezier.quadratic(ship.getCurrentAnimationLocation(), step, ship.getStartPoint().cpy(),
new Vector2(ship.getStartPoint().x, ship.getEndPoint().y), ship.getEndPoint().cpy(), new Vector2());
}
// check if the step is reached to the end, and dispose the movement
if (step >= 0.99f) {
ship.setX(ship.getEndPoint().x);
ship.setY(ship.getEndPoint().y);
ship.setMoving(false);
System.out.println("ENDED MOVE AT "+ ship.getX() + " " + ship.getY());
}
else {
// process move
ship.setX(ship.getCurrentAnimationLocation().x);
ship.setY(ship.getCurrentAnimationLocation().y);
}
// tick rotation of the ship image
if (System.currentTimeMillis() - ship.getLastAnimationUpdate() >= Vessel.ROTATION_TICK_DELAY) {
ship.tickRotation();
}
}
当我运行它时,80%的时间它没有问题顺利运行,但有时它会运行,并且在两个动作之间只有一些奇怪的延迟(如果我做第一个曲线然后是另一个曲线),就像是我在那里不明白。
我是否使用delta错误?
答案 0 :(得分:1)
正如@ Tenfour04对您的问题发表了评论。可能是垃圾收集器开始并造成滞后。不要在更新/渲染循环中创建新对象。
// instance variable tmp
private Vector2 tmp = new Vector2();
// dir is now a reference to tmp no new objects allocated
Vector2 dir = this.tmp.set(ship.getEndPoint()).sub(ship.getLinearVector()).nor();
// the same thing with Bezier.quadratic equation
// only the currentAnimationLocation and tmp will be modified in the method
Bezier.quadratic(
ship.getCurrentAnimationLocation(), step, ship.getStartPoint(),
ship.getInbetweenPoint(), ship.getEndPoint(), this.tmp
);