Skip to main content
added 233 characters in body
Source Link

Another option, which is more useful if you need to know the rotation of your object as well, is this:

rotation = atan2(dy, dx);
px += speed * elapsed * cos(rotation);
py += speed * elapsed * sin(rotation);

Where elapsed is the time since your last game loop, dy and dx are the y and x coordinates of the vector from your current position to the target destination, and px and py are your current x and y coordinates.

You should also check if the distance you need to go is less than the total amount you can move so that you don't pass your target. A method to do this is:

if( dx*dx + dy*dy < speed * elapsed * speed * elapsed){
    px += dx;
    py += dy;
}
else { 
    calculate position with the first formula
}

This method saves you from having to normalize your vectors and gives you your rotation relative to the x axis in the range of -180 to +180 degrees, while using arccos will not tell you whether your angle is positive or negative.

Another option, which is more useful if you need to know the rotation of your object as well, is this:

rotation = atan2(dy, dx);
px += speed * elapsed * cos(rotation);
py += speed * elapsed * sin(rotation);

Where elapsed is the time since your last game loop, dy and dx are the y and x coordinates of the vector from your current position to the target destination, and px and py are your current x and y coordinates.

You should also check if the distance you need to go is less than the total amount you can move so that you don't pass your target. A method to do this is:

if( dx*dx + dy*dy < speed * elapsed * speed * elapsed){
    px += dx;
    py += dy;
}
else { 
    calculate position with the first formula
}

Another option, which is more useful if you need to know the rotation of your object as well, is this:

rotation = atan2(dy, dx);
px += speed * elapsed * cos(rotation);
py += speed * elapsed * sin(rotation);

Where elapsed is the time since your last game loop, dy and dx are the y and x coordinates of the vector from your current position to the target destination, and px and py are your current x and y coordinates.

You should also check if the distance you need to go is less than the total amount you can move so that you don't pass your target. A method to do this is:

if( dx*dx + dy*dy < speed * elapsed * speed * elapsed){
    px += dx;
    py += dy;
}
else { 
    calculate position with the first formula
}

This method saves you from having to normalize your vectors and gives you your rotation relative to the x axis in the range of -180 to +180 degrees, while using arccos will not tell you whether your angle is positive or negative.

added 338 characters in body
Source Link

Another option, which is more useful if you need to know the rotation of your object as well, is this:

rotation = atan2(dy, dx);
px += speed * elapsed * cos(rotation);
py += speed * elapsed * sin(rotation);

Where elapsed is the time since your last game loop, dy and dx are the y and x coordinates of the vector from your current position to the target destination, and px and py are your current x and y coordinates.

You should also check if the distance you need to go is less than the total amount you can move so that you don't pass your target. A method to do this is:

if( dx*dx + dy*dy < speed * elapsed * speed * elapsed){
    px += dx;
    py += dy;
}
else { 
    calculate position with the first formula
}

Another option, which is more useful if you need to know the rotation of your object as well, is this:

rotation = atan2(dy, dx);
px += speed * elapsed * cos(rotation);
py += speed * elapsed * sin(rotation);

Where elapsed is the time since your last game loop, dy and dx are the y and x coordinates of the vector from your current position to the target destination, and px and py are your current x and y coordinates.

Another option, which is more useful if you need to know the rotation of your object as well, is this:

rotation = atan2(dy, dx);
px += speed * elapsed * cos(rotation);
py += speed * elapsed * sin(rotation);

Where elapsed is the time since your last game loop, dy and dx are the y and x coordinates of the vector from your current position to the target destination, and px and py are your current x and y coordinates.

You should also check if the distance you need to go is less than the total amount you can move so that you don't pass your target. A method to do this is:

if( dx*dx + dy*dy < speed * elapsed * speed * elapsed){
    px += dx;
    py += dy;
}
else { 
    calculate position with the first formula
}
Source Link

Another option, which is more useful if you need to know the rotation of your object as well, is this:

rotation = atan2(dy, dx);
px += speed * elapsed * cos(rotation);
py += speed * elapsed * sin(rotation);

Where elapsed is the time since your last game loop, dy and dx are the y and x coordinates of the vector from your current position to the target destination, and px and py are your current x and y coordinates.