Jump to content
  • Advertisement
Sign in to follow this  
Gordo4444

Moving towards mouse in a 2D grid

This topic is 3167 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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~

Share this post


Link to post
Share on other sites
Advertisement

//calculate vector from bug to mouse pointer
Vector2 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 length
move_dir = move_dir.Normalize(); //Makes a vector of length 1 in the same direction
move_dir *= bug_speed; //gives it a length of bug_speed

//now it's easy to move the bug towards the mouse
bug_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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Quote:
Original post by theOcelot

//calculate vector from bug to mouse pointer
Vector2 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 length
move_dir = move_dir.Normalize(); //Makes a vector of length 1 in the same direction
move_dir *= bug_speed; //gives it a length of bug_speed

//now it's easy to move the bug towards the mouse
bug_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 point


I'm using Java, I guess I should have put that in the OP... But thank you for the feedback.

Share this post


Link to post
Share on other sites
Quote:
Original post by discodowney
Just make Vector2 a class in java, or a struct, with two floats and you can use the code above


Not if, as I remember, Java doesn't have operator overloading. OP, you'll have to use named member functions for subtraction, scaling, etc.

Share this post


Link to post
Share on other sites
Quote:
Original post by discodowney
Just make Vector2 a class in java, or a struct, with two floats and you can use the code above


I 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]

Share this post


Link to post
Share on other sites
Quote:
Original post by Gordo4444
Quote:
Original post by discodowney
Just make Vector2 a class in java, or a struct, with two floats and you can use the code above


I 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) = u

Since 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]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!