Following eyes?

Just in general: how would you program/implement following eyes in 2D space? For example: A face in the middle of the screen, the eyes are following the mousepointer.

greetings

One option would be to calculate the direction from your eyes center point to the mouse cursor in 2D and add a fixed offset to place the eyes.


// pseudo-code

Vector2f eyesCenter; // assume it exists
Vector2f mouseLocation; // should be set in an update / mouse event

// later on
Vector2f direction = mouseLocation.subtract(eyesCenter);
direction.normalize();

float eye1X = fixedEye1PositionX + Math.sin(direction.x) * offset;
float eye1Y = fixedEye1PositionY + Math.cos(direction.y) * offset;


It’s just written here and not tested, maybe you need to switch sin / cos. But it should give you an implementation advice ;o)

How will you draw the updated eyes? with image or Graphics2D draw manually then use g.rotate()? if you use g.rotate() try to find the angle of those two points (mouse and eye location).