Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

Capture Mouse Moving Direction RSS

7 replies

Last post Oct 15, 2007 02:22 PM by nirav5884

(0)
  • nirav5884

    nirav5884

    Member

    110 Points

    110 Posts

    Capture Mouse Moving Direction

    Oct 12, 2007 09:59 AM | LINK

    How can i capture mouse moving direction?

    I want to know mouse direction that Is it anticlockwise or is it clockwise?

    Is there any way to know?

    In my application i want to rotate arrow as per mouse moving direction. If my mouse is moving in clockwise then i want to increase the rotate angle otherwise i want to decrease the rotate angle of arrow. 

  • chrishay_uk

    chrishay_uk

    Participant

    876 Points

    213 Posts

    Re: Capture Mouse Moving Direction

    Oct 12, 2007 10:05 AM | LINK

    You will hate this answer, but you could track the last couple of x,y coordinates, and then calculate if its anti-clockwise by comparing the current X,Y to the previous.

    If this has answered your question, please hit the Mark as Answered thingy.

    http://silverlightuk.blogspot.com/
  • nirav5884

    nirav5884

    Member

    110 Points

    110 Posts

    Re: Capture Mouse Moving Direction

    Oct 15, 2007 10:47 AM | LINK

    Thanks chirshay_uk for your reply,

    Ok i can compare last couple of x,y coordinates. But how can i know that my mouse is moving in clockwise or anticlockwise direction. 

    There isn't any specific x,y coordinates from that i can capture the mouse direction.

    Can you or anybody help me more?

     

    -Nirav 

  • Mimiste

    Mimiste

    Member

    156 Points

    33 Posts

    Re: Capture Mouse Moving Direction

    Oct 15, 2007 12:14 PM | LINK

    like Chrishay said you capture the last and the current mouse position then

     if Xlast - Xcurrent = a negative number you are moving clockwise else you are moving anticlockwise

    think it's something like that [*-)]

    Loïc

    If your question was answered, please mark the response as the answer.

    Silverlight apps :
    - Photo Gallery (Silverlight 1.1 C# PHP)
    - Silverball Game (Silverlight 1.1 C#)
  • chrishay_uk

    chrishay_uk

    Participant

    876 Points

    213 Posts

    Re: Re: Capture Mouse Moving Direction

    Oct 15, 2007 12:27 PM | LINK

    Exactly, Mimiste

    If this has answered your question, please hit the Mark as Answered thingy.

    http://silverlightuk.blogspot.com/
  • y_makram

    y_makram

    Contributor

    6952 Points

    1353 Posts

    Re: Re: Capture Mouse Moving Direction

    Oct 15, 2007 02:15 PM | LINK

    No it will not work this way, you will need to calculate the angle between the two vectors originating from the center of the rotated object to the new point and the previous point

    for example to rotate an object based on mouse drag:

    onRotateMouseDown = function(sender, args){
        this.isRotating = true;
        sender.captureMouse();
       
        this.lastXPosition = args.getPosition(sender).x - (sender.width/2);
        this.lastYPosition = args.getPosition(sender).y - (sender.height/2);
        this.angle = sender.renderTransform.angle;
    }

    onRotateMouseMove = function(sender, args){
        if(this.isRotating){
           var x = (args.getPosition(sender).x - (sender.width/2));
           var y = (args.getPosition(sender).y - (sender.height/2));
           var theta = Math.atan2(y, x) - Math.atan2(this.lastYPosition, this.lastXPosition);
           var angleInDegrees = Math.round(theta*180/Math.PI);
           sender.renderTransform.angle = this.angle + angleInDegrees;
        }

    onRotateMouseUp = function(sender){
        this.isRotating = false;
        sender.releaseMouseCapture();
    }

    Thanks
    Yasser Makram
    Independent Consultant
    http://www.silverlightrecipes.com
    _____
    Dont forget to click "Mark as Answer" on the post that helped you.
  • y_makram

    y_makram

    Contributor

    6952 Points

    1353 Posts

    Re: Re: Capture Mouse Moving Direction

    Oct 15, 2007 02:16 PM | LINK

    And by the way I am assuming in the code that that the object rendertransform is of type rotatetransform 

    Thanks
    Yasser Makram
    Independent Consultant
    http://www.silverlightrecipes.com
    _____
    Dont forget to click "Mark as Answer" on the post that helped you.
  • nirav5884

    nirav5884

    Member

    110 Points

    110 Posts

    Re: Re: Capture Mouse Moving Direction

    Oct 15, 2007 02:22 PM | LINK

    Hi yaseer and all,

    Thanks yaseer for your reply with explanation.

    Please can you convert or give me code in C#.Net?

    Actually i am rotation Line Object.

    I can rotate my line on both direction but line is not rotating properly when my mouse rotates.

    My Code is:

                                    Point actPos = e.GetPosition(MyCanvas);

                                    if (oActiveLine != null && sElementName == "Line")
                                    {
                                        //Fidning the angle using Triangle theorem.
                                        double a, b, c;
                                        X.X = oActiveLine.Left;
                                        X.Y = oActiveLine.Top;
                                        Y.X = structStartPosition.X;
                                        Y.Y = structStartPosition.Y;
                                        Z.X = actPos.X;
                                        Z.Y = actPos.Y;
                                        b = System.Math.Sqrt(((Y.X - oActiveLine.Left) * (Y.X - oActiveLine.Left)) + ((Y.Y - oActiveLine.Top) * (Y.Y - oActiveLine.Top)));
                                        oActiveLine.Height = System.Math.Sqrt(((actPos.X - oActiveLine.Left) * (actPos.X - oActiveLine.Left)) + ((actPos.Y - oActiveLine.Top) * (actPos.Y - oActiveLine.Top)));
                                        a = System.Math.Sqrt(((Y.X - Z.X) * (Y.X - Z.X)) + ((Y.Y - Z.Y) * (Y.Y - Z.Y)));
                                        c = System.Math.Sqrt(((X.X - actPos.X) * (X.X - actPos.X)) + ((X.Y - actPos.Y) * (X.Y - actPos.Y)));

                                        double angle = System.Math.Acos((b * b + c * c - a * a) / (2 * b * c)) * (180.0 / System.Math.PI);
                                        if (((Z.Y < X.Y) && (Z.X < Y.X)) || ((Z.Y > X.Y) && (Z.X > Y.X)))
                                            oActiveLine.Angle -= angle;
                                        if (((Z.Y < X.Y) && (Z.X > Y.X)) || ((Z.Y > X.Y) && (Z.X < Y.X)))
                                            oActiveLine.Angle += angle;
                                        angle = 0.0;
                                        a = b = c = 0;
                                       
                                    }

                                    structStartPosition = actPos;