Comments on: Pointing and moving towards a target in XNA (2D) http://xnafan.net/2012/12/pointing-and-moving-towards-a-target-in-xna-2d/ XNA tutorials, code and games Fri, 10 Aug 2018 04:55:21 +0000 hourly 1 https://wordpress.org/?v=4.2.22 By: James http://xnafan.net/2012/12/pointing-and-moving-towards-a-target-in-xna-2d/comment-page-1/#comment-179943 Tue, 02 Sep 2014 12:21:44 +0000 http://xnafan.net/?p=693#comment-179943 Yes, this is exactly the approach I went for! I get the length of the distance vector and check if the length is less than the specified speed. If the length is less than speed I return the destination vector. Thanks Jakob!

Here's the method I'm using in case someone else happens to run into this problem (note there are issues with this method if you overshoot the destination)

public static Vector2 Move(this Vector2 position, Vector2 destination, float speed)
{
var dist = destination - position;

if (dist.LengthSquared() < speed)
{
return destination;
}

dist.Normalize();

return position + dist * speed;
}

]]>
By: admin http://xnafan.net/2012/12/pointing-and-moving-towards-a-target-in-xna-2d/comment-page-1/#comment-176871 Sat, 30 Aug 2014 12:25:37 +0000 http://xnafan.net/?p=693#comment-176871 Hi James :)

What I usually do is to check the distance to the target for every Update.
If the distance is smaller than a threshold I've selected (say 2 pixels or "units" if you will), I move the moving object on top of the target and stop moving.

Would that work for you?

Kind regards - Jakob

]]>
By: James http://xnafan.net/2012/12/pointing-and-moving-towards-a-target-in-xna-2d/comment-page-1/#comment-170327 Thu, 21 Aug 2014 16:46:17 +0000 http://xnafan.net/?p=693#comment-170327 Great read! One question, say if I want to gradually move an object from one position to another and then stop when the object reached it's destination. since we are multiplying the vector with a float we will get really, really close but never just on the exact destination. I'm moving to [64,30], I'll get to about [63.999992, 29.999991] What's a standard way to solve this problem? Rounding/Clamping???

]]>
By: Christoph Wagner http://xnafan.net/2012/12/pointing-and-moving-towards-a-target-in-xna-2d/comment-page-1/#comment-116881 Wed, 23 Apr 2014 18:11:47 +0000 http://xnafan.net/?p=693#comment-116881 Thanks, very nice explained, even the vector subtraction. Thanks again!!

]]>