Dynamic color change in distance calculation

Typically: "How do I... ", "How can I... " questions
Leizah
Posts: 61
Joined: 20 Jun 2021, 16:47

Dynamic color change in distance calculation

Post by Leizah »

Hi,

I've a scene with two objects, a table (Object_1) and a walking Bill (Object_2).
I would like to display the minimum distance between these object with a red line when d < 0.5m and a green one when d > 0.5m.
Using this tutorial as reference, I've made the following child script for the table (That's the fixed object into the scene):

Code: Select all

function sysCall_init()
    local Object_1_Handle=sim.getObjectHandle('Object_1')
    Object_1_Collection=sim.createCollection(0)
    sim.addItemToCollection(Object_1_Collection,sim.handle_tree,Object_1_Handle,0)
    distanceSegment_true=sim.addDrawingObject(sim.drawing_lines,4,0,-1,1,{0,1,0})
    distanceSegment_false=sim.addDrawingObject(sim.drawing_lines,4,0,-1,1,{1,0,0})
end

function sysCall_sensing()
    
    local result,distData,objectPair=sim.checkDistance(Object_1_Collection,sim.handle_all)
    if  result <0.5 then
        sim.addDrawingObjectItem(distanceSegment_false,nil)
        sim.addDrawingObjectItem(distanceSegment_false,distData)
        local txt='Object_1 clearance is '..distData[7]
        txt=txt..'m, minimum distance object pair is '..getAsString(objectPair)
        print(txt)
    else
        sim.addDrawingObjectItem(distanceSegment_true,nil)
        sim.addDrawingObjectItem(distanceSegment_true,distData)
        local txt='Object_1 clearance is '..distData[7]
        txt=txt..'m, minimum distance object pair is '..getAsString(objectPair)
        print(txt)
    end
end
Unfortunately, the segment doesn't change color when the distance crosses the d = 0.5m value, so it stays red/green if the first calculated value was d < 0.5m / d > 0.5m

There is a way to dinamically change the line color according to the calculated distance between the objects?

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

Re: Dynamic color change in distance calculation

Post by fferri »

It should be if distData[7]<0.5 then ... not if result<0.5 then ....

result is 0 or 1 (1 if distance is smaller than threshold).

distData is a table, indices 1 to 6 being the distance segment, and index 7 is the distance between the entities.

Leizah
Posts: 61
Joined: 20 Jun 2021, 16:47

Re: Dynamic color change in distance calculation

Post by Leizah »

It worked, thank you!

I would like to extend my scene with another table, and I would like to mantain the distance line between Bill and each teable, adding a green/red panel (like a door) between the two table according to their relative distance, is that achievable using a similiar script?

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

Re: Dynamic color change in distance calculation

Post by fferri »

Yes

You have various item types, e.g. pixel-sized points, pixel-sized lines, triangles, "triangle points", "rectangle points", "disc points", "cube points", "sphere points".

Additionally, you can dynamically create and destroy primitive shapes and meshes.
See also:

Leizah
Posts: 61
Joined: 20 Jun 2021, 16:47

Re: Dynamic color change in distance calculation

Post by Leizah »

Thanks,

I knew about that possible solution, but I would like to generate the rectangular mesh fixed between two specific object (If a walking element i.e. Bill passes between these objects, the mesh has not to desappear) and to maintain the line for all the other possible combination.

There's a way to get that kind of result?

Leizah
Posts: 61
Joined: 20 Jun 2021, 16:47

Re: Dynamic color change in distance calculation

Post by Leizah »

fferri wrote: 17 Jul 2021, 17:28 Yes

You have various item types, e.g. pixel-sized points, pixel-sized lines, triangles, "triangle points", "rectangle points", "disc points", "cube points", "sphere points".

Additionally, you can dynamically create and destroy primitive shapes and meshes.
See also:
Hi, I've tried to use the "rectangle points" item, but it returns me a very big area that doesn't link the two minimum distance points (as the line does), but that's set in a random place in the space between the two object.
That's the code I used:

Code: Select all

function sysCall_init()
    local Stand_Enter_Handle=sim.getObjectHandle('Stand_Enter')
    Stand_TP_Handle=sim.getObjectHandle('Teach_Pendant_Stand')
    Stand_Exit_Handle=sim.getObjectHandle('Stand_Exit')
    EnterCollection=sim.createCollection(0)
    sim.addItemToCollection(EnterCollection,sim.handle_tree,Stand_Enter_Handle,0)
    TPCollection=sim.createCollection(0)
    sim.addItemToCollection(TPCollection,sim.handle_tree,Stand_TP_Handle,0)
    ExitCollection=sim.createCollection(0)
    sim.addItemToCollection(ExitCollection,sim.handle_tree,Stand_Exit_Handle,0)

    distanceSegment_true=sim.addDrawingObject(sim.drawing_lines,4,0,-1,1,{0,1,0})
    distanceSegment_false=sim.addDrawingObject(sim.drawing_lines,4,0,-1,1,{1,0,0})
    SafeExit_true=sim.addDrawingObject(sim.drawing_cubepoints,0.1,0,-1,1,{0,1,0})
    SafeExit_false=sim.addDrawingObject(sim.drawing_cubepoints,0.1,0,-1,1,{1,0,0,})
end

function sysCall_sensing()
    
    local result,distData,objectPair=sim.checkDistance(EnterCollection,sim.handle_all)
    local result_1,distData_1,objectPair_1=sim.checkDistance(EnterCollection,Stand_TP_Handle)
    local result_2,distData_2,objectPair_2=sim.checkDistance(EnterCollection,ExitCollection)
    local result_3,distData_3,objectPair_3=sim.checkDistance(TPCollection,ExitCollection)
    
    if  distData[7] <0.5 and distData_1[7]>0.5 and distData_2[7]>0.5 and distData_3[7]>0.5 then
    
        sim.addDrawingObjectItem(distanceSegment_false,nil)
        sim.addDrawingObjectItem(distanceSegment_false,distData)
        sim.addDrawingObjectItem(SafeExit_true,nil)
        sim.addDrawingObjectItem(SafeExit_true,distData_1)
        sim.addDrawingObjectItem(SafeExit_true,nil)
        sim.addDrawingObjectItem(SafeExit_true,distData_2)
        sim.addDrawingObjectItem(SafeExit_true,nil)
        sim.addDrawingObjectItem(SafeExit_true,distData_3)
        local txt='Enter clearance is '..distData[7]
        txt=txt..'m, minimum distance object pair is '..getAsString(objectPair)
        print(txt)
        
        else
        
        sim.addDrawingObjectItem(distanceSegment_true,nil)
        sim.addDrawingObjectItem(distanceSegment_true,distData)
        sim.addDrawingObjectItem(SafeExit_false,nil)
        sim.addDrawingObjectItem(SafeExit_false,distData_1)
        sim.addDrawingObjectItem(SafeExit_false,nil)
        sim.addDrawingObjectItem(SafeExit_false,distData_2)
        sim.addDrawingObjectItem(SafeExit_false,nil)
        sim.addDrawingObjectItem(SafeExit_false,distData_3)
        local txt='Enter clearance is '..distData[7]
        txt=txt..'m, minimum distance object pair is '..getAsString(objectPair)
        print(txt)
    end

end


This is what I get using the sim.drawing_quadpoints type:
Image

This is what I get using the cubepoints type:
Image

I would like to get a rectangle with the same dimension of the green line (that's the distance between the objects) and an height h= 1 m that is green/red according to the minimum distance value, but I was not able to figure out what I've to change my code in order to get what I want.

In addition, I set the code for generate three different cubes, but it only returns one (which has, however, the wrong color, because the distance shown into the picture is d >0.5 m, so it should be green, but, as you can see, it's red)

There's any tutorial to understand properly how the command works when a different item type is selected?

coppelia
Site Admin
Posts: 10339
Joined: 14 Dec 2012, 00:25

Re: Dynamic color change in distance calculation

Post by coppelia »

You have several possibilities to create a rectangle or cuboid:
Cheers

Leizah
Posts: 61
Joined: 20 Jun 2021, 16:47

Re: Dynamic color change in distance calculation

Post by Leizah »

coppelia wrote: 22 Jul 2021, 08:18 You have several possibilities to create a rectangle or cuboid:
Cheers
Hi, thanks or you reply.

Unfortunately, I still don't get how to get it work.
In both solution, I don't know how to set the mesh into the space between the objects.

Using sim.drawing_triangles I've the following description in the regular API page:
items are triangles (9 values per item (x0;y0;z0;x1;y1;z1;x2;y2;z2) + auxiliary values)
How I've to set these values and how I could link that to the distance that I got from the checkDistance command?

Using sim.createPureShape I got a shape at the origin of the reference system, and don't know how to place this shape along the distance line that I've obtained with the sim.drawing_lines command.

Thanks in advance for your help
Cheers

coppelia
Site Admin
Posts: 10339
Joined: 14 Dec 2012, 00:25

Re: Dynamic color change in distance calculation

Post by coppelia »

Well,

you'll have to do some trigonometry and coordinate transformations! You'll have to start with your two points, and figure out where your other 6 points should be, in order to obtain a cuboid.

Cheers

Leizah
Posts: 61
Joined: 20 Jun 2021, 16:47

Re: Dynamic color change in distance calculation

Post by Leizah »

coppelia wrote: 22 Jul 2021, 09:43 Well,

you'll have to do some trigonometry and coordinate transformations! You'll have to start with your two points, and figure out where your other 6 points should be, in order to obtain a cuboid.

Cheers
Thank you!

I managed to get the desired shape, but now I would like to set it's trasparency to a 5% value, so i added the sim.drawing_itemtransparency string to the command, but I don't know where I've to write the transparency value.

Post Reply