我很难模仿一个眼球'影响。我有一个from sfml import sf
w = sf.RenderWindow(sf.VideoMode(600,400),"pySFML Window")
WIDTH = 600
HEIGHT = 400
w.size = (WIDTH, HEIGHT)
w.clear(sf.Color.GREEN)
w.title = "Window"
while(True):
if(sf.Keyboard.is_key_pressed(sf.Keyboard.ESCAPE)):
w.close()
w.display()
(与PowerPack形状一样)并且可以将运动约束到特定的x,y。但是,我需要一双新鲜的眼睛来帮助我,我想我已经看了太长时间才能理解它。
目标:
按照光标位置模拟眼球运动体验。
具体细节:
光标位置是OvalShape
类型,ergo,它相对于表单。
对象/形状相对于PointToClient
,因此MousePosition 10,10将转换为EyePupil位置10,10,因为该位置基于它的父级
这是一个概要:
ShapeContainer
处理事件:
Class
Dim MousePosition As Drawing.Point
.....Handles MyBase.Load
Dim ctrl As System.Windows.Forms.Control
For Each ctrl In Me.Controls
AddHandler ctrl.MouseMove, AddressOf OnMouseMoveHandler
Next
Dim canvas As New ShapeContainer
canvas.Parent = Me.EyeIcon
Me.EyePupilShape.Parent = canvas
其中Clamp只是MathHelper.clamp的自定义版本,而最小/最大值是' pupil'应该将眼睛约束在其父元素
中这样可以将瞳孔限制在特定的x,y界限
然而,它并没有遵循'每个光标。例如,如果光标位于左下角,则它不会识别出它只是基于x,y值,我到目前为止需要帮助。对于它的价值,我从MSDN获得了大部分信息
答案 0 :(得分:2)
您需要考虑鼠标光标的角度,然后根据该角度计算单位圆上的位置,并按照"瞳孔运动区域的半径进行缩放",调整鼠标位置为在眼睛/瞳孔内。
我在VB.NET中编写了一个示例posted on Github。
计算在Eyeball.Pupil.Update(Point mouse)
函数内,该函数接收使用PointToClient()
函数转换的鼠标坐标,如您的情况。这是代码:
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