Any suggestions on how to tune up physics.body/movement of imported mesh?

Dear Codea Community,

I’m returning to working in Codea after a hiatus and would like to import a proof of concept project I’ve been working on outside Codea that allows user to in real-time switch viewpoints and control different complex meshes. I had stopped working on it in Codea for a while as I had a hard time controlling the imported meshes. I had previously written the community and got some good suggestions for 3D physics with the built-in assets/voxels but could never get the more complicated imported meshes to work correctly with rigid bodies and the physics engine (bodies would not move or move incorrectly).

I tried again and still could not get the easiest integration solution to work (i.e. craft.shape.model). However, for basic testing purposes, I was able to fit a craft.shape.box to my imported test mesh (a crab). I though had no framework to know what size of a box I should use for my imported mesh and had to do a lot of trial and error. Thus, for this simple test project, I’ve included:

imported mesh (a crab)
a cube block mesh (to test collisions)
a floor (for perspective)
a nice sky background (to make the crab feel at home :blush:)
keyboard commands to control the force on the crab (movement, rotation)
[I’ve been testing using iPad with external keyboard)

I’d appreciate the community’s suggestions on how to optimize 3D physics (movement by forces on its rigid body) on my imported mesh so that I (and hopefully others) could use such an optimized strategy for applying 3D physics to other imported complex meshes.

Thanks! :blush:

Code:


viewer.mode=FULLSCREEN

function setup()
    
    -- setup the scene
    
    rectMode(CENTER)
    scene = craft.scene()

    scene.sky.material.envMap = craft.cubeTexture(asset.SeaSky)
    scene.camera.position = vec3(0,0,-10)
    
    -- crab model available for free download at: http://3dmag.org/en/market/item/1590/
    Crab = scene:entity()
    Crab.model = craft.model(asset.crab.Crab_obj)
    Crab.scale = vec3(2,2,2)
    Crab.eulerAngles = vec3(0,-180,0)
    CrabBody = Crab:add(craft.rigidbody, DYNAMIC)
    Crab:add(craft.shape.box, vec3(1,0.5,1))
    -- CrabBody.friction = [could set]
    -- CrabBody.linearDamping = [could set]
    
    Cube = scene:entity()
    Cube.model = craft.model.cube()
    Cube.position = vec3(1.5,0,0)
    Cube.material = craft.material(asset.builtin.Materials.Standard)
    Cube.material.map = readImage(asset.builtin.Surfaces.Basic_Bricks_Roughness)
    CubeBody = Cube:add(craft.rigidbody, DYNAMIC)
    Cube:add(craft.shape.box, vec3(1,1,1))
    
    floor = scene:entity()
    floor.model = craft.model.plane(vec2(60,60))
    floor.position = vec3(0,-0.5, 0)
    floor.material = craft.material(asset.builtin.Materials.Standard)
    floor:add(craft.rigidbody,STATIC)
    floor:add(craft.shape.model,floor.model)
    floor.material.map = readImage(asset.builtin.Surfaces.Basic_Bricks_Roughness)
    
    -- movement forces (axis + torque in y axis (t))
    x,y,z,t = 0,0,0,0
    craft.physics.gravity = vec3(0,0,0)
    -- setup optimally to work with remote keyboard 
    showKeyboard()
end
    

function draw()
    CrabBody:applyForce(vec3(x,y,z))
    CrabBody:applyTorque(vec3(0,t,0))
    scene:update(DeltaTime)
    scene:draw()
    end

function keyboard(key)
    if key == "w" then 
        scene.camera.position = scene.camera.position + vec3(0,0,1)
    end
    if key == "a" then
        scene.camera.position = scene.camera.position + vec3(1,0,1)
    end
    if key == "s" then
        scene.camera.position = scene.camera.position + vec3(0,0,-1)
    end
    if key == "d" then
        scene.camera.position = scene.camera.position + vec3(-1,0,0)
    end
    if key == "q" then
        scene.camera.position = scene.camera.position + vec3(0,1,0)
    end
    if key == "e" then
        scene.camera.position = scene.camera.position + vec3(0,-1,0)
    end
    if key == "i" then
        x,y,z = 0,0,3
    end
    if key == "j" then
        x,y,z = 3,0,0
    end
    if key == "k" then
        x,y,z = 0,0,-3
    end
    if key == "l" then
        x,y,z = -3,0,0
    end
    if key == "u" then
        x,y,z = 0,5,0
    end
    if key == "o" then
        x,y,z = 0,-5,0
    end
    if key == "m" then
        t = -3
    end
    if key == "." then
        t = 3
    end
end


SimpleCrab.zip (1.9 MB)