## How to do distance measurement between two dynamic objects

Typically: "How do I... ", "How can I... " questions
zdunker
Posts: 2
Joined: 02 Dec 2019, 05:43

### How to do distance measurement between two dynamic objects

Hello,

I'm trying to measure distance between two dynamic objects. Attaching a screenshot showcasing what I'm doing.

The robot is moving along the teal line, and cuboid on the bottom is moving along the blue line.

I couldn't find an API like simGetDistance(robot_handle, cuboid_handle) to return dynamic distance between those two objects, so I have to try something else. Below is what I've tried so far.

1. I tried simGetObjectPosition of those two object respectively, then I apply Euclidean distance to calculate their distance, However, here comes the problematic part to me. Because of the objects are moving, I have to calculate the distance with time goes forward. What I come up with is to use simWait() with a high frequency. But no matter how frequent I adjust the parameter in simWait(), the returned distance is always ~0.37 (which I expect to be more diverse in [0.2, 1.0] ). Attaching my code below:

Code: Select all

robot_position = simGetObjectPosition(robot_handle, -1)
cuboid_position = simGetObjectPosition(cuboid_handle, -1)
r_dist = math.sqrt(math.pow((robot_position[1]-cuboid_position[1]),2) + math.pow((robot_position[2]-cuboid_position[2]),2))
while furthest_dis < r_dist do
furthest_dis = r_dist
simWait(0.001, true)
r_dist = math.sqrt(math.pow((robot_position[1]-cuboid_position[1]),2) + math.pow((robot_position[2]-cuboid_position[2]),2))
end

2. I tried adding a graph in the scene which monitoring the distance of the two objects. The distance graph looks just right, but how can I get the data that graph gets?

Since the graph could get the right data, I believe there's some way the program could measure the distance in a timely fashion, it's just me didn't discover it yet. Could you please help?

zdunker
Posts: 2
Joined: 02 Dec 2019, 05:43

### Re: How to do distance measurement between two dynamic objects

After taking a closer look to sim.readDistance, I found out it actually return 2 values, where the second return value is what I'm looking for. I suggest the admin to modify the api document for this method a little, because the 'C return value' is confusing, move smallestDistance into return values.

fferri
Posts: 656
Joined: 09 Sep 2013, 19:28

### Re: How to do distance measurement between two dynamic objects

Hi,

the documentation of sim.readDistance looks about right to me: smallestDistance is already in the Lua return values section. C return values applies only to C/C++ API, not to Lua scripts.

coppelia
You can also use sim.checkDistance. In that case, you do not need to register a distance object previously. Also, for the robot, you could define a collection, then measure the distance between that collection and the cuboid.