   Moving an object between 2 points.
  Hey guys, I'm new on GD.net :) I am currently learning to code C# and Unity.Anyway, I have this problem, which I hope some of you can help me with. I've spend some time thinking about what forum to put it in, so if it's in the wrong on, please don't hesitate to throw some notices at me.I am messing around with some a Top-down RTS in a windows application, and I need help to script the movement of my placeholder (called btnUnit).I have to variables, mouseX and mouseY, which is constantly being set to the cursor's X and Y position in Form_MouseMove-method. I want my little btnUnit to move towards the cursor's XY position whenever I click on the application.I hardly think there's a method for this particular problem, so you might need some great math skills to help out at this ;)I'm looking forward to a reply or two!- Mikkelet   
  You'd benefit from learning about vector maths for this sort of thing.If you treat the mouse position x,y as a 2D vector (M) and the unit's position x,y as another (U), you can find a vector pointing from U to M by doing M-U where vector subtraction is defined as a simple per-component subtraction:M-U = (M.x-U.x,M.y-U.y)If you now do something called normalising this vector, it will produce a vector that points in the same direction but of length 1 in whatever units you are working.To normalise the vector you first need to find its length, which is basic pythagorus:length of V = sqrt((V.x*V.x)+(V.y*V.y))If you think of the vector as the hypotenuse of a triangle and the horizontal and vertical differences between the end points of the vector as the adjacent an opposite sides of a triangle, this is identical to the rule that the square of the length of the hypotenuse is equal to the sum of the squares of the other two sides.So normalising M-U gives a unit vector (vector of length 1) pointing from U to M.You can multiply this vector by the distance you want to travel each frame. Vector times a scalar is just per component multiplication:V*3 = (V.x*3,V.y*3)Finally, you can add this to U to move the unit that distance towards M:S=(M-U)*stepU+S = (U.x+S.x,U.y+S.y)where step is the distance to travel.If all of that is gibberish, just have a google for 2D vector maths and get familiar with the concepts.   ;  That was a great help! Thank you! Now, this is my current movement-code:unitVelocityX = (int)Math.Sqrt((e.X - btnUnit.Left) * (e.X - btnUnit.Left)) / 10;unitVelocityY = (int)Math.Sqrt((e.Y - btnUnit.Top) * (e.Y - btnUnit.Top)) / 10;(dividing by 10 to slow it down)However, my btnUnit move exactly the direction I want, but not at the same speed. If the XY position of the btnUnit is (0,0) and I click on the application at about (200,200), the btnUnit moves alot faster than if I clicked at fx (50,50).Maybe this was the last part you explained, but I didn't quite it:S=(M-U)*stepU+S = (U.x+S.x,U.y+S.y)But again, thanks alot! :)- Mikkelet   ;Quote:Original post by Mikkelet(dividing by 10 to slow it down)...However, my btnUnit move exactly the direction I want, but not at the same speed....Maybe this was the last part you explained, but I didn't quite it:All of this is basic linear algebra.You have probably noticed that all your objects require a matrix or two, and they are moved with vectors.Linear algebra is the branch of mathematics that covers matrix and vector operations.It is either required or recommend course in most Computer Science programs.  If you are not that far in school you can still learn the topics on your own, just as you are learning programming and other technical skills.You need at least basic understanding of the math to develop games.  A solid understanding of linear algebra is essential if you want to get serious with 3D engine work.I strongly recommend you find an introductory book on linear algebra and work through it.  If you can find a teacher to help answer questions that would be even better.   ;Quote:Original post by frobQuote:Original post by Mikkelet(dividing by 10 to slow it down)...However, my btnUnit move exactly the direction I want, but not at the same speed....Maybe this was the last part you explained, but I didn't quite it:All of this is basic linear algebra.You have probably noticed that all your objects require a matrix or two, and they are moved with vectors.Linear algebra is the branch of mathematics that covers matrix and vector operations.It is either required or recommend course in most Computer Science programs.  If you are not that far in school you can still learn the topics on your own, just as you are learning programming and other technical skills.You need at least basic understanding of the math to develop games.  A solid understanding of linear algebra is essential if you want to get serious with 3D engine work.I strongly recommend you find an introductory book on linear algebra and work through it.  If you can find a teacher to help answer questions that would be even better.You're of couse right - books can solve any problem. I was hoping I could get some quality help to this (I really didn't know it would be so advanced to make a button move), so I don't go wrong somewhere :)I'll try to look into it! Replies on this matter may however still be posted and is greatly appreciated!- Mikkelet;  It's a good idea to set up a vector class for this. We want the following operations:Add vector to vectorSubtract vector from vectorGet vector lengthNormalise vectorMultiply vector by scalar (single number)// typed straight into browser so no guarantees :)class vec{public:    vec(float x,float y) : x(x),y(y) { }    float x,y;    vec operator+(vec v) const { return vec(x+v.x,y+v.y); }    vec operator-(vec v) const { return vec(x-v.x,y-v.y); }    float len() const { return sqrt((x*x)+(y*y)); }    vec normalised() const { float l=len(); return vec(x/l,y/l); }    vec operator*(float s) const { return vec(x*s,y*s); }};Nothing at all complicated, but now we can think in terms of vector operations and forget the maths behind it.vec m; // mousevec u; // unitfloat step=10.0f; // distance to movevec d=m-u; // vector from u to mvec n=d.normalised(); // as above but of length 1vec s=n*step; // as above but of length "step"vec p=u+s; // p equals new position, "step" units closer to mHTH, but I also agree with frob that it is a good idea to learn about this properly. I'd rate learning vector maths as one of the most useful things I ever did in this area.   