Howdy, Stranger!

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

Motion Blur

Hi guys, I just realised that if you don't use background() you can get neat motion blur on stuff. Most of you probably know this, so to other noobs like me, if you don't use background, it doesn't clear the previous drawing, and if you draw a semi-transparent rectangle over the whole screen, previous drawings will slowly fade.
An example is here:


--# Main -- Bouncing ball function setup() rectMode(CORNERS) ellipseMode(CENTER) parameter.number("blurAmount", 0, 20, function() blurAlpha = 255/(blurAmount + 1) end) ball = physics.body(CIRCLE, 100) ball.restitution = 1 ball.x = WIDTH/2 ball.y = HEIGHT/2 box = physics.body(CHAIN, vec2(0,0), vec2(WIDTH,0), vec2(WIDTH,HEIGHT), vec2(0,HEIGHT), vec2(0,0)) end function draw() --background(0) -- You don't actually need to have a background fill(0,blurAlpha) rect(0,0,WIDTH,HEIGHT) fill(255) ellipse(ball.x, ball.y, 200) physics.gravity(Gravity.x * 500, Gravity.y * 500) end

Comments

  • dave1707dave1707 Mod
    Posts: 7,605

    Nice example. You get interesting results when you try coding things out of the ordinary.

  • dave1707dave1707 Mod
    Posts: 7,605

    @MattthewLXXIII I dug up one of my old solar system projects and added your motion blur to it. Tap the screen to cycle thru the different trail options, No trails, Full trails, and Partial trails (motion blur).

    displayMode(FULLSCREEN)
    
    function setup()
        pl={}
        table.insert(pl,planet(WIDTH/2,HEIGHT/2+50,-2.1,0))
        table.insert(pl,planet(WIDTH/2,HEIGHT/2+100,-1.74,0))
        table.insert(pl,planet(WIDTH/2,HEIGHT/2+150,-1.4,0))
        table.insert(pl,planet(WIDTH/2,HEIGHT/2+200,-1.2,0))
        table.insert(pl,planet(WIDTH/2,HEIGHT/2+250,-1.1,0))
        table.insert(pl,planet(WIDTH/2,HEIGHT/2+300,-1,0))
        table.insert(pl,planet(WIDTH/2,HEIGHT/2+350,-.94,0))
        table.insert(pl,planet(WIDTH/2,HEIGHT/2+400,-.91,0))    
        sunX=WIDTH/2
        sunY=HEIGHT/2
        sunGravity=300
        show=0
    end
    
    function draw()
        if show==0 then
            str="No trails"
            done=false
            background(0)        
        elseif show==1 and not done then
            str="Full trails"
            backingMode(RETAINED)
            done=true
        elseif show==2 then
            str="Partial trails"
            fill(0,12)
            rect(0,0,WIDTH,HEIGHT)
        end
        fill(255)
        ellipse(sunX,sunY,10)
        for a,b in pairs(pl) do
            b:calc()
            b:draw()        
        end
        text("Tap screen for next option.",WIDTH/2,HEIGHT-25)
        text(str,WIDTH/2,HEIGHT-50)
    end
    
    function touched(t)
        if t.state==BEGAN then
            show=(show+1)%3
        end
    end
    
    planet=class()
    
    function planet:init(x,y,xv,yv)
        self.x=x
        self.y=y
        self.xVel=xv
        self.yVel=yv
    end
    
    function planet:calc()
        local d=(sunGravity/((self.x-sunX)^2+(self.y-sunY)^2)^1.5)
        self.xVel=self.xVel-((self.x-sunX)*d)
        self.yVel=self.yVel-((self.y-sunY)*d)                
        self.x=self.x+self.xVel
        self.y=self.y+self.yVel    
    end
    
    function planet:draw()
        ellipse(self.x,self.y,4)    
    end
    
  • Hi @dave1707, thanks for your response. What happens if you use backingMode(STANDARD)? It still seems to retain the stuff that has been drawn. How does it choose whether to clear it or not?

Sign In or Register to comment.