2015-02-28 07:05:31
Programming
, 2015-02-28 07:05:31

2D movement not working?

private double GetAngle(double x1,double y1,double x2,double y2)
        {
            double ang = Math.Atan((x1 - x2) / (y1 - y2)) * Rad2Deg;
            if ((x1 < x2) && (y1 > y2))
                return ang;
            else if (((x1 < x2) && (y1 < y2)) || ((x1 > x2) && (y1 < y2)))
                return ang + 180d;
            else
                return ang + 360d;
        }

        public void MoveTo(double x, double y)
        {
            TX = x; TY = y;
            Angle = GetAngle(TX, TY, X, Y);
            TimerMove.Interval = 10;
            TimerMove.Start();
        }

        private void Move(object sender, EventArgs e)
        {
            X += Speed * (Math.Sin(Angle * Rad2Deg));
            Y += Speed * (Math.Cos(Angle * Rad2Deg));
        }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mrrl, 2015-02-28
Chevengur

If the Rad2Deg constant is defined as 180/pi, then in the Move function it is necessary not to multiply by it, but to divide.
And in general, it would be nice to at least briefly describe what you want to get and what you get.

S
Stanislav Silin, 2015-02-28
@byme

Chet tells me that you do not have a mainloop, where it all works, or it is, but the functions are not called there. Do you have mainloop("the main loop where all changes are processed")?

TimerMove.Interval = 10;
TimerMove.Start();

If I used my clairvoyance correctly, then you will have a slight shift in 10 seconds, and then it will all be over ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question