#### Howdy, Stranger!

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

# How to do 3D?

Posts: 4

I tried do 3D but I didn't work can someone please help me. I used a table called `vecs` but i need to draw them now.
Okay plz help

Tagged:

• Posts: 8,389

@Purple107 3D isn't easy, and will take some time to learn. The next version of Codea will have Craft which makes 3D a lot easier. Here's a simple cube.

``````function setup()
parameter.integer("R",-360,360, 60)
parameter.number("x",0,1,.5)
parameter.number("y",0,1,.5)
parameter.number("z",0,1,.5)
parameter.integer("size", 1, 50, 20)
end

function draw()
background(0, 0, 0, 255)
perspective(50,WIDTH/HEIGHT)
camera(-200,0,0,0,0,0,0,1,0)
rotate(R,x,y,z)
fill(255, 0, 0, 255)
cube(0,0,0,size,size,size)
end

function cube(x,y,z,w,h,d)
local cube = mesh()
v = {
vec3(x-w,y-h,z+d),vec3(x-w,y+h,z+d),vec3(x+w,y-h,z+d),vec3(x+w,y+h,z+d),
vec3(x-w,y-h,z-d),vec3(x-w,y+h,z-d),vec3(x+w,y-h,z-d),vec3(x+w,y+h,z-d)
}

local faces = {
v,v,v,v,v,v,
v,v,v,v,v,v,
v,v,v,v,v,v,
v,v,v,v,v,v,
v,v,v,v,v,v,
v,v,v,v,v,v
}
local c = { color(255, 0, 0, 255), color(0, 255, 0, 255), color(255, 243, 0, 255),
color(0, 0, 255, 255), color(255, 255, 255, 255), color(255, 0, 189, 255)}
local colors = {}
for i = 1, 6 do
for j = 1, 6 do
table.insert(colors, c[i])
end
end
cube.vertices = faces
cube.colors = colors
cube:draw()
end
``````
• edited July 2017 Posts: 4

@dave1707 That looks hard. Thank you for the code.

• Posts: 69
@Purple107 for now until craft is fully released I suggest you study the 3d example a lot! Make small changes and see how they effect your game. After a while you will understand it a lot better!
• Posts: 889

@GR00G0 My opinion is doing it after CC. Then it will be times easier?

• edited July 2017 Posts: 8,389

@Purple107 Just to give you an idea of the simplicity of Craft 3D, here's a program that draws a red cube and allows you to rotate it in the x,y,z axis similar to my above program.

NOTE: This requires Craft 3D which is in the next Codea version.

``````function setup()
parameter.integer("x",-180,180,45)
parameter.integer("y",-180,180,30)
parameter.integer("z",-180,180,60)

scene=craft.scene()
cube=scene:entity()
cube.z=10
r.material=craft.material("Materials:Standard")
r.material.diffuse=color(255,0,0)
end

function update(d)
cube.rotation = quat.eulerAngles(x,y,z)
end

function draw()
update(DeltaTime)
scene:draw()
end
``````