9
\$\begingroup\$

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.

Game.cs

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

Sprite.cs

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?

\$\endgroup\$
2
  • 3
    \$\begingroup\$ 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. \$\endgroup\$
    – Notabene
    Jan 26, 2011 at 22:18
  • \$\begingroup\$ Hello, I've looked at the link and it was exactly what I needed. Thank you! \$\endgroup\$
    – Matt
    Jan 26, 2011 at 22:23

2 Answers 2

12
\$\begingroup\$

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;
sprite.Direction.Normalize();
\$\endgroup\$
6
  • \$\begingroup\$ Note also that the if the length of sprite.Direction (before normalization) is less than some threshold, your sprite has arrived. \$\endgroup\$
    – TreDubZedd
    Jan 26, 2011 at 22:22
  • \$\begingroup\$ 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. \$\endgroup\$ Jan 26, 2011 at 22:26
  • \$\begingroup\$ 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. \$\endgroup\$
    – Matt
    Jan 26, 2011 at 22:27
  • \$\begingroup\$ :) 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 ;) ). \$\endgroup\$ Jan 26, 2011 at 22:31
  • \$\begingroup\$ Lets say we are using a different IDE that didn't have a normalize function, what would be the Normalize equation? \$\endgroup\$
    – Spooks
    Jan 26, 2011 at 23:32
2
\$\begingroup\$

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.

\$\endgroup\$
2
  • 3
    \$\begingroup\$ 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. \$\endgroup\$
    – Notabene
    Jan 26, 2011 at 22:12
  • \$\begingroup\$ I should have commented, you are right. \$\endgroup\$ Jan 27, 2011 at 12:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .