Build a box

Typically: "How do I... ", "How can I... " questions
Post Reply
siat_znb
Posts: 7
Joined: 13 Mar 2020, 09:53

Build a box

Post by siat_znb » 16 Mar 2020, 10:04

I want to build a box in v-rep. But when I use 'Convex hull of selection' this operator, the hull contains all the boxes. Then when I put a banana model in it with run 'start simulation', the banana will be popped out. I need a box, it can check the collision and put some things(such as banana, ape or other 3d models) in it. So who can tell me where get the open box into the v-rep?

siat_znb
Posts: 7
Joined: 13 Mar 2020, 09:53

Re: Build a box

Post by siat_znb » 16 Mar 2020, 10:08

just like that Image

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

Re: Build a box

Post by coppelia » 17 Mar 2020, 17:04

Hello,

you can create such an open box by creating cuboids (sim.createPureShape(0,...)), then grouping them (sim.groupShapes(...)). Or you can use following function:

Code: Select all

function createOpenBox(size,baseThickness,wallThickness,density,inertiaCorrectionFact,static,respondable,color)
    local parts={}
    local dim={size[1],size[2],baseThickness}
    parts[1]=sim.createPureShape(0,16,dim,density*dim[1]*dim[2]*dim[3])
    sim.setObjectPosition(parts[1],-1,{0,0,baseThickness*0.5})
    dim={wallThickness,size[2],size[3]-baseThickness}
    parts[2]=sim.createPureShape(0,16,dim,density*dim[1]*dim[2]*dim[3])
    sim.setObjectPosition(parts[2],-1,{(size[1]-wallThickness)*0.5,0,baseThickness+dim[3]*0.5})
    parts[3]=sim.createPureShape(0,16,dim,density*dim[1]*dim[2]*dim[3])
    sim.setObjectPosition(parts[3],-1,{(-size[1]+wallThickness)*0.5,0,baseThickness+dim[3]*0.5})
    dim={size[1]-2*wallThickness,wallThickness,size[3]-baseThickness}
    parts[4]=sim.createPureShape(0,16,dim,density*dim[1]*dim[2]*dim[3])
    sim.setObjectPosition(parts[4],-1,{0,(size[2]-wallThickness)*0.5,baseThickness+dim[3]*0.5})
    parts[5]=sim.createPureShape(0,16,dim,density*dim[1]*dim[2]*dim[3])
    sim.setObjectPosition(parts[5],-1,{0,(-size[2]+wallThickness)*0.5,baseThickness+dim[3]*0.5})
    for i=1,#parts,1 do
        sim.setShapeColor(parts[i],'',sim.colorcomponent_ambient_diffuse,color)
    end
    local shape=sim.groupShapes(parts)
    if math.abs(1-inertiaCorrectionFact)>0.001 then
        local transf=sim.getObjectMatrix(shape,-1)
        local m0,i0,com0=sim.getShapeMassAndInertia(shape,transf)
        for i=1,#i0,1 do
            i0[i]=i0[1]*inertiaCorrectionFact
        end
        sim.setShapeMassAndInertia(shape,m0,i0,com0,transf)
    end
    if static then
        sim.setObjectInt32Parameter(shape,sim.shapeintparam_static,1)
    else
        sim.setObjectInt32Parameter(shape,sim.shapeintparam_static,0)
    end
    if respondable then
        sim.setObjectInt32Parameter(shape,sim.shapeintparam_respondable,1)
    else
        sim.setObjectInt32Parameter(shape,sim.shapeintparam_respondable,0)
    end
    sim.reorientShapeBoundingBox(shape,-1)
    return shape
end
e.g.:

Code: Select all

createOpenBox({0.3,0.5,0.05},0.002,0.002,500,1,true,true,{1,0,1}) 
Cheers

siat_znb
Posts: 7
Joined: 13 Mar 2020, 09:53

Re: Build a box

Post by siat_znb » 18 Mar 2020, 04:07

Hi,

Firstly, thanks for your careful explanation! And your code is really effective.
But there is still some confusion about the thing that the vision_sensor can not see this box from code built.


zhangnengbo

siat_znb
Posts: 7
Joined: 13 Mar 2020, 09:53

Re: Build a box

Post by siat_znb » 18 Mar 2020, 04:52

hi,

thanks to this forum!
I add the renderable property.
......
local p=simGetObjectSpecialProperty(handle)
p=simBoolOr16(p,sim_objectspecialproperty_renderable)
simSetObjectSpecialProperty(handle,p)
......
That is work, thanks to this website! how to donate this website! it is a really beautiful workshop.
And I have another question about how to export the dynamic model from Lua code to convert the file .ttm?

city: shenzhen
name: zhangnengbo

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

Re: Build a box

Post by fferri » 18 Mar 2020, 17:09

Hi,

you can use function sim.saveModel to programmatically export a model.

Cheers

siat_znb
Posts: 7
Joined: 13 Mar 2020, 09:53

Re: Build a box

Post by siat_znb » 19 Mar 2020, 07:25

hi,

Code: Select all

Lua runtime error: [string "MAIN SCRIPT"]:26: Object is not tagged as model base. (sim.saveModel)
stack traceback:
    [C]: in function 'saveModel'
    [string "MAIN SCRIPT"]:26: in function  
I don‘t know how to use it.

My code follows here.

Code: Select all

function sysCall_init()  
    sim.handleSimulationStart()
    sim.openModule(sim.handle_all)
    sim.handleGraph(sim.handle_all_except_explicit,0)
    print(0)
    -- customized code
    handle = createOpenBox({0.3,0.5,0.3},0.002,0.002,500,1,true,true,{1,1,1}) 
    print(handle)
    print(10)
    local p = sim.getObjectSpecialProperty(handle)
    p = sim.boolOr32(p,sim.objectspecialproperty_renderable)
    sim.setObjectSpecialProperty(handle,p)
    
    han = sim.getObjectHandle("Cuboid3")
    result = sim.saveModel(han, "znb_box.ttm")
end
......
......
......
function createOpenBox(size,baseThickness,wallThickness,density,inertiaCorrectionFact,static,respondable,color)
    local parts={}
    local dim = {size[1],size[2],baseThickness}
    parts[1] = sim.createPureShape(0,16,dim,density*dim[1]*dim[2]*dim[3])
    sim.setObjectPosition(parts[1],-1,{0,0,baseThickness*0.5})
    dim={wallThickness,size[2],size[3]-baseThickness}
    parts[2] = sim.createPureShape(0,16,dim,density*dim[1]*dim[2]*dim[3])
    sim.setObjectPosition(parts[2],-1,{(size[1]-wallThickness)*0.5,0,baseThickness+dim[3]*0.5})
    parts[3] = sim.createPureShape(0, 16, dim, density*dim[1]*dim[2]*dim[3])
    sim.setObjectPosition(parts[3],-1,{(-size[1]+wallThickness)*0.5,0,baseThickness+dim[3]*0.5})
    dim={size[1] - 2*wallThickness,wallThickness,size[3] - baseThickness}
    parts[4] = sim.createPureShape(0, 16, dim, density*dim[1]*dim[2]*dim[3])
    sim.setObjectPosition(parts[4],-1,{0,(size[2]-wallThickness)*0.5,baseThickness+dim[3]*0.5})
    parts[5] = sim.createPureShape(0,16,dim,density*dim[1]*dim[2]*dim[3])
    sim.setObjectPosition(parts[5],-1,{0,(-size[2]+wallThickness)*0.5,baseThickness+dim[3]*0.5})
    print(#parts)
    for i=1,#parts,1 do
        sim.setShapeColor(parts[i],'',sim.colorcomponent_ambient_diffuse,color)
    end
    local shape = sim.groupShapes(parts)
    if math.abs(1-inertiaCorrectionFact) > 0.001 then
        local transf = sim.getObjectMatrix(shape,-1)
        local m0,i0,com0 = sim.getShapeMassAndInertia(shape,transf)
        for i=1,#i0,1 do
            i0[i] = i0[1] * inertiaCorrectionFact
        end
        sim.setShapeMassAndInertia(shape,m0,i0,com0,transf)
    end
    if static then
        sim.setObjectInt32Parameter(shape,sim.shapeintparam_static,1)
    else
        sim.setObjectInt32Parameter(shape,sim.shapeintparam_static,0)
    end
    if respondable then
        sim.setObjectInt32Parameter(shape,sim.shapeintparam_respondable,1)
    else
        sim.setObjectInt32Parameter(shape,sim.shapeintparam_respondable,0)
    end
    sim.reorientShapeBoundingBox(shape,-1)
    return shape
end
Cheers

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

Re: Build a box

Post by fferri » 19 Mar 2020, 08:19

As the error message (and the API documentation) says, the object must be marked as "Object is model base".

siat_znb
Posts: 7
Joined: 13 Mar 2020, 09:53

Re: Build a box

Post by siat_znb » 22 Mar 2020, 07:42

My question is that if I build an object from this code “sim.createPureShape”.
how can I convert it to the type of "baseOfModel".

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

Re: Build a box

Post by coppelia » 26 Mar 2020, 08:57

yes, there is an API function for that:

sim.setModelProperty. Following of the possible flags is relevant: sim.modelproperty_not_model

Cheers

Post Reply