# (Finally solved) Compare distance between 2 objects (XandY)

I’m using the condition (COMPARE DISTANCE BETWEEN TWO OBJECTS), the problem is that it only let you write a number (px) for the comparation, and seems that it’s doing the comparation of your number like this… respecting your first object, comparing with the second. if you choose 60px.
Compare the position -60px X +60px X -60px Y +60px Y… Is there any way to compare the position in only X or -X (just horizontal not vertical) between two objects? i’m using the expresion “object.X()+60px” but it seems to be using the X value of the object and then make a +60px, so it turns like 300px or something like this in my case…

Two days working on it and the moment i write here i have an idea that could work… . Let me try it and i tell you

I suppose that if i use the condition: X position of object is > or < or whatever of the object “object.X()+60” i will have what i want, i’m gonna try

Try this:

```X = object1.X() - object2.X() distanceX = sqrt(X * X) distanceX = distanceX - ( (object1.Width()/2) + (object2.Width()/2) ) ```

By doing this, distanceX will return the value of distance only on X axes between the two object. You gonna get 0 when two objects almost colliding with each other and you gonna get negative value when two objects are overlapping each other, otherwise always gonna get a positive value.

To check if distance between the two object is greater then 60px, you can simply check if the variable distanceX > 60

Just to comment some things

I’m pretty sure GD uses the euclidean distance, the classic one: the distance between (x1,y1),(x2,y2) = sqrt((x1-x2)^2 + (y1-y2)^2) ==> Pythagoras. The condition only needs one parameter because it “searchs” objects inside a circle of radius 60px, and center Object.X().

Just use the “Compare two expressions” condition, and check:

``abs(Object1.X()-Object2.X()) < 60             //abs() because a distance must be positive``

To get if the distance in X between Object1 and Object2 is below 60.

I hope so, as “Object.X()” returns the object x position, “Object.X() + 60” should return the object x position plus 60… you can’t restrict axes this way

Thanks a lot for your help!.
Unfortunately i think that i can’t use this options with variables… What you said as what i said of compare X distance of object1 to “object2.X()+whatever” works, but only if i have only 1 object of a kind, but i have some of the same object in diferent positions… I already check it, and it works with only 1 object, but not with more because it takes the x position of all of them so it’s not working because it can’t compare… The only way it works is with the condition “Compare distance between two objects” but not as good as i want…I have to use diferent objects, or the same, yes, but with diferent name (block1, block2, block3…) But i’ll have to reprogram every object

Identify the first object instance (use some conditions that only this first instance fulfills), and save it’s x position in a variable
Then identify the second instance in other event with other conditions, and check the distance between the object and the variable:

[code]Conditions: Variable ID of Object is = 0
Actions: Do = Object.X() to variable “pos_x”

Conditions: Variable “ID” of Object is = 1
Actions: Do = abs(Object.X() - Variable(pos_x)) to variable “distance_x”
Conditions: Variable “distance_x” < 60
Actions: Actions when distance between Objects with ID=0 and ID=1 is below 60[/code]
You can avoid the long second event using this instead:

```Conditions: Variable "ID" of Object is = 1 abs(Object.X() - Variable(pos_x)) < 60 // "Compare two expressions" condition Actions: Actions when distance between Objects with ID=0 and ID=1 is below 60```

Y es, definetly it could work, but i don’t know 2 thinks. How can i make a diference between 2 objects of the same one. The only i can see its the variable instance but seems that i can’t use it for actions nor conditions, the other is the x and y position but that’s useless. Also don’t know how to compare position between 2 objects with id=0 and id=1. Al the dificult it’s because the same object are repeated

You can use For each object event and object variables to run the actions and store values (distance) individually for each object, and you can check the value of the object variable to check the distance individually.

Wow, thank you ddabrahim, you give me a great clue to make it, it’s a lot harder because i need to interactuate with a some objects and sprites at the same time, i will need some time but i try what you tell me and it’s taking every object from one to one, so probably it’ll work, i’m gonna try it. Thanks a lot.

Yeeeeeeess!! With the tool “For Each Object objectx, repeat:” i make it work!!! (it seems ) Finally seems that i can automatice it, so it’s working as i want, i’ll let the info here so if can be useful to anybody in a situation like mine.

The idea was to compare the X position and the Y position of an object that i can move, to other objects, so if it’s in collision with another one, i can’t move it more (logical), try with position between to objects it can’t be, because takes the same Xpx points in X position and Y position, and there is another problem, i have repeated the same object several times in the stage, so the solution was use “For each object objectx, repeat:” and save into variables the X position and the Y position, here’s is a picture of it, because i have to check with some other objects, the list continius, but in the same way .

In my case, i erase the object and change it for another, it’s because i need that the object follows gravity, so i can throw it into a hole (platformCharacter), and then change for one i can walk above it (platform).

Thanks to everybody for his help!!!