I'm having a difficult time emulating an 'eyeball' effect. I have an
Dim MousePosition As Drawing.Point
Dim ctrl As System.Windows.Forms.Control
For Each ctrl In Me.Controls
AddHandler ctrl.MouseMove, AddressOf OnMouseMoveHandler
Dim canvas As New ShapeContainer
canvas.Parent = Me.EyeIcon
Me.EyePupilShape.Parent = canvas
MousePosition = Me.PointToClient(Cursor.Position)
Debug.Print("Mouse position : X = " & MousePosition.X & ", " & MousePosition.Y)
Me.EyePupilShape.Location = New Drawing.Point(Clamp(MousePosition.X, 32, 50), Clamp(MousePosition.Y, 31, 50))
You need to take the angle to the mouse cursor into account, then calculate position on a unit circle based on that angle, and scale by radius of your "pupil movement area", adjusting for mouse position being inside the eye/pupil.
I wrote a sample in VB.NET, posted on Github.
Calculations are within the
Eyeball.Pupil.Update(Point mouse) function, which receives the mouse coordinates translated using
PointToClient() function, as in your case. Here's the code:
Public Sub Update(ByVal mouse As Point) 'Calculate angle to mouse position' Dim distanceVector = New PointF(mouse.X - _center.X, mouse.Y - _center.Y) Dim angleToMouse = Math.Atan2(distanceVector.Y, distanceVector.X) 'If the mouse is within the movement radius, restrict movement' Dim absDistanceVector As PointF = distanceVector If absDistanceVector.X < 0 Then absDistanceVector.X *= -1 If absDistanceVector.Y < 0 Then absDistanceVector.Y *= -1 'Calculate scale' Dim scale = New PointF(Math.Min(absDistanceVector.X, _movementRadius.X), Math.Min(absDistanceVector.Y, _movementRadius.Y)) 'Adjust X and Y of the pupil based on scaled vector to mouse cursor, offset by pupil origin' X = CType(Math.Cos(angleToMouse), Single) * scale.X + _center.X - BoundingBox.Width / 2 Y = CType(Math.Sin(angleToMouse), Single) * scale.Y + _center.Y - BoundingBox.Height / 2 End Sub
Ignore the closing single-quotes at the end of the comments - I added these to properly colorize the code in this post.