public void sendBack(float dist) { int index = mWayPointIndex - 1; Vector2 pos = getPosition().copy(); while (index > 0) { Vector2 wp = mPath.get(index); Vector2 toWp = Vector2.fromTo(pos, wp); float toWpLen = toWp.len(); if (dist > toWpLen) { dist -= toWpLen; pos = wp; index--; } else { pos = toWp.norm().mul(dist).add(pos); setPosition(pos); mWayPointIndex = index + 1; return; } } setPosition(mPath.get(0)); mWayPointIndex = 1; }
public Vector2 getPositionAfter(float sec) { if (mPath == null) { return getPosition(); } float distance = sec * getSpeed(); int index = mWayPointIndex; Vector2 position = getPosition().copy(); while (index < mPath.count()) { Vector2 toWaypoint = mPath.get(index).copy().sub(position); float toWaypointDist = toWaypoint.len(); if (distance < toWaypointDist) { return position.add(toWaypoint.mul(distance / toWaypointDist)); } else { distance -= toWaypointDist; position.set(mPath.get(index)); index++; } } return position; }