【问题标题】:How to get a change in mouse position difference using Cocoa?如何使用 Cocoa 改变鼠标位置差异?
【发布时间】:2012-06-16 23:05:09
【问题描述】:

我正在使用 opengl 并尝试创建第一人称相机。所有示例都使用 GLUT,我需要在可可中获取鼠标差异。但是我遇到了与 mouseMoved 相关的问题,一旦鼠标移动就会被调用(这是可以预料的)。有没有办法使这个mroe准确?还是类似 GLUTS glutMouseFunc 的函数?

当前尝试:

-(void)mouseMoved:(NSEvent *)event{
        static bool wrap = false;

        if(!wrap){
            NSPoint eventLocation = [event locationInWindow];

            float centerX = self.frame.size.width/2 + [self window].frame.origin.x;
            float centerY = self.frame.size.height/2 + [self window].frame.origin.y;
            CGPoint mousePointCenter = CGPointMake(centerX, centerY);
            CGWarpMouseCursorPosition(mousePointCenter);

            int dx = eventLocation.x - self.frame.size.width/2 ;
            int dy = eventLocation.y - self.frame.size.height/2 ;

            const float mousespeed = 0.001;

            angles.x += dx * mousespeed;
            angles.y += dy * mousespeed;

            if(angles.x < -M_PI)
              angles.x += M_PI * 2;
            else if(angles.x > M_PI)
              angles.x -= M_PI * 2;

            if(angles.y < -M_PI / 2)
              angles.y = -M_PI / 2;
            if(angles.y > M_PI / 2)
              angles.y = M_PI / 2;

            lookat.x = sinf(angles.x) * cosf(angles.y);
            lookat.y = sinf(angles.y);
            lookat.z = cosf(angles.x) * cosf(angles.y);

           CGWarpMouseCursorPosition(mousePointCenter);

          [self setNeedsDisplay:YES];
        }
        else{
            wrap = true;
        }

}

【问题讨论】:

  • 你能分享你的完整代码吗...

标签: c++ macos cocoa opengl


【解决方案1】:

我不确定我是否遵循了您的代码应该做的事情,但反复将鼠标光标扭曲到中心点很少是正确的做法。

首先,您可以使用NSEvent 的deltaX 和deltaY 值。

也许你想做CGAssociateMouseAndMouseCursorPosition(false) 来解除鼠标与光标位置的关联。当您这样做时,屏幕上的光标不再随鼠标移动。你可以隐藏它或重新定位它(是的,在这种情况下你会扭曲它一次)。此外,事件的绝对位置不再发生变化。但它们仍然带有反映鼠标移动的增量移动值。

【讨论】:

  • 太棒了,完美运行。我是古玩,你可能不知道,但你能向我解释一下这段代码的三角部分是如何工作的吗?它用于根据您更改光标位置的程度来确定您应该如何更改查看向量。我了解基本的触发,只是不清楚这到底是做什么的。
  • 不确定。我已经很久没有做trigger了。看起来angles 在极坐标中,并且正在转换为笛卡尔坐标。它类似于但不完全如下所示:en.wikipedia.org/wiki/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-27
  • 1970-01-01
相关资源
最近更新 更多