Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm currently looking into XNA game development with the C# language.

I have two classes: the main game handler and a "sprite" class. Following is some basic pseudo-code which I hope adequately describes the issue.


class game {
  sprite the_sprite;
  void update(time) {
    var mouse = mouse.state
    if(mouse.clicked) { this.the_sprite.moveTo(mouse.x, mouse.y) }


class sprite {
  vector2 location;
  vector2 move_to;
  void moveTo(x, y) { this.move_to = new vector2(x, y) }
  void update(time) {
    if(this.location.x > this.move_to.x /* (or less than) */) {
      // adjust location.x
    if(this.location.y > this.move_to.y /* (or greater than) */) {
      // adjust location.y

Basically: when the user clicks somewhere in the game window, the x and y coordinates of the mouse are taken, and the game object will move towards that location over a period of time.

Well... the code works, but it's ugly, and objects don't move directly towards the object (instead, it's diagonal movement, followed by single-axis movement). I'm guessing there are some mathematical functions I can use, but I honestly haven't a clue where to get started. Any suggestions?

share|improve this question
Hello guest. What are you looking for is normalization. Please read answers to this question: gamedev.stackexchange.com/questions/7540/… . One offers code which you need, second explains how it works very nicely. –  Notabene Jan 26 '11 at 22:18
Hello, I've looked at the link and it was exactly what I needed. Thank you! –  Matt Jan 26 '11 at 22:23

2 Answers 2

up vote 11 down vote accepted

If I understand your problem properly, you should just have a direction Vector2 representing the direction you want to move in inside your sprite class. Like this:

public Vector2 Direction { get; set; }

This is the normalized vector(which means it has a length of 1) showing where you want to go.

Then, add a Speed float property, which says how fast the sprite should go.

public float Speed { get; set; }

You also need to add a UpdateSprite function, so why not put it inside your Sprite class?

public Update(GameTime gameTime)
   Position += Direction * Speed * gameTime.ElapsedGameTime.TotalSeconds;

This will update the sprite's position to make it move(you multiply by the delta seconds so that the sprite moves properly on slow computers too).

Finally, you just set your direction property like this:

sprite.Direction = location - sprite.Position;
share|improve this answer
Note also that the if the length of sprite.Direction (before normalization) is less than some threshold, your sprite has arrived. –  TreDubZedd Jan 26 '11 at 22:22
Exactly, I was about to edit my post, but I guess I won't. If you don't want your sprite to constantly seek the target, just set the Direction to Vector2.Zero when it is close enough to the target. –  Jesse Emond Jan 26 '11 at 22:26
Very good answer... and you also taught me about the use of gameTime (to move sprites at an appropriate speed on slower computers), so two birds with one stone. I'll flag this answer as accepted. –  Matt Jan 26 '11 at 22:27
:) By the way I personally like to put the normalization inside my sprite class(inside my update function), to prevent having to constantly normalize it outside of the class. You should also make sure that the vector's length is not equal to 0, otherwise you'll get a divide by 0 exception. Just check if Direction.LengthSquared() > 0 before normalizing it to be safe(length squared so that you don't have to calculate that expensive square function ;) ). –  Jesse Emond Jan 26 '11 at 22:31
Lets say we are using a different IDE that didn't have a normalize function, what would be the Normalize equation? –  Spooks Jan 26 '11 at 23:32

Google normalization... i am also a little lost at this, but i am pretty sure this will help solve your problem. Someone else can elaborate or disprove my answer i am sure

Edit: I could answer this better, apologies

I personally always look to this blog post to help with these types of problems: http://blog.wolfire.com/2009/07/linear-algebra-for-game-developers-part-1/

It starts from scratch and is relevant to game development.

share|improve this answer
You are right, it is normalization. But it is always better to try to find good answer in previous questions. And if you are answering it is always better to little explain or share code or whatever you think that can be useful, not just point to the google. –  Notabene Jan 26 '11 at 22:12
Thanks for your response, it was indeed normalisation. –  Matt Jan 26 '11 at 22:27
I should have commented, you are right. –  Douglas Rae Jan 27 '11 at 12:54
+1 because Wolfire is great :) –  Jesse Emond Jan 27 '11 at 13:03

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.