This question already has an answer here:

I want to move a game object from its current position toward the mouse position.

I wrote this Vector2D class to help me:

public class Vector2D {

        private double xVect;
        private double yVect;

        public Vector2D() {
                xVect = 0;
                yVect = 0;
        public Vector2D(double x1, double y1) {
                xVect = x1;
                yVect = y1;
        public double getX(){
            return xVect;
        public double getY(){
            return yVect;

        public double length() {
                return Math.sqrt((xVect*xVect) + (yVect * yVect));
        public void normalize() {
                double length = length();
                if(length > 0){
                    xVect = (xVect/length);
                    yVect = (yVect/length);
        public Vector2D normalize2() {
                double length = length();
                return new Vector2D((xVect/length), (xVect/length));

I then try to move it toward the target position.

public void move(){
// saveX , saveY - mouse x, y coordinates
// x, y - game object coordinates
    Vector2D moveVec = new Vector2D(saveX, saveY);
        if(saveX > x){
            x += ((moveVec.getX()));
        }if(saveX < x){
            x -= ((moveVec.getX()));
        if(saveY > y){
            y += ((moveVec.getY()));
        }if(saveY < y){
            y -= ((moveVec.getY()));
        sleep(); // Thread.sleep(200);
        cont = moveEnemy(moveVec);

But something strange happens: When x = 0 and y = 0, everything is OK, but when for example x = 200 y = 0, my object moves along the red line instead of directly toward its target.

object movement path

Why is this? What's wrong with my code?


public boolean moveEnemy(Vector2D v){
    if((((x - sizeX) <= currentX && currentX <= (x + sizeY) && (y - sizeY) <= currentY && currentY <= (y + sizeY)))){
        return false;
    }else if((((x - sizeX) <= saveX && saveX <= (x + sizeY) && (y - sizeY) <= saveY && saveY <= (y + sizeY))) && (saveX != currentX || saveY != currentY)){
        return false;
    return true;

marked as duplicate by Anko, Kromster says support Monica, bummzack, congusbongus, MichaelHouse Sep 23 '14 at 21:56

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


Look at what's happening.

SaveX is 300, SaveY is 0.

When you normalize moveVec you take the Vector (300,0) and resize it to be a length of 1, which makes it (1,0).

You test to see if 300 > your position, if so you add moveVec.getX(), otherwise you subtract. So you move 1 to the right. You then test to see if 0 > your position, if so you add moveVec.getY(). The Y value of this, in this case is 0, so it shouldn't adjust the position of the object at all.

So after that, you call moveEnemy, which you don't describe in this post, and we know from your picture that it does move down the screen, so something else is moving it.

Now, consider what's happening when you use the vector (0,0). Regardless of whether the tests pass or fail, there is no movement, because moveVec.getX() and moveVec.getY() will be 0. So your move function should not cause any movement at all.

But it does move. It gets to your point correctly. But again, moveEnemy gets called.

My guess is that whatever is in moveEnemy might be doing its job correctly, but in move you're also moving the object, but incorrectly. When you use 0,0 it moves correctly because that causes the move function to not move the object at all.


Not the answer you're looking for? Browse other questions tagged or ask your own question.