1

I have been working on this one bit of code all week and so far have had no answer that fully work around the error I am coming across. The error is on the line var diff = Cursor.Position - playerPoint;, this error occurs because you cannot use the (-) operand between a Windows.Point and a Drawing.Point but the coding to convert it seems to only convert it for the expression it is involved in.

The commented out code is a different attempt at the same method but it produces the same error. Anyone got anything on this?

private void tmrMoving_Tick(object sender, object value, Type targetType,
            object parameter, CultureInfo culture, EventArgs e)
        {
            System.Drawing.Point dp = (System.Drawing.Point)value;
            var playerPoint = new System.Windows.Point(dp.X, dp.Y);

            var cursPoint = new System.Windows.Point(Cursor.Position.X, Cursor.Position.Y);
           // var playerPoint = new System.Windows.Point(player.Location.X, player.Location.Y);
            var diff = Cursor.Position - playerPoint;
            var speed = Math.Sqrt(diff.X * diff.X + diff.Y * diff.Y);
            if (speed > 10)
            {
                diff.X /= speed / 10;
                diff.Y /= speed / 10;
            }
            player.Location += new System.Drawing.Point((int)diff.X, (int)diff.Y);
        }

Edit If a method isn't seen through the above code there may be a resolve in the below:

private void tmrMoving_Tick(object sender, object value, Type targetType,
        object parameter, CultureInfo culture, EventArgs e)
    {

        var cursPoint = new System.Windows.Point(Cursor.Position.X, Cursor.Position.Y);
        var playerPoint = new System.Windows.Point(player.Location.X, player.Location.Y);
        var diff = Cursor.Position - playerPoint;
        var speed = Math.Sqrt(diff.X * diff.X + diff.Y * diff.Y);
        if (speed > 10)
        {
            diff.X /= speed / 10;
            diff.Y /= speed / 10;
        }
        player.Location += new System.Drawing.Point((int)diff.X, (int)diff.Y);
    }

2 Answers2

1

Since Cursor is a Windows Forms / GDI+ class, and your use of System.Windows.Point indicates you use WPF you have a problem. Those classes are not inter-exchangeable.

If you are using WPF: Instead of Cursor.Position, you can use Mouse.GetPosition(Application.Current.MainWindow); as pointed out here.

If you are using WF: Use System.Drawing.Point all the way, not System.Windows.Point.

Still, subtracting points isn't possible. You should write this yourself, or better, construct a new point and do the math in the constructor call.

Community
  • 1
  • 1
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/74244/discussion-between-ashley-fayers-and-patrick-hofman). – Ashley Fayers Apr 01 '15 at 09:11
0

You may calculate the vector from one point to the next directly on the coordinates:

int diffX = dp.X - Cursor.Position.X;
int diffY = dp.Y - Cursor.Position.Y;
var speed = Math.Sqrt(diffX * diffX + diffy * diffY);

as long as dp and Cursor.Position use the same coordinate-system i.e. 0,0 means the same for both.

DrKoch
  • 9,556
  • 2
  • 34
  • 43