Example Entity Project - Attila717/Codea-Craft-Tutorial GitHub Wiki

Physics and Collision Mechanics

--# Main
-- Make sure to enable Craft in the dependency settings
function setup()  
  -- for fps function
  frame = 0
  etime = 0 
  parameter.watch("fps")
    
  -- positions in space
  cubexyz = vec3(0,5,0)
  platformxyz = vec3(0,-5,0)
  spherexyz = vec3(0,12,0)
    
    
  -- init four entities, cube, platform, childs (a sphere), and sphere (not actually a sphere)
  cube = craft.entity()
  sphere = craft.entity()
  childs = craft.entity()
  platform = craft.entity()
    

  -- Setup lighting
  craft.scene.sun.rotation = quat.eulerAngles(25,0,125)
  -- Set the scenes ambient lighting
  craft.scene.ambientColor = color(127, 127, 127, 255)   
    
  -- Create meshs
  local cubeMesh = craft.mesh.cube(vec3(1,1,1))
  local platformMesh = craft.mesh.cube(vec3(8,1,8))
  local sphereMesh = craft.mesh.icoSphere(1,1,true)
  local childsMesh = craft.mesh.icoSphere(1,3,false)
    
  -- add default block library (currently have to add Block Library as a dependancy)
  AllBlocks = blocks()
    
  -- Setup voxel terrain
  craft.voxels:resize(vec3(1,1,1))      
  craft.voxels.coordinates = vec3(0,0,0)
    
  -- add voxels
  local Attila = craft.voxels.blocks:new("AttilaTest")
  Attila.setTexture(ALL, "Blocks:Error")
  craft.voxels:set(vec3(0,0,0), "AttilaTest")

  -- Add the renderer component, which will actually draw our mesh
  renderer = cube:add(craft.renderer, cubeMesh)
  renderer2 = platform:add(craft.renderer, platformMesh)
  renderer3 = sphere:add(craft.renderer, sphereMesh)
  renderer4 = childs:add(craft.renderer, childsMesh)
    
  -- move cubes to certain positions and rotate the cube a bit
  cube.x,cube.y,cube.z = cubexyz.x,cubexyz.y,cubexyz.z
  sphere.x,sphere.y,sphere.z = spherexyz.x,spherexyz.y,spherexyz.z
  --childs.x,childs.y,childs.z = spherexyz.x+2,spherexyz.y,spherexyz.z
  childs.x = 3
  platform.y = platformxyz.y
  local r = 45
  cube.rotation = quat.eulerAngles(r,r,r)
  sphere.rotation = quat.eulerAngles(r,r,r)
    
  -- add physics collision bodies
  cubep = cube:add(craft.rigidbody, DYNAMIC, 0.5)
  platform:add(craft.rigidbody, STATIC)
  spherep = sphere:add(craft.rigidbody, DYNAMIC, 0.5)
  childsp = childs:add(craft.rigidbody, DYNAMIC, 0.5) 
  -- give a shape to the collision area
  -- for box, first argument is shape, second is dimentions of the bounding box, and third is the offset from your mesh
  cube:add(craft.shape.box, vec3(1,1,1), vec3(0,0,0))
  platform:add(craft.shape.box, vec3(8,1,8), vec3(0,0,0))
  sphere:add(craft.shape.mesh, craft.mesh.icoSphere(1,1))
  childss = childs:add(craft.shape.sphere, 1, vec3(0,0,0))
  --sphere:add(craft.shape.sphere, 1, vec3(0,0,0))

  -- Apply a material to the cube
  renderer.material = craft.material("Materials:Standard")
  renderer2.material = craft.material("Materials:Standard")
  renderer3.material = craft.material("Materials:Standard")
  renderer4.material = craft.material("Materials:Standard")
    
  -- Use renderer.material.map to apply a texture
  renderer.material.map = "Blocks:Greystone"
  renderer2.material.map = "Blocks:Wood Red"
  --renderer3.material.map = "Blocks:Error"
    
  -- bounciness doesn't seem to work yet
  cubep.restitution = 50
  spherep.sleepingAllowed = false
    --childs.parent = sphere
    
  view1 = craft.scene.camera:add(OrbitViewer, vec3(0,0,0), 20)

  -- Move the camera forwards and rotate 180 degrees
  --craft.scene.camera.position = vec3(-2,0,15)
  --craft.scene.camera.rotation = quat.eulerAngles(0,0,180)
end

function update()
  -- I think this is called every time the physics engine updates
    fpsEval()
end

function fpsEval()
    -- just counting fps
    frame = frame + 1
    etime = etime + DeltaTime
    fps = math.floor(frame/etime+.5)
end

function draw()
    -- Nothing happening here D:
end

function touched(touch)
    touches.touched(touch)
    if touch.state == BEGAN then
        -- wake up cube every time a touch registers
        cubep.awake = true
        if touch.x > WIDTH/3*2 then
            -- apply force to cube (up and right)
            spherep:applyForce(vec3(75,75,0))
        elseif touch.x < WIDTH/3 then
            -- apply force to cube (up and left)
            spherep:applyForce(vec3(-75,75,0))
        else
            -- apply force to cube (up)
            spherep:applyForce(vec3(0,75,0))
        end
    end
end