Voxel tutorial?

Are there any tutorials around the craft stuff that start a little more basic?

Froggy does just a bit too much for me at this stage but has in there somewhere bits of what I’m after.

Failing that how do I load something created in the voxel editor and get it to move around the screen relative to my finger as done with a sprite here:

https://codea.io/talk/discussion/8987/relative-touch

Hi @RaggedTooth

Here’s a simple example that uses the existing voxel models in the simplest way I can think of. I’ve had some issues with creating new models so we might need to push some fixes for that in a new update.

-- Voxel Example

function setup()
    -- Create a new craft scene
    scene = craft.scene()

    -- Create a new entity for the frog
    frog = scene:entity()
    frog.scale = vec3(1,1,1) * 0.05
    
    -- Create an entity to display the frog's voxel model and parent to the frog entity
    local model = scene:entity()
    model.parent = frog
    
    -- Add a volume component (displays voxel models)
    local v = model:add(craft.volume)
    -- Load an existing model from the Froggy project (any model created with Codea will work, including the Voxel Editor project)
    v:load(asset.documents.Craft.Froggy.Player_cvox)

    -- Offset the entity's local position based on the size of the voxel model so that it is centered
    model.position = -vec3(v:size()) * 0.5
    
    frog.z = 5
end

function update(dt)
    -- Update the scene (physics, transforms etc)
    scene:update(dt)
    
    -- Randomly rotate the frog over time
    local angle = ElapsedTime * 45
    frog.rotation = quat.eulerAngles(angle, angle, angle)
end

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

    -- Draw the scene
    scene:draw()	
end

function touched(touch)
    -- Convert from screen to world coordinates using the main camera component (z is left at 5 to keep it consistent)
    local p = vec3(touch.x, touch.y, 5)
    frog.position = scene.camera:get(craft.camera):screenToWorld(p)
end

I’d like to support more simple examples like this one. it’s far easier, i think, for a beginner to pick up how to do things.

i see that there’s an issue with organizing the examples, maybe making a search for them, so they’re findable, but i think it’s worth doing.

i know you’re just a couple of wandering minstrels and all, and i’ll help if i can.

Agreed @RonJeffries, whilst things like the Froggy code are amazing to dig into, they’re also way beyond me right now.

There is very little stuff from first principles or explanations in simpler terms (As the questions I’m about to ask in my next post will clearly show!). There is more for the non Craft stuff, but even most of those are somewhat outdated (but will work) with the latest Codea.

Hi @John

Apologies for the slow reply, I’ve tried to take a few days away from the keyboard. Anyway…

Thank you, that’s a great help and I’m getting results playing with it (multiple models on the screen, some moving independently others under your control) and thrown in a few other things.

But I’d be lying if I actually understood completely, so some questions if you don’t mind. Apologies if they’re a bit stupif…

    -- Create a new entity for the frog
    frog = scene:entity()
    frog.scale = vec3(1,1,1) * 0.05

How big are models created in the editor? I created a 15*15 model and needed similar scaling to get it to a reasonable size. Obviously I can go blindly with this and just play around until my models look right, but I just wondered how a model related to the scene by default.

    -- Create a new entity for the frog

    -- Create an entity to display the frog's voxel model and parent to the 

    -- Add a volume component (displays voxel models)

    -- Load an existing model from the Froggy project (any model created 

So these stages need to be done for every model you want to display? I’ve just repeated this with a frog2 = scene:entity() and get a second model.

What if I wanted 10, Could you do a loop and have an array, sorry table(?) of frogs and reference them frog(1), frog(2)?

    -- Offset the entity's local position based on the size of the voxel model so that it is centered
    model.position = -vec3(v:size()) * 0.5

    frog.z = 5

OK, I have no clue how this puts the model into the middle of the screen!

If frog.z is setting the z coordinate. Why can’t you use frog.x and frog.y to position the frog?

Later on you position the frog with:

    local p = vec3(touch.x, touch.y, 5)
    frog.position = scene.camera:get(craft.camera):screenToWorld(p)

Can you position the frog using this principal in the setup code? I tried and didn’t seem to be able to. Obviously I was replacing the touch.x/y with more meaningful variables.

frog.rotation = quat.eulerAngles(angle, angle, angle)

OK, this just works, but why do you need quat.eulerAngles and not just put in the angles?

When messing with non Craft stuff I’ve added a background image using:

sprite(imageBackground, WIDTH/2, HEIGHT/2, WIDTH, HEIGHT)

When I try this with the example above I get the background, but none of the models showing. I assume the background is on top.

How do I move it back, or the models on top of it.

Or does it work completely differently with Craft stuff?

@RaggedTooth - if you want a background in Craft I think there are three ways to do it:

  1. Use a Skybox which is basically a cube containing your object. Add a light source to ensure all internally is visible.

  2. Or, create a large 3D image plain behind your Voxel World placed with 3D vertices.

  3. You can set up a scene in Craft where you can specify sky and horizon colour.

It depends what effect you need to generate and the scope of your Voxel world.

The voxel world is static for this, so I just want to put an image that fills the screen as a background.

@RaggedTooth - Ok, you’ll have to build an image as a texture for a cube which has dimensions in the x and y direction to cover the screen area you need. Make the z dimension 0.1 and your away. If you check out @dave1707 ‘s starter game 22 (worth a look in it’s own right) you can see what he has done for the terrain. You just need something like that in a vertical orientation.

Thanks, I’ll take a look, tomorrow, it’s late here. So many hidden gems.

To save others hunting, it’s here:

https://codea.io/talk/discussion/8932/starter-game-22

@John @RaggedTooth Here’s some code that I stripped down from the examples to be as basic as I can get to show something working. I would like to see more code like this in the examples that show the very basic code needed to do something and then other code like what’s in the examples we have now that’s more complicated. It’s hard for new users to look at the examples and try to figure out how to just do something basic to start with.

-- Voxel Example

displayMode(FULLSCREEN)

function setup()
    fx,fy,fz=20,0,0
    cx,cy,cz=0,0,0
    scene = craft.scene()
    v = scene.camera:add(OrbitViewer, vec3(0,0,0), 600, 0, 2000)
    v.rx=30
    
    frog = scene:entity()
    local a = frog:add(craft.volume)
    a:load(asset.documents.Craft.Froggy.Player_cvox)
    frog.rotation=quat.eulerAngles(0,0,0)
    frog.scale = vec3(1,1,1) * 0.8
    
    car = scene:entity()
    local a = car:add(craft.volume)
    a:load(asset.documents.Craft.Froggy.Car_cvox)
    car.rotation=quat.eulerAngles(0,90,0)
    
    floor=scene:entity()
    floor.model = craft.model.cube(vec3(400,.2,400))
    floor.position=vec3(0,-1,0)
    floor.material = craft.material(asset.builtin.Materials.Basic)
    floor.material.map = readImage(asset.builtin.Blocks.Brick_Grey)
    floor.material.offsetRepeat=vec4(0,0,30,30)

end

function update(dt)
    scene:update(dt)
end

function draw()
    update(DeltaTime)
    scene:draw()  
    fz=fz-.1
    cx=cx-.1
    frog.position = vec3(fx,fy,fz)  
    car.position = vec3(cx,cy,cz)  
end

@dave1707 thanks for the code, that does look even simpler, and yes, totally agree with your comment.

I guess we’re all programmers at heart, so documentation and examples are very hard to bring yourself to do!

A couple of comments in the code would be quite nice… ;j

@RaggedTooth Are you talking about comments in my code. Even when I was working I didn’t comment code. I would always tell my boss that the code was the comments. It was always up to date and correct for what it was doing. I always tried to keep my code as simple as possible so when I looked at it later I knew exactly what it was doing. Habits are hard to break.