• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GML Move toward center of an object

Traversal

Member
Hello, I wanted to test how to move around. Traget is to move my player object to
another object into its center.

Code:
if(distance_to_object(obj_field5)>0)
{
    move_towards_point(obj_field5.x, obj_field5.y, 3);
}
It moves to the lower left corner and I am not sure how to calculate the correct place
(center of obj_field5). Obj_field5 has sprite of 64+64. I guess I have somehow to
change my if statement away from >0 to the center (32/32). I just can't figure it out.
 

Traversal

Member
Did solve it (dirty) with settings x and y half the size, after movement, to place my player object:

Code:
   x=obj_field5.x-32;

   y=obj_field5.y-32;
 

Tthecreator

Your Creator!
You can do several things:
-Change the origin of your sprite. Your object has simple X and Y coordinates, even though your sprite isn't a single pixel but a square of pixels. Changing the origin of your sprite changes the place at which the X and Y coordinates on your sprite is mapped. This can be changed on the sprite.
-Add half of your objects size to your functions. You can use sprite_width and sprite_height to get the width and height of obj_field5. Divide it by two to get half the size of your object to put the point in the middle. You'll also need to change distance_to_object() to distance_to_point(x, y).

Either of these solutions should be fine, but you should try the first one first and the second one if you can't get the first one to work or if it gives you other issues.
 

Traversal

Member
Thank you @Tthecreator I did set my sprite to middle/center and only issue now is, that I cant get the
movement until this center. Issue is within distance_to_object >0 for sure, as it can not become negative?


Code:
if(distance_to_object(obj_field5)>0)
{
    move_towards_point(obj_field5.x, obj_field5.y, 3);
}
That is why i do a "jump" to the center by setting x and y to obj_field5.x-32 and obj_field5y-32.
Not a smooth movement :(
 

Tthecreator

Your Creator!
Then try my second idea by changing distance_to_object to distance_to_point. With this, you can manually make changes to your x and y. Also make sure your other object also has it's sprite set to the center.
For example:
Code:
distance_to_object(obj_field5)
is exactly the same as:
Code:
distance_to_object(obj_field5.x,obj_field5.y)
 

Traversal

Member
Last issue with using distance_to_point is a permanent flickering (shaking of the player object)
while it reached the center of obj_field5

Code:
if(distance_to_point(obj_field5.x,obj_field5)>=1)
{
    move_towards_point(obj_field5.x-32, obj_field5.y-32, 3);
}
 

Traversal

Member
Solved.

The flickering was showing up, because the x and y never matched the traget location exactly.
I had to use round(x) and round(y).

Code:
x = round(x);
y = round(y);

if(x!=guix && y!=guiy)
{  
move_towards_point(guix, guiy, 3);
}
 
Top