Howdy, Stranger!

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

Getting acquainted with Craft

Most of my time to date has been spent out of Codea Craft...but I’d like to start delving more into using Shade in Codea. I’d like to first explore using shaders in 2D...or at least fake it with a top-down camera.

My first question is really just a sanity check: am I right that good ol’ rotate(), scale(), and translate() have no effect on the drawing of the Craft scene? Do I just have to replicate that behavior with camera movement?

Tagged:

Comments

  • dave1707dave1707 Mod
    Posts: 7,912

    @brianolive In Craft, you move the camera. Here's a small example of attaching the camera to a moving object. Tilt the device left or right to turn.

    displayMode(FULLSCREEN)
    
    function setup()
        bx,bz=0,0   -- initial block position
        vel=.2      -- velocity
        dir=0
    
        scene = craft.scene()
    
        ground=scene:entity()
        ground.model = craft.model.cube(vec3(1000,1,1000))
        ground.position=vec3(0,-10,0)
        ground.material = craft.material("Materials:Basic")
        ground.material.map = readImage("Surfaces:Desert Cliff Color")
        ground.material.offsetRepeat=vec4(0,0,50,50)
    
        block = scene:entity()
        block.model = craft.model.cube(vec3(4,.5,2))
        block.material = craft.material("Materials:Standard")
        block.material.map = readImage("Blocks:Trunk White Top")
        block.position=vec3(0,0,0)
    
        cam = scene:entity()
        cam.model = craft.model.cube(vec3(.2,.2,.2))
        cam.material = craft.material("Materials:Specular")
        cam.material.map = readImage("Blocks:Brick Grey")
        cam.position=vec3(0,10,6)    
    
        cam.parent=block    -- attach camera to block
        c=cam:add(craft.camera, 120, .1, 1000, false) -- set camera values
        c.entity.eulerAngles=vec3(0,180,0)  -- set camera pointing direction
    end
    
    function update(dt)
        scene:update(dt)
    
        block.eulerAngles = vec3(0,dir,0)
        bx=bx-vel*math.sin(math.rad(dir))   -- calculate x value
        bz=bz-vel*math.cos(math.rad(dir))   -- calculate z value
        block.position=vec3(bx,0,bz)        -- update block x,z position   
    
        c.entity.eulerAngles=vec3(20,180,-Gravity.x*100)  -- camera pointing direction
    end
    
    function draw()
        update(DeltaTime)
        scene:draw()    
        dir=dir-Gravity.x*3
    end
    
  • dave1707dave1707 Mod
    Posts: 7,912

    @brianolive Here's another Craft example. Slide your finger around the screen. You're not really moving the box, but the camera.

    displayMode(FULLSCREEN)
    
    function setup()
        assert(OrbitViewer, "Please include Cameras (not Camera) as a dependency")
        scene = craft.scene()
        scene.camera:add(OrbitViewer, vec3(0,0,0), 100, 0, 500)
        c=scene:entity()
        c.position=vec3(0,0,0)
        c.model = craft.model.cube(vec3(25,10,10))
        c.material = craft.material("Materials:Standard")
        c.material.map = readImage("Blocks:Trunk White Top")
    end
    
    function draw()
        update(DeltaTime)
        scene:draw()    
        text("Drag your finger on the screen to rotate the cube",WIDTH/2,HEIGHT-100)
    end
    
    function update(dt)
        scene:update(dt)
    end
    
  • Thanks @dave1707 , much appreciated!

Sign In or Register to comment.