# What is the logic of 'move_towards_point'?

I think I was using the move_towards_point function incorrectly. I discovered that I could not make the object move down.

In the create event, if I include the code move_towards_point(x,y,10);, the object in question moves to the right:

If I include the code move_towards_point(-x,y,10);, the object moves to the left:

I wanted to make the object move up, so I left x and used move_towards_point(x,-y,10);. It moved up as I wanted.

Yesterday, I had the need to make the object move down. That's where my dilemma began. I thought to use move_towards_point(x,+y,10);. It did not work; the object moved to the right, as in the first demonstration. I tried move_towards_point(0,y,0);. The object moved to the left, again, as in the second demonstration.

I began to suspect that I was not using the function correctly. By the name, I assumed it would move an object to a certain specific point. I went back to test move_towards_point(-x,-y,0);, and the object move diagonally:

I found the solution to all cases of movement:

• Down : move_towards_point(obj_Bola.x,obj_Bola.y+1,10);
• Up : move_towards_point(obj_Bola.x,obj_Bola.y-1,10);
• Left : move_towards_point(obj_Bola.x-1,obj_Bola.y,10);
• Right : move_towards_point(obj_Bola.x+1,obj_Bola.y,10);

The explanation seems to be that I am now using the x and y, relative to the object. When I do not want to change it's position on any of it's axis, I simply do not add any value. On the axis that I want the object to move in, I add or subtract a value.

One observation is that the value added or subtracted does not change the speed that the object moves, so move_towards_point(obj_Bola.x,obj_Bola.y+99999,10); moves the object down with a speed of 10.

I would like to know the logic behind move_towards_point, and if the way I solved my problem is the right one.

• +y is the same as y – Bálint Mar 23 '17 at 22:43
• Yes, I know, but at the time I thought it might have some effect – Boneco Sinforoso Mar 23 '17 at 22:46
• you could think of the function having the name move_towards_the_points_passed_in_the_parameters_from_the_current_position_and_at_the_specified_speed do not confuse with move_with_the_speed_vector_given_in_the_paramteres (i.e. setting hspeed and vspeed] - to emphasise, the parameters are the target position, not the direction of movement, nor the speed vector to use. – Theraot Mar 23 '17 at 23:16
• Welcome to GameDev! Note that there were a few redundancies (things you were telling us twice) in your question, so I have removed them for an easier read. I have also formatted the structure to make it easier to read (everything stated is in a "timeline" order of what you did, instead of jumping back and forth, for example). I also removed the part about using a translator. Note that if we have any questions, we will ask via comments. You do not need to ask, as that is what comments are for. – Gnemlock Mar 23 '17 at 23:30
• @Gnemlock Thank you for your reception and editing!!! – Boneco Sinforoso Mar 24 '17 at 0:38

move_towards_point() Makes the character go to a specific position, it's not intended to use it for general movement.

For example, if your character is at (100, 100) and you use the function with (200, 200), then it starts moving diagonally in the right-down direction.

Because the x coordinate 0 is at the left edge and the y coordinate 0 is at the top, if you enter these values, the character starts moving towards those edges.

If you enter the character's positions, it defaults to right.

To make the player go in a specific direction, you need to add some amount to the x and y coordinates. For example, to move down you do

y += 1;


move_towards_point makes the object move towards a particular point - as its name implies.

If the object is at coordinates 200,200 and you do move_towards_point(x, -y, 10) then you are telling it to move to coordinates 200,-200 which is off the top of the screen. (10 is the speed)

If the object is at coordinates 200,200 and you do move_towards_point(-x, -y, 10) then you are telling it to move to coordinates -200,-200 which is off the top-left of the screen.

If the object is at coordinates 200,200 and you do move_towards_point(x, y, 10) then you are telling it to move to coordinates 200,200. But it's already there, so I guess it arbitrarily moves right. (Personally, I'd expect it to not move if you tell it to move to where it already is...)

• This doesnt appear to add anything new ontop of the previous answer – Gnemlock Mar 24 '17 at 7:06
• The function uses vector math under the hood, and because a 0 length vector can't be normalized correctly, it returns a vector pointing to rigjt. – Bálint Mar 24 '17 at 7:52
• @Bálint That is not a good reason for the function to behave the way it does. "Under the hood" the function is perfectly capable of computing the length of the movement vector. – immibis Mar 24 '17 at 10:35
• @immibis A vector with the length 0 would prodice a division error on normalizing. You usually have a fixed position for the object to go to, stuff like this doesn't hapoen – Bálint Mar 24 '17 at 10:46
• @Bálint You don't normalize the vector and then check if it's zero-length. You calculate how far the object should move in one step, then see if the target is closer than that, and if so you move the object directly to the target position. Only if the target is further away than that do you need to calculate a normalized direction vector. – immibis Mar 25 '17 at 7:50