Multiple objects set parent

Typically: "How do I... ", "How can I... " questions
Post Reply
Jonatas Teixeira
Posts: 28
Joined: 13 Oct 2023, 18:04

Multiple objects set parent

Post by Jonatas Teixeira »

Hello,

I'm having trouble on setting an object parent based on sim.checkCollision(table,sim.handle_all) calls.

Consider a table, on which I'm placing two objects.

It only works for the first collision pair, when I try to add a third object in the same tree it's not working because the sim.checkCollision(table,sim.handle_all) keeps retrieving the first object handle. How could I code it in a way it works considering I'm working with random objects, knowing only the first one in advance, which is a work station table?
coppelia
Site Admin
Posts: 10747
Joined: 14 Dec 2012, 00:25

Re: Multiple objects set parent

Post by coppelia »

Hello,

you have at least 3 possibilities:

1) after detecting an object collide with the table, (temporarily?) make it non-collidable in order to be able to detect the next object(s). You'd use sim.getObjectSpecialProperty and sim.setObjectSpecialProperty(objectHandle, p), using sim.objectspecialproperty_collidable:

Code: Select all

    local p = sim.getObjectSpecialProperty(objectHandle)
    sim.setObjectSpecialProperty(objectHandle, p ~ sim.objectspecialproperty_collidable)
2) you can loop through each collidable object in the scene:

Code: Select all

    local allObjects = sim.getObjectsInTree(sim.handle_scene, sim.object_shape_type) 
    for i = 1, #allObjects, 1 do
        local object = allObjects[i]
        if object ~= tableObject then
            if sim.getObjectSpecialProperty(object) & sim.objectspecialproperty_collidable ~= 0 then
                local result = sim.checkCollision(tableObject, object)    
            end
        end
    end
3) even better would be to create a collection that includes the table and all already detected objects on it:

Code: Select all

function sysCall_init()
    sim = require('sim')
    ...
    tableCollection = sim.createCollection()
    sim.addItemToCollection(tableCollection, sim.handle_tree, tableObject)
end

function sysCall_sensing()
    local result, collidingObjects = sim.checkCollision(tableCollection, sim.handle_all)    
    if result > 0 then
        sim.setObjectParent(collidingObjects[2], tableObject)
    end
end
Cheers
Jonatas Teixeira
Posts: 28
Joined: 13 Oct 2023, 18:04

Re: Multiple objects set parent

Post by Jonatas Teixeira »

Hi there, I'm trying to group two objects, which handles are saved in a table, using this third option you talked about but it's not working (nothing happens). Could you help me?

Code: Select all

			collectionHandle = sim.createCollection(0)
                        sim.addItemToCollection(collectionHandle, sim.handle_tree, pc[C-1],1)
                        sim.addItemToCollection(collectionHandle, sim.handle_tree, pc[C],1)
                                            
Jonatas Teixeira
Posts: 28
Joined: 13 Oct 2023, 18:04

Re: Multiple objects set parent

Post by Jonatas Teixeira »

ok, reading the scene examples I just found the command sim.groupShapes() I'll try it out....
Post Reply