Exploding mesh with shaders

@andrew_stacey I just wanted to set up an explosion in “setup” and then, when a certain variable becomes true, I want to begin and draw the explosion in the draw function (in main).

@Monkeyman32123 It is so much easier to debug with some actual code. Could you post your attempt somewhere?

In the “drawship” function I did this

If youdead and explosion == nil then
local img = image(self.cw,self.lh) -- image the size of the current word
setContext(img)
resetMatrix()
-- draw word on to image
setContext()
self.explosion = Explosion({
    image = img,
    centre = shiploc,
    trails = true
})
self.explosion:activate(1.5)
End
If explosion ~= nil then
    explosion:draw()
End

@monkeyman32123 try this:

-- 0test

-- Use this function to perform your initial setup
function setup()
    print("Hello World!")
    img = image(500,500)
    setContext(img)
    fill(255, 0, 0, 255)
    fontSize(50)
    text("Hello World",250,250)
    setContext()
    
    
end

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(40, 40, 50)

    -- This sets the line thickness
    strokeWidth(5)
    
    if explosion then
        explosion:draw()
    end

    -- Do your drawing here
    
end

function touched(t)
    if t.state == BEGAN then
        explosion = Explosion({
    image = img,
    centre = vec2(WIDTH/2,HEIGHT/2),
    trails = false,
    rows = 50,
    cols = 50
    })
        explosion:activate(1)
    end
end



```

Here is a main tab that lets you play with all the explosion shader settings. This is only the main tab, grab the explosion shader from above.

function setup()
    explode = nil
    displayMode(OVERLAY)
    parameter.number("Friction",0.01,2)
    parameter.integer("Factor",1,100,10)
    parameter.integer("Rows",2,100,20)
    parameter.integer("Cols",2,100,20)
    parameter.boolean("Trails")
    parameter.integer("TrailLength",1,100,16)
    parameter.number("TrailSeparation",0.01,1,0.5)
    parameter.action("Explode",function()
        explode = nil
        print(Trails)
        local img = readImage("Cargo Bot:Codea Icon")
        explode = Explosion({image = img,
                            friction = Friction,
                            factor = Factor,
                            rows = Rows,
                            cols = Cols,
                            trails = Trails,
                            trailLength = TrailLength,
                            trailSeparation = TrailSeparation,
                            centre = vec2(0,0)            
        })
        explode:activate(1)
        end)

end

-- This function gets called once every frame
function draw()
    background(0, 0, 0, 255)
    if explode then
        translate(WIDTH/2,HEIGHT/2)
        explode:draw()
    end

    
end


```

@Monkeyman32123 You’re missing a couple of selfs in that code.

I have it so that it sets up the explosion in setup, then activates it when you die, then draws it in the draw step…and it works…BUT it makes the explosion always in the place I set it to be in setup (obviously)…is there a way for me to keep setting up the explosion in setup but change it’s location when you die before it draws? If I try to setup the explosion anywhere but in setup it causes massive lag spikes

Can’t you wrap it in a translate?

translate(100,200)
explosion:draw()

Yeah, I literally just figured that out right before you commented XD
But thank you sir