3D?

I would like to start coding simple things in 3D, but I haven’t found anything to be very helpful to me. I tried picking apart the “3D Lab” code, but nothing really clicked for me. If anyone can help in anyway, like posting some easy-to-understand code, I’d really appreciate it.

(I love the app, by the way :slight_smile: )

I’ll second this request. I have found myself stuck in the gap between noob and expert and find some of the much appreciated examples and answers quickly get out of my understanding.

Thanks again to all involved with Codea and the community.

Here is a simple example for 3D. You can make minor program changes to see what happens. I find making changes is the best way to understand what’s going on. Also, having a simple program helps too.


-- 3d example

function setup()
    displayMode(STANDARD)   
    setVerts()
    parameter("Size",1,100,25)
    parameter("CameraHeight", 0, 1000, 0)
    parameter("Angle",-180, 180, 0)
    parameter("FieldOfView", 1, 140, 100)            
end

function draw()   
    perspective(FieldOfView, WIDTH/HEIGHT)
    camera(0,CameraHeight,-300, 0,0,0, 0,1,0)   
    background(40, 40, 50)
    rotate(Angle,0,1,0)
    s = Size*0.25
    scale(s,s,s)
    mg:draw()    
end

function setVerts()
    -- define 3d coordinates to be used
    vertices = 
    {
      vec3(-12,-4,16),
      vec3(12,-4,16),
      vec3(12,4,16),
      vec3(-12,4,16),
      vec3(-12,-4,-16),
      vec3(12,-4,-16),
      vec3(12,4,-16),
      vec3(-12,4,-16),
    
      vec3(13,4,16),
      vec3(0,4,16),
      vec3(0,8,16),
    
      vec3(-13,4,16),
      vec3(0,4,16),
      vec3(0,8,16),
    
      vec3(13,4,-16),
      vec3(0,4,-16),
      vec3(0,8,-16),
    
      vec3(-13,4,-16),
      vec3(0,4,-16),
      vec3(0,8,-16),
    
      vec3(-4,-4,16),
      vec3(0,-4,16),
      vec3(0,4,16),
    }
    
    -- construct a rectangle out of the 3d coordinates
    -- rectangles are made from 2 triangles
    rects = {
      -- Front
      vertices[1], vertices[2], vertices[3],
      vertices[1], vertices[3], vertices[4],
      -- Right
      vertices[2], vertices[6], vertices[7],
      vertices[2], vertices[7], vertices[3],
      -- Back
      vertices[6], vertices[5], vertices[8],
      vertices[6], vertices[8], vertices[7],
      -- Left
      vertices[5], vertices[1], vertices[4],
      vertices[5], vertices[4], vertices[8],
      -- Top
      vertices[4], vertices[3], vertices[7],
      vertices[4], vertices[7], vertices[8],
      -- Bottom
      vertices[5], vertices[6], vertices[2],
      vertices[5], vertices[2], vertices[1],

      vertices[9],  vertices[10], vertices[11],
      vertices[12], vertices[13], vertices[14],
    
      vertices[15],  vertices[16], vertices[17],
      vertices[18], vertices[19], vertices[20]

    }

    mg = mesh()
    mg.vertices = rects  
    mg:setColors(0,0,255,255) -- set initial color to blue
    
    -- set individual color for each vertices     
    mg:color(1,0,255,0)    -- color for vertices 1
    mg:color(2,0,255,0)    -- color for vertices 2 etc.
    mg:color(3,0,255,0)
    mg:color(4,255,0,0)
    mg:color(5,255,0,0)
    mg:color(6,255,0,0)
    mg:color(7,0,255,0)
    mg:color(8,0,255,0)  
    mg:color(9,0,255,0) 
    mg:color(10,0,0,255)
    mg:color(11,0,0,255)
    mg:color(22,0,0,255)

    mg:color(28,255,255,255)
    mg:color(29,255,255,255)
    mg:color(30,255,255,255)
    
    mg:color(25,255,255,255)
    mg:color(26,255,255,255)
    mg:color(27,255,255,255)

    mg:color(37,0,255,0)
    mg:color(38,0,255,0)  
    mg:color(39,0,255,0)    
    
    mg:color(40,0,255,0)
    mg:color(41,0,255,0)  
    mg:color(42,0,255,0)
        
    mg:color(43,0,255,0)
    mg:color(44,0,255,0)  
    mg:color(45,0,255,0)   

    mg:color(46,0,255,0)
    mg:color(47,0,255,0)  
    mg:color(48,0,255,0)  
end

Thanks, dave1707. I’ll load this in and try and change the code to achieve some new features.

Here’s the first of probably many questions! Have I got the following correct?

The front of the rectangle is drawn from 2 triangles, the first of which has its first point 12 left, 4 down and 16 into the screen. Its second point is 12 right, 4 down and 16 into the screen, and its third point is 12 right, 4 up and 16 into the screen. This means the first triangle is flat in the z plane (all verts at 16 into the screen), it is 24 long in the x plane, 8 long in the y plane on the right hand side tapering to zero length in the y plane on the left hand side.

That’s correct. I was originally trying to create a 3D building 24 wide, 8 high, 32 deep with a peek on the front and back, 26 wide by 4 high at the center. In order to fill a full triangle with color, you have to set the same color for the 3 vertices. You can also set 3 different colors for the vertices to give the triangle multiple colors. Apparently you need to create everything from triangles. There may be easier ways to do this, but I didn’t spend a lot of time on it. When I’m trying to learn new things, I like to keep things as simple and small as possible and start changing one thing at a time to see the effect. Hope this helps.

Thanks, it does help.

I’m struggling with the Perspective function. I think it sets the outwards angle of projection from the camera position, the same angle wide as high, so that it defines the view frustum (a new term to me!), my reference of which is the info in http://ksgamedev.wordpress.com/tag/maths/. So in your example code, decreasing the FOV angle makes the visible world smaller, resulting in the camera moving closer to the drawn object so that the clipping occurs at the 2D screen’s boundaries?

Manatee, sorry to hijack your thread. I hope I’m not stopping you from getting the info you need.

No problem, time_trial.

And thanks dave1707! That’s just what I needed. I’ll fiddle around with the code on a 14 hour plane ride tomorrow; I’ll probably have more questions!

@time_trial
The FOV is the view angle you see. It’s like the reverse of a telescope. The larger the FOV, the lower the telescope power and the more of the object you see. The smaller the FOV, the higher the telescope power and the less of the object you’ll see, but I don’t think the camera changes position in relation to the object. The FOV differs from the SIZE. By increasing the SIZE, the camera seems to get closer to the center of the object, but the object is just getting bigger. If you position the building so you are looking at it straight on, (size 40, camera height 0, angle 0, FOV 140, and increase the size, you will go through the front wall and into the building. Changing the angle will then show you the different walls from the inside. I haven’t played around with the 3D functions that much, so I hope I’m giving you correct info. I’m mostly playing with the physics.body and physics.joint functions right now.