I am using scene2d and custom Actors with Spine Skeletons, AnimationStates etc. I want these custom Actors to look at things by providing a (worldX, worldY) in stage coordinates. I make this work by adding pupilBones and use their length to calculate the position of the pupils. There is, however, one thing I don't understand; I have to subtract 90 degrees when calculating the position of the pupils, else they look 90 degrees wrong. Could someone tell me what I am missing here? I would guess it has something to do with local/parent/global coordinates, but I cannot really see how that matters here.
/**Makes the owner {@link Sock} look at the argument point in world coordinates.*/
private void lookAt(float worldX, float worldY){
/*Reset pupil positions*/
owner.getLeftPupilBone().setToSetupPose();
owner.getRightPupilBone().setToSetupPose();
/*Get world position of the left eye.*/
float leftEyeX = owner.getLeftPupilBone().getWorldX() + owner.getX();
float leftEyeY = owner.getLeftPupilBone().getWorldY() + owner.getY();
/*Get world position of the right eye.*/
float rightEyeX = owner.getRightPupilBone().getWorldX() + owner.getX();
float rightEyeY = owner.getRightPupilBone().getWorldY() + owner.getY();
/*Calculate angle for both eyes, in radians.*/
float angleLeftEye = MathUtils.atan2(worldY - leftEyeY, worldX - leftEyeX);
float angleRightEye = MathUtils.atan2(worldY - rightEyeY, worldX - rightEyeX);
/*Calculate new position for the pupils. Have to subtract 90 degrees to make it look correct.*/
float newLeftPupilPosX = owner.getLeftPupilBone().getX() + owner.getLeftPupilBone().getData().getLength() * (float) MathUtils.cos(angleLeftEye-MathUtils.PI/2.0f);
float newLeftPupilPosY = owner.getLeftPupilBone().getY() + owner.getLeftPupilBone().getData().getLength() * (float) MathUtils.sin(angleLeftEye-MathUtils.PI/2.0f);
float newRightPupilPosX = owner.getRightPupilBone().getX() + owner.getRightPupilBone().getData().getLength() * (float) MathUtils.cos(angleRightEye-MathUtils.PI/2.0f);
float newRightPupilPosY = owner.getRightPupilBone().getY() + owner.getRightPupilBone().getData().getLength() * (float) MathUtils.sin(angleRightEye-MathUtils.PI/2.0f);
/*Set the new position for the pupils.*/
owner.getLeftPupilBone().setX(newLeftPupilPosX);
owner.getLeftPupilBone().setY(newLeftPupilPosY);
owner.getRightPupilBone().setX(newRightPupilPosX);
owner.getRightPupilBone().setY(newRightPupilPosY);
}