   Moving towards mouse in a 2D grid
  I'm making a "swarm" type game. Pretty much there are 10 "bugs" on the screen that swarm all around your mouse. Everything works just fine except the function that tells the bugs to go towards the mouse if they get too far away. How can I tell each bug to move towards the mouse? I've tried to use a Rise/Run system, by calculating and reducing it, then just moving by those coordinates. I've tried to figure out how to use Tan to help but I can't mentally figure out how that would apply to the gaming world. If anyone has any information on either Trig in the gaming world or how to move an object towards the mouse (or both) please let me know. Thanks!~Gordo~   
//calculate vector from bug to mouse pointerVector2 mouse_position, bug_position;Vector2 move_dir = mouse_position - bug_position;//move_dir is now a vector that points in the line from the bug to the mouse//now make it the right lengthmove_dir = move_dir.Normalize(); //Makes a vector of length 1 in the same directionmove_dir *= bug_speed;    //gives it a length of bug_speed//now it's easy to move the bug towards the mousebug_position += move_dir;I assume you have a basic understanding of how vectors work. I can neither quickly find nor write a good description at the moment for you if you don't. I'm also assuming that you want the bugs to always move straight at the mouse, instead of some more complicated movement.Have you done basic trigonometry/geometry in school?   ;  Have a look at this site.   ;  On a grid it is pretty easy:- do Unproject to find the point on the grid plane over which the mouse hovers (Unproject method in DirectX / XNA if using those)- move the bugs toward that point;Quote:Original post by theOcelot//calculate vector from bug to mouse pointerVector2 mouse_position, bug_position;Vector2 move_dir = mouse_position - bug_position;//move_dir is now a vector that points in the line from the bug to the mouse//now make it the right lengthmove_dir = move_dir.Normalize(); //Makes a vector of length 1 in the same directionmove_dir *= bug_speed;    //gives it a length of bug_speed//now it's easy to move the bug towards the mousebug_position += move_dir;I assume you have a basic understanding of how vectors work. I can neither quickly find nor write a good description at the moment for you if you don't. I'm also assuming that you want the bugs to always move straight at the mouse, instead of some more complicated movement.Have you done basic trigonometry/geometry in school?Yes, I do understand how vectors work although I need to find the Java equivalent of the code you posted. And yes I did take basic trig/geo in school but I made the mistake of thinking I'll never use it again and didn't pay as much attention as I should have. This was also 4 years ago so it's not as imprinted in my mind as it once was.Quote:On a grid it is pretty easy:- do Unproject to find the point on the grid plane over which the mouse hovers (Unproject method in DirectX / XNA if using those)- move the bugs toward that pointI'm using Java, I guess I should have put that in the OP... But thank you for the feedback.;  Just make Vector2 a class in java, or a struct, with two floats and you can use the code above   ;Quote:Original post by discodowneyJust make Vector2 a class in java, or a struct, with two floats and you can use the code aboveNot if, as I remember, Java doesn't have operator overloading. OP, you'll have to use named member functions for subtraction, scaling, etc.   ;Quote:Original post by discodowneyJust make Vector2 a class in java, or a struct, with two floats and you can use the code aboveI understand all of this, It's the Normalize(); function that I don't know how to replicate...Vector2 mouse_position, bug_position;Vector2 move_dir = mouse_position - bug_position;I also don't get this... You say a vector is a set of 2 points. That's 2 X's and 2 Y's, correct? Why would you have mouse_position as a set of 4 points? Or am I missing the concept of Vector2?[Edited by - Gordo4444 on April 13, 2010 7:14:07 PM];I 2nd red's site.It describes swarm movement, with seek/flee/approach/etc...Looks like his code is in Java as well. See also, the related GDC paperI found a Java vector2d class.java3d/Vector2dI'm not familiar with Java, so I don't know if it's a default library?   ;Quote:Original post by Gordo4444Quote:Original post by discodowneyJust make Vector2 a class in java, or a struct, with two floats and you can use the code aboveI understand all of this, It's the Normalize(); function that I don't know how to replicate...Normalizing a vector makes it's length equal to 1. That is, magnitude( normal(v) ) = 1.  v1*v1 + v2*v2 + ... = v*v = v^2; dot product  magnitude( v ) = sqrt( v^2 ) = M  magnitude( normal(v) ) = 1 = magnitude( v ) / magnitude( v ) = magnitude( v ) / M  magnitude( v ) / M = sqrt( v^2 ) / M = sqrt( v^2 / M^2 )  if sqrt( v^2/M^2 ) = 1    v^2/M^2 = 1    v / M = normal(v) = uSince v^2/M^2 is a scaler value, but v/M is a vector. That's why i did not write sqrt( v^2/M^2 ) = v/M. If you're not familiar with the dot product, i was only using it so i wouldn't have to write out v1*v1+..., so what's important to see is how it works, not the notation.It is often written v / |v|. A normalized vector may be referred to as a unit vector, written u.More information can be found at your local wikipedia. In fact, it's probably more accurate too!EDIT: completely rewrote example[Edited by - Splinter of Chaos on April 13, 2010 8:05:14 PM]