反应式扩展 (RX) 旋转角度



我们都看到了鼠标拖放的良好RX处理。我想要类似的东西,但将角度的变化输出到屏幕中心。

就像如果用户点击鼠标按钮并在屏幕上转圈两次,我会得到从 0 到 720 度的值。

本质上是旋转屏幕上的东西。

怎么做呢?

这将为您提供与对象中心的角度(以弧度为单位)。但是,它只能在范围(-Pi,Pi)内运行。如果旋转三次,则不会获得高于单次旋转的值。

var mousedown = from evt in Observable.FromEventPattern<MouseButtonEventArgs>(this, "MouseDown")
        select evt.EventArgs.GetPosition(this);
var mouseup = from evt in Observable.FromEventPattern<MouseEventArgs>(this, "MouseUp")
              select evt.EventArgs.GetPosition(this);
var mousemove = from evt in Observable.FromEventPattern<MouseEventArgs>(this, "MouseMove")
                select evt.EventArgs.GetPosition(this);
Vector center = new Vector(this.Width / 2, this.Height / 2);
var radian = from start in mousedown
        from pos in mousemove.StartWith(start).TakeUntil(mouseup)
        select Math.Atan2((pos - center).Y, (pos -center).X);

编辑

如果您正在改变角度,则以下方法应该有效:

var angle = from start in mousedown
            from pos in mousemove.StartWith(start).TakeUntil(mouseup)
            select Vector.AngleBetween(pos - center, start - center);

感谢您的帮助。我自己想出了最后一点:

        var down = Observable.FromEventPattern<MouseButtonEventArgs>(this, "MouseDown").Select(e=>e.EventArgs.GetPosition(this));
        var move = Observable.FromEventPattern<MouseEventArgs>(this, "MouseMove").Select(e => e.EventArgs.GetPosition(this));
        var up = Observable.FromEventPattern<MouseEventArgs>(this, "MouseUp").Select(e => e.EventArgs.GetPosition(this));
        Vector center = new Vector(this.Width / 2, this.Height / 2);
        var f = from start in down
                from pos in move.StartWith(start).TakeUntil(up).Buffer(2, 1)
                where pos.Count == 2
                select  Vector.AngleBetween(new Vector(pos[0].X, pos[0].Y) - center, new Vector(pos[1].X, pos[1].Y) - center);

        f.ObserveOnDispatcher().Subscribe(p => {
            game.Player.Angle += p;
        });

相关内容

  • 没有找到相关文章

最新更新