How to detect if a dummy is inside a cube?

Typically: "How do I... ", "How can I... " questions
Post Reply
zorro
Posts: 27
Joined: 06 Nov 2023, 09:31

How to detect if a dummy is inside a cube?

Post by zorro »

I want to check if a dummy is inside a cube. Initially, I thought of achieving this by checking for collisions between the dummy and the cube. However, I found that regardless of whether there is a collision between the dummy and the cube, the sim.checkCollision() function always returns -1.

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

Re: How to detect if a dummy is inside a cube?

Post by fferri »

Not sure if that's the intended behavior, as dummies have a "Collidable" flag, but that doesn't seem to affect sim.checkCollision which always returns 0.

You have two options, depending on how you want to treat a dummy.

If you want to treat the dummy as a point in space, you can simply check if its position relative to cuboid is inside its bounding box:

Code: Select all

function isDummyInsideCuboid(dummy, cuboid)
    local bb = sim.getShapeBB(cuboid) -- the 3 sizes of cuboid
    local p = sim.getObjectPosition(dummy, cuboid) -- position of dummy in cuboid's reference frame
    return math.abs(p[1]) <= bb[1] / 2 and math.abs(p[2]) <= bb[2] / 2 and math.abs(p[3]) <= bb[3] / 2
end
If you want to treat a dummy as a sphere, simply add a sphere of the desired size as child of it, with pose 0,0,0,0,0,0,1, so it will always have the same dummy's position, then use sim.checkCollision. (or you can use a modified version of above algorithm to take into account sphere size, it would only be slightly faster)

Post Reply