public void moveInDirection(CGPoint direction, double timeInterval) { CGPoint curPosition = getPosition(); double dx = movementSpeed * direction.getX(); double dy = movementSpeed * direction.getY(); double dt = movementSpeed * timeInterval; CGPoint targetPosition = new CGPoint(curPosition.getX() + dx, curPosition.getY() + dy); double ang = APAUtils.polarAdjust(APAUtils.getRadiansBetweenPoints(targetPosition, curPosition)); setZRotation(ang); double distRemaining = Math.hypot(dx, dy); if (distRemaining < dt) { setPosition(targetPosition); } else { setPosition( new CGPoint( curPosition.getX() - Math.sin(ang) * dt, curPosition.getY() + Math.cos(ang) * dt)); } // Don't change to a walk animation if we planning an attack. if (!attacking) { requestedAnimation = APAAnimationState.Walk; } }
public void moveTowards(CGPoint position, double timeInterval) { CGPoint curPosition = getPosition(); double dx = position.getX() - curPosition.getX(); double dy = position.getY() - curPosition.getY(); double dt = movementSpeed * timeInterval; double ang = APAUtils.polarAdjust(APAUtils.getRadiansBetweenPoints(position, curPosition)); setZRotation(ang); double distRemaining = Math.hypot(dx, dy); if (distRemaining < dt) { setPosition(position); } else { setPosition( new CGPoint( curPosition.getX() - Math.sin(ang) * dt, curPosition.getY() + Math.cos(ang) * dt)); } requestedAnimation = APAAnimationState.Walk; }