Howdy, Stranger!

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

Craft example spheres

dave1707dave1707 Mod
in Code Sharing Posts: 7,155

Here’s something I threw together just to see what would happen. I found it kind of interesting, so I thought I’d share it. When the program starts, 300 random spheres move towards coords 0,0,0 . Once they collect there, you can tap the screen to shoot another sphere at them to cause a collision. You can keep tapping the screen to shoot more spheres. To restart the program, just tap the restart icon in the lower left of the screen. It doesn’t do a whole lot, but I thought someone else might find it interesting.

displayMode(FULLSCREEN)

function setup()
    tab={}
    assert(craft, "Please include Craft as a dependency")
    assert(OrbitViewer, "Please include Cameras (not Camera) as a dependency")
    scene = craft.scene()
    v=scene.camera:add(OrbitViewer, vec3(0,0,0), 500, 0, 500)
    scene.physics.gravity=vec3(0,0,0)    
    for z=1,300 do
        create()
    end
end

function draw()
    update(DeltaTime)
    scene:draw()    
    if not tch then
        for a=1,#tab do
            x,y,z=tab[a][2].x,tab[a][2].y,tab[a][2].z
            tab[a][1].linearVelocity=vec3(-x/2,-y/2,-z/2)
        end
    end
    v.rx=v.rx+.2  
    v.ry=v.ry+.2 
end

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

function create()
    size=math.random(40)/10
    sphere=scene:entity()
    s=sphere:add(craft.rigidbody,DYNAMIC)
    s.restitution=1
    sphere.position=vec3(math.random(-300,300),
            math.random(-300,300),
            math.random(-300,300))
    sphere:add(craft.shape.sphere,size*1.2)
    sphere.model = craft.model.icosphere(size,2)
    sphere.material = craft.material("Materials:Specular")
    r,g,b=math.random(255),math.random(255),math.random(255)
    sphere.material.diffuse=color(r,g,b)
    table.insert(tab,{s,sphere})
end

function touched(t)
    if t.state==BEGAN then
        if not tch then
            for z=1,#tab do
                tch=true
                tab[z][1].linearVelocity=vec3(0,0,0)
            end
        end
        sp9=scene:entity()
        s9=sp9:add(craft.rigidbody,DYNAMIC)
        s9.restitution=1
        s9.linearVelocity=vec3(0,0,-1000)
        sp9.position=vec3(0,0,400)
        sp9:add(craft.shape.sphere,8)
        sp9.model = craft.model.icosphere(4,2)
        sp9.material = craft.material("Materials:Specular")
        sp9.material.diffuse=color(255,0,0)   

    end
end
Sign In or Register to comment.