Moving along a vector

I have two arrays containing the x and y positions of two points - the target position and the origin position. To move a point along this vector, would I be correct to assume that the code below would be the correct calculation and method to use? I'm asking because it seems to be creating unexpected problems when I use it in my game.

double target[2] = {10.0, 10.0};
double origin[2] = {0.0, 0.0};
int step = 100;

double delta[2] = {target[0] - origin[0], target[1] - origin[1]};
double dotPos[2] = {origin[0], origin[1]};

while(abs(target[0] - dotPos[0]) > 1 && abs(target[1] - dotPos[1]) > 1)
    dotPos[0] += delta[0] / step;
    dotPos[1] += delta[1] / step;
It looks fine at first glance...what kind of issues is it causing?
Yep, looks fine to me too. One quick question though, the condition in your while loop suggests that you are not making a complete transition from origin to target. Is that what you intended?
Is this probably due to comparing doubles to an integer, namely 1?
I think 1 was intended to be a sort of epsilon to avoid directly comparing the floating point numbers.
and by the way, shouldn't there be fabs() instead?
Well, the problem is that the dotPos coordinates seem to ignore my collision check when the vector line is completely horizontal. I thought that maybe my collision check was wrong, but it works in all other functions exactly the way it is supposed to without fault, so in my mind it can only be a problem with the vector.

Fixed problem by offsetting the origin y coordinate of the vector so that it can never be horizontal. How strange...
Last edited on
Topic archived. No new replies allowed.