## sim.checkProximitySensor always returns result = 1

Typically: "How do I... ", "How can I... " questions
SRL_Max
Posts: 8
Joined: 13 Jun 2018, 09:02

### sim.checkProximitySensor always returns result = 1

Hello,

I'm trying to work with proximity sensors and I encountered a problem with the sim.checkProximitySensor function : it seems that it always detects the object I'm passing as argument.

My scene is very simple : a ray type proximity sensor is attached to a robot leg. A static, respondable shape is approached to the sensor until contact (I do this manually).

Here is my code:

Code: Select all

-- attached to each leg tip

function sysCall_init()

prox_FL = sim.getObjectHandle("prox_FL") -- proximity sensor on leg tip
-- sensor range is 0.002m in front of leg tip
dummy_tip_FL = sim.getObjectHandle("dummy_tip_FL")
tar = sim.getObjectHandle("tar") -- target
cub = sim.getObjectHandle("Cuboid") -- static cube shape

end

function sysCall_sensing()

res_FL, dist_FL, det_pt_FL = sim.checkProximitySensor(prox_FL, cub)

if res_FL then -- we detect the cube in front of the foot
print(dist_FL)
end

end

No matter where I put the cube, res_FL is always 1.
The strange thing is that dist_FL is "nil" when the cube is not in the sensor range, which contradicts the fact that res_FL is 1 (ref. to the function documentation).

Explicit handling or not doesn't change the output.

I think I don't understand how the sim.checkProximitySensor is supposed to work. Can you help me with that?

Best,
Maxens

coppelia
Posts: 7396
Joined: 14 Dec 2012, 00:25

### Re: sim.checkProximitySensor always returns result = 1

Hello Maxens,

in Lua, 0 or 1 (or for that matter any number, string, function, etc.) evaluates to true. That is the problem in your code. Only nil or false evaluates to false. So you should write instead:

Code: Select all

if res_FL>0 then
-- we detected the cube
end
Cheers

SRL_Max
Posts: 8
Joined: 13 Jun 2018, 09:02