1

In the program that I'm working on, I have an object (the player) in the shape of a triangle, and that triangle is supposed to rotate always facing the mouse. given this two points I have tried different equations I've found online but non of them seem to work or at least preform well enough.

delta_x = cursor.X - pos.X;
delta_y = cursor.Y - pos.Y;
cursorAngle = (float)Math.Atan2(delta_y, delta_x) * (float)(180 / Math.PI);

this is the most efficient formula I found but it is still not working well enough, since it only faces the mouse at specific angles or distances. Cursor.X and .Y are the coordinates of the mouse and pos.X and .Y are the coordinates of the player.

insignia
  • 51
  • 1
  • 10
  • 2
    You can't have an angle with just 2 points, you're going to need a third point. – Ido H Levi Mar 05 '19 at 19:59
  • 1
    maybe he means the origin is the 3rd point – TaW Mar 05 '19 at 20:04
  • 1
    The formula should do just fine actually, are you sure you aren't mixing up radians and degrees? Remember that `cursorAngle` retrieves the value in degrees (thanks to the `180/Math.PI` part. – fhcimolin Mar 05 '19 at 20:10
  • i am using windows form, for the initial position of the player i set it to x = width /2 and y = height / 2, from there the user can move in any direction. and its also receiving the location of the mouse, what seems to be the problem is that the distance between the mouse and the player seems to affect the angle dramatically thus not being accurate. i seen that the close the distance between the two, the smaller the number, so if the mouse is at the same angle, and i pull it closer or further from the player, the player starts to rotate – insignia Mar 05 '19 at 20:40
  • @IdoHLevi I was thinking that the third Point would be the one to create a right triangle between all of them. thats why im looking for a formula which finds the angle to rotate the player by creating a right triangle between those 2 points. the idea is to rotate the player to match the Hypotenuse. – insignia Mar 05 '19 at 20:47
  • 1
    @insignia - Given only two points, there are _infinite_ right triangles that can be created between them. – Chris Dunaway Mar 05 '19 at 22:29
  • @ChrisDunaway correct me if im wrong, but im pretty sure if you have 2 diagonal points on a plane, the only way you can connect them with right triangles is if you use 2 right triangles, making it a square. I don't think that there are more than 2. – insignia Mar 07 '19 at 08:22
  • @insignia - Since you can draw a line from one of the points at any angle, you can also connect the other point to that line at 90 degrees. – Chris Dunaway Mar 07 '19 at 14:37

1 Answers1

8

I created this WinForm example that calculates the angle and distance of the mouse from the center of the form every time you move the mouse on the form. The result I display in a label.

enter image description here

The red dot in the center of the form is just a reference panel and has no relevance in the code.

    private void f_main_MouseMove(object sender, MouseEventArgs e)
    {
        Point center = new Point(378, 171);
        Point mouse = this.PointToClient(Cursor.Position);

        lb_mouseposition.Text = $"Mouse Angle: {CalculeAngle(center, mouse)} / Distance: {CalculeDistance(center, mouse)}";
    }


    private double CalculeAngle(Point start, Point arrival)
    {
        var deltaX = Math.Pow((arrival.X - start.X), 2);
        var deltaY = Math.Pow((arrival.Y - start.Y), 2);

        var radian = Math.Atan2((arrival.Y - start.Y), (arrival.X - start.X));
        var angle = (radian * (180 / Math.PI) + 360) % 360;

        return angle;
    }

    private double CalculeDistance(Point start, Point arrival)
    {
        var deltaX = Math.Pow((arrival.X - start.X), 2);
        var deltaY = Math.Pow((arrival.Y - start.Y), 2);

        var distance = Math.Sqrt(deltaY + deltaX);

        return distance;
    }

The angle is here shown in degrees varying from 0 to 359. I hope this helps in calculating the angle between your two points.

Alberto Santos
  • 357
  • 1
  • 9
  • yes it works, but i have a problem and i think it has to do with the method im using for rotation of the object `e.Graphics.TranslateTransform(pos.X, pos.Y); e.Graphics.RotateTransform((float)cursorAngle); e.Graphics.TranslateTransform(-pos.X, -pos.Y);` it seems to rotate it well till it gets to a certain angle and then does a quick 360. i got this code from a sample code a friend gave me some time ago – insignia Mar 06 '19 at 19:25
  • Try using `using (Matrix draw = new Matrix())` along with `draw.RotateAt()` will help you. – Alberto Santos Mar 06 '19 at 21:50