# How can I move an object in a direction other than the ordinals?

I want to move a game object from its current position to the mouse position, but my current code only ever moves in one of the eight ordinal directions. In other words, I want it to move along the red path, but I only know how to move it along the white path.

Here's my code:

while(cont){
if(x1 > x){
x++;
}else if(x1 < x){
x--;
}
if(y1 > y){
y++;
}else if(y1 < y){
y--;
}
frame.repaint();
}
// x1 y1 - mouse position
// x, y - sprite position


What am I missing?

Vector2 movementVec = (target-position).normalized();

• Just like you've done in your code, you add the movement vector to the position of the sprite. So, for X for example, instead of ++ you'll use += movementVec.x. – MichaelHouse Sep 14 '14 at 15:27