Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

How exactly do you use groups/masks in a craft project? (code updated)

RdoRdo
edited October 2017 in Codea Craft Posts: 23

Below is my code for testing the craft physics

-- Craft Template
displayMode(OVERLAY)
function setup()
    -- Create a new craft scene
    scene = craft.scene()
scene.camera.position = vec3(HEIGHT/2,HEIGHT/2,HEIGHT/2)
floor = scene:entity()
    floor.position = vec3(HEIGHT/2,-1,HEIGHT/2)
  floor.model = craft.model.plane(vec2(1024,1024))
mate = craft.material("Materials:Basic")

    mate.diffuse = color(255, 255, 255, 255)
    floor.material = mate

    hardfloor = floor:add(craft.rigidbody,STATIC)

    hardfloor.sleepingAllowed = false
 hardfloor.awake = true
    hardfloor.group = 0
    hardfloor.mask = 1

    verticalx,horizontaly = 0,0

    -- Create a new entity

 bx = {}
pog = {}
end

function update(dt)
    -- Update the scene (physics, transforms etc)



    scene.camera.eulerAngles = vec3(verticalx,horizontaly,0)
    scene:update(dt)
end

-- Called automatically by codea 
function draw()
    update(DeltaTime)

    -- Draw the scene
    scene:draw()    
end 
function touched(touch)
    if CurrentTouch.state == MOVING then
    verticalx,horizontaly = verticalx + CurrentTouch.deltaY,horizontaly + CurrentTouch.deltaX
    end
    if CurrentTouch.tapCount == 2 then
        bx[#bx + 1] = scene:entity()
        bx[#bx].position = vec3(CurrentTouch.prevX,HEIGHT/2,CurrentTouch.prevY)
        surface = craft.material("Materials:Basic")
        surface.diffuse = color(math.random(0,255),math.random(0,255),math.random(0,255))
        bx[#bx].model = craft.model.cube(vec3(13,13,13))
        bx[#bx].material = surface
                        bx[#bx]:add(craft.rigidbody,DYNAMIC,13)
bx[#bx]:add(craft.shape.box,vec3(13,13,13))--,vec3(6.5,6.5,6.5))        

    pog[#pog + 1] = bx[#bx]:get(craft.rigidbody)
pog[#pog].group = 1        
pog[#pog].mask = 0    
pog[#pog].sleepingAllowed = false
 pog[#pog].awake = true       
    end

end

I called the project Drop the block.
So in the code I repositioned the scene camera, created a floor, tried to attach a STATIC rigidbody. Discovered they dont use tables for filtering like categories/mask would in a default template project. They expect numbers.

So I used numbers, and coded it so that a box is created when you double tap(and of course applied a Dynamic rigidbody to the box and the box shape so that it can be moved by physics), and the camera pivots based on your swipe movement(from a completely independant fixed location).

The box you drop is dropped from thhe same height as the scene camera. Your x,y touch vectors are used for its x,z placement in space.

I’ve skimmed the craft demo code. I don’t see any examples of group/mask being used in code. So thought maybe its in the documentation. The closest I see is in the craft physics raycast, but its not a returned key-value.

The long and short of it is this. When I drop a box? It goes through the plane but it doesn’t land on it.

Tagged:

Comments

  • RdoRdo
    edited October 2017 Posts: 23

    updated the code still doesn’t work as intended. But now it has a camera height slider(so now you can move the camera down through the plane), and the box creation code has been moved into the update function

    It should be noted that I used print statements to get the default group/mask numbers. But I removed those print statements from the code.

    When they were in the code I used

    print(floor:get(craft.rigidbody).group)
    
    print(floor:get(craft.rigidbody).mask)
    

    I used then before I redefined them. I did the same thing for bx. But it still doesn’t work right even though they are coded opposite to each other.

    -- Craft Template
    displayMode(OVERLAY)
    function setup()
    
            parameter.integer("CamHeight",-HEIGHT/2,HEIGHT/2,HEIGHT/2) 
    
        -- Create a new craft scene
        scene = craft.scene()
    scene.camera.position = vec3(HEIGHT/2,HEIGHT/2,HEIGHT/2)
    floor = scene:entity()
        floor.position = vec3(HEIGHT/2,-1,HEIGHT/2)
      floor.model = craft.model.plane(vec2(1024,1024))
    mate = craft.material("Materials:Basic")
    
        mate.diffuse = color(255, 255, 255, 255)
        floor.material = mate
    
    floor:add(craft.rigidbody,STATIC)
     floor:get(craft.rigidbody).sleepingAllowed = false
     floor:get(craft.rigidbody).awake = true
    floor:get(craft.rigidbody).group = -1
      floor:get(craft.rigidbody).mask = 1        
    
    
    print(floor:get(craft.rigidbody))  
        verticalx,horizontaly = 0,0
    
        -- Create a new entity
    
     bx = {}
    
    
    end
    
    function update(dt)
        -- Update the scene (physics, transforms etc)
    
    scene.camera.y = CamHeight
        scene.camera.eulerAngles = vec3(verticalx,horizontaly,0)
    
        if addit ~= nil and addit == "y" then
            bx[#bx + 1] = scene:entity()
            bx[#bx].position = vec3(CurrentTouch.prevX,HEIGHT/2,CurrentTouch.prevY)
            surface = craft.material("Materials:Basic")
            surface.diffuse = color(math.random(0,255),math.random(0,255),math.random(0,255))
            bx[#bx].model = craft.model.cube(vec3(13,13,13))
            bx[#bx].material = surface
            bx[#bx]:add(craft.rigidbody,DYNAMIC,13)
    
        bx[#bx]:get(craft.rigidbody).group = 1        
    bx[#bx]:get(craft.rigidbody).mask = -1    
    
    bx[#bx]:get(craft.rigidbody).sleepingAllowed = false
     bx[#bx]:get(craft.rigidbody).awake = true       
    
    bx[#bx]:add(craft.shape.box,vec3(13,13,13),vec3(6.5,6.5,6.5))        
     addit = nil
    
                end
        scene:update(dt)
    end
    
    -- Called automatically by codea 
    function draw()
        update(DeltaTime)
    
        -- Draw the scene
        scene:draw()    
    end 
    function touched(touch)
        if CurrentTouch.state == MOVING then
        verticalx,horizontaly = verticalx + CurrentTouch.deltaY,horizontaly + CurrentTouch.deltaX
        end
            if CurrentTouch.tapCount == 2 then
            addit = "y"
    
    
        end
    
    end
    
    

    I also tried to write the mass of the rigidbody? It told me it did not exist or isn’t writeable so I removed that from the code. But when it was in it I used

    floor:get(craft.rigidbody).mass = 1024
    

    Was that not the right syntax?

    How come group/mask do not work the same way categories/mask does? Raycast was optional in the default project even without using it the underlying collision filtering/detection worked.

  • Posts: 789
    Have you looked at the AR demo? I think this does what you are trying to do (albeit with the live camera feed in the background)
  • RdoRdo
    Posts: 23

    Yes, I’ve looked at it but it inly has mask. Oddly enough when I used the suggested mask its revealed to be the value of 4.

    As I said earlier there is no group usage anywhere in any if the demos. Side note I’ve tried using the mask as the group and even went with 4 as the group for both the plane and the box.

    It still goes through it.

  • RdoRdo
    edited October 2017 Posts: 23

    @West I took another look at the code. Still haven’t found group usage but came to realize mask is being applied by way of the renderer. I wasn’t using renderer.

    I also realized that they were setting the center of the shape to make it work and did the same.

    Updated code down below. Still no group usage but it works. The block stops now. Thank you for the suggestion.

    -- Craft Template
    displayMode(OVERLAY)
    function setup()
    
            parameter.integer("CamHeight",-HEIGHT/2,HEIGHT/2,HEIGHT/2) 
    
        -- Create a new craft scene
        scene = craft.scene()
    scene.camera.position = vec3(HEIGHT/2,HEIGHT/2,HEIGHT/2)
    floor = scene:entity()
        floor.position = vec3(HEIGHT/2,-1,HEIGHT/2)
      floor.model = craft.model.plane(vec2(1024,1024))
        floor:add(craft.rigidbody,STATIC)
          floor:add(craft.shape.box,vec3(1024,2,1024),vec3(0,1,0))
    
    
       -- self.entity:add(craft.shape.box, vec3(1,0.1,1), vec3(0,-0.05,0))
    
    
    mate = craft.material("Materials:Basic")
    
        mate.diffuse = color(255, 255, 255, 255)
        floor.material = mate
    
     floor:get(craft.rigidbody).sleepingAllowed = false
    
      floor:get(craft.renderer).mask = 1
    
    
    print(floor:get(craft.rigidbody).group)  
        verticalx,horizontaly = 0,0
    
        -- Create a new entity
    
     bx = {}
    
    
    end
    
    function update(dt)
        -- Update the scene (physics, transforms etc)
    
    scene.camera.y = CamHeight
        scene.camera.eulerAngles = vec3(verticalx,horizontaly,0)
        floor.active = true
        if addit ~= nil and addit == "y" then
            bx[#bx + 1] = scene:entity()
            bx[#bx].position = vec3(CurrentTouch.prevX,HEIGHT/2,CurrentTouch.prevY)
            surface = craft.material("Materials:Basic")
            surface.diffuse = color(math.random(0,255),math.random(0,255),math.random(0,255))
            bx[#bx].model = craft.model.cube(vec3(13,13,13))
            bx[#bx].material = surface
            bx[#bx]:add(craft.rigidbody,DYNAMIC,13)
    
    
    bx[#bx]:get(craft.rigidbody).sleepingAllowed = false
    
            print(bx[#bx]:get(craft.rigidbody).group)
    bx[#bx]:add(craft.shape.box,vec3(13,13,13),vec3(0,6.5,0))        
     addit = nil
    
                end
        scene:update(dt)
    end
    
    -- Called automatically by codea 
    function draw()
        update(DeltaTime)
    
        -- Draw the scene
        scene:draw()    
    end 
    function touched(touch)
        if CurrentTouch.state == MOVING then
        verticalx,horizontaly = verticalx + CurrentTouch.deltaY,horizontaly + CurrentTouch.deltaX
    
        end
            if touch.tapCount == 2 and touch.state == BEGAN then
            addit = "y"
    
    
        end
    
    end
    
  • JohnJohn Admin Mod
    Posts: 622

    @Rdo
    Groups and masks are bitfields. This allows 32 independent groups and masks for each rigidbody that can be enabled and disabled. These can be controlled via bitwise operators such as <<, | and &. Each bit value is a power of 2, this makes the first bit 1, the second 2, the third 4, and so on.

    Two rigidbodies will collide with each other if both of their respective groups and masks have bits in common by using the following equation:

    ((rb1.group & rb2.mask) != 0) and ((rb2.group && rb1.mask) != 0
    

    By default all rigidbodies will collide with each other. One issue I'm seeing with your example is that you are using very large values for everything. Craft uses meters as units so keep that in mind. I'll come up with a simple example of how to us them properly and post it soon.

Sign In or Register to comment.