I am using SFML. I have a rock object. It moves from its starting position, always the same to the players position which may be different.
I have taken the difference between the 2 points, used the distance formula and normalized the value and move the object using moveX and moveY.
The object however stops moving once it reaches the players position.
How do I make it continue to move in that same straight line, past the player's position?
Looking at your code I would change all the floats to doubles. Doubles have better precision and are the more preferred type these days.
Lines 1 and 2 define "rockX" and "rockY", but I do not see where they are used.
Lines 4 and 5 what happens when "playerPosX/Y" is equal to "rockX/Y"? It seems to me that "difX/Y" would be zero which does not do much for the following calculations. And I am think that line 13 should stop the program with a divide by zero error. If be chance you do reach line 16 the parameters could be zero, so no move would happen.
I am thinking that prior to line 4 you should check the rock position to a boundary to see if there is any room to move first. Then lines 4 and 5 would would work better by subtracting "rockPosX/Y" from the boundary or maybe the difference of the boundary - playerPos.
Not having anything more to work with I have no way to see what some of the variable values are.
playerPos and rockPos are always different. They can never equal each other in my program anyways.
The output I get is -0.41for moveX and is a constant until it reaches the player position.
Then the value alternates between +0.41 and -0.41, thereby remaining in the same place.
Not sure why that is happening.
I would have assumed it should continue to call move with -0.41 and proceed in the same line.
That's just the x value, the y value does the same thing.
if you are just moving at a fixed rate in 2-d, just add to the current position each time.
for example
void rock::move()
{
currentx += change_in_x_per_sec * change_in_time;
currenty += change_in_y_per_sec * change_in_time; //time is a snapshot, not current exact or it will drift
}
if the rate is not a constant, you will have to do more work, of course.
you can update the change_in class members once to change direction or speed for a while, as needed.
you may not even need the change in time (cpu clock time or 'real' time) if the loop is executing at a nice fixed rate on all machines. But if it executes faster on some machines than others or varies depending on other work happening, etc, you should probably use it as a control.
what this is doing is updating the position based off the slope of the line of its travel by tying the x axis (arbitrary choice) to wall clock time. So change is X is a function of time, and change in y is a function of x. It all unwraps to this simple form, thinking of it that way.