How to apply snaphost api to physic objects?

Tried to use snapshot api to apply multiple effects. That works perfectly for static items. But non static objects like physical i am not sure to works. I cant ensure to apply effects to grouped physics object. How can i solve this problem? Are there any sample code for demontrate this case?

Thanks

Hi @ulrozremo,

Yes, you can create a snapshot object, then apply a filter to it, then add a physics body. I just tested the following code and it works for me.

[lua]

local snapshot = display.newSnapshot( 200, 200 )

math.randomseed( 0 )

– Add fish to the screen

for i=1,4 do

    local fish = display.newImage( “fish.png” )

    – move to random position in a 200x200 region relative to snapshot’s origin

    fish:translate( math.random( -100, 100 ), math.random( -100, 100 ) )

    – insert fish into snapshot

    snapshot.group:insert( fish )

end

local halfW, halfH = display.contentCenterX, display.contentCenterY

snapshot:translate( halfW, halfH )                   – Center group

snapshot:invalidate()                                – Invalidate snapshot

snapshot.fill.effect = “filter.sepia”

physics.addBody( snapshot )

[/lua]

However, snapshots are a premium graphics feature available to Pro and Enterprise users, so you may not be able to run this code.

Best regards,

Brent

Thanks for your reply. But that is not same think. Imagine to bouncy 100 balls on the screen. Apply blur effect to balls when you create new and apply all balls(group) to single effect like colormatrix. sample code is bellow. thanks

local physics = require("physics") physics.setDrawMode("normal") physics.start() -- create borders local b1 = display.newRect(100,100,10,260) b1.anchorX, b1.anchorY = 0,0 b1:setFillColor(1,0,0) physics.addBody(b1, "static") local b2 = display.newRect(200,100,10,300) b2.anchorX, b2.anchorY = 0,0 b2:setFillColor(1,0,0) physics.addBody(b2, "static") local b3 = display.newRect(100,400,110,10) b3.anchorX, b3.anchorY = 0,0 b3:setFillColor(1,0,0) b3.rotation = 0 physics.addBody(b3, "static") local i = 0 local particles = {} -- generate random splashes/particles local function onEveryFrame( event ) local maxParticleCount = 600 if i % 1 == 0 then local dif = 10 if #particles \> maxParticleCount then dif = 0 end for i=1,dif do local tmp = display.newImage("3.png") tmp.x, tmp.y = math.random(160,190),math.random(160,200) -- apply blur effect to particle tmp.fill.effect = "filter.blur" physics.addBody(tmp, {friction=0.3, radius=2, density=0.1, bounce=0}) table.insert(particles, tmp) end --question is here: --and now, i must apply color matrix effect to entire particles --this ensures splashes looks like a water or somethink like that(i think) end i = i + 1 for k,v in pairs(particles) do if v.y \> 500 then table.remove(particles, k) v:removeSelf() end end end Runtime:addEventListener( "enterFrame", onEveryFrame )

Hi @ulrozremo,

Yes, you can create a snapshot object, then apply a filter to it, then add a physics body. I just tested the following code and it works for me.

[lua]

local snapshot = display.newSnapshot( 200, 200 )

math.randomseed( 0 )

– Add fish to the screen

for i=1,4 do

    local fish = display.newImage( “fish.png” )

    – move to random position in a 200x200 region relative to snapshot’s origin

    fish:translate( math.random( -100, 100 ), math.random( -100, 100 ) )

    – insert fish into snapshot

    snapshot.group:insert( fish )

end

local halfW, halfH = display.contentCenterX, display.contentCenterY

snapshot:translate( halfW, halfH )                   – Center group

snapshot:invalidate()                                – Invalidate snapshot

snapshot.fill.effect = “filter.sepia”

physics.addBody( snapshot )

[/lua]

However, snapshots are a premium graphics feature available to Pro and Enterprise users, so you may not be able to run this code.

Best regards,

Brent

Thanks for your reply. But that is not same think. Imagine to bouncy 100 balls on the screen. Apply blur effect to balls when you create new and apply all balls(group) to single effect like colormatrix. sample code is bellow. thanks

local physics = require("physics") physics.setDrawMode("normal") physics.start() -- create borders local b1 = display.newRect(100,100,10,260) b1.anchorX, b1.anchorY = 0,0 b1:setFillColor(1,0,0) physics.addBody(b1, "static") local b2 = display.newRect(200,100,10,300) b2.anchorX, b2.anchorY = 0,0 b2:setFillColor(1,0,0) physics.addBody(b2, "static") local b3 = display.newRect(100,400,110,10) b3.anchorX, b3.anchorY = 0,0 b3:setFillColor(1,0,0) b3.rotation = 0 physics.addBody(b3, "static") local i = 0 local particles = {} -- generate random splashes/particles local function onEveryFrame( event ) local maxParticleCount = 600 if i % 1 == 0 then local dif = 10 if #particles \> maxParticleCount then dif = 0 end for i=1,dif do local tmp = display.newImage("3.png") tmp.x, tmp.y = math.random(160,190),math.random(160,200) -- apply blur effect to particle tmp.fill.effect = "filter.blur" physics.addBody(tmp, {friction=0.3, radius=2, density=0.1, bounce=0}) table.insert(particles, tmp) end --question is here: --and now, i must apply color matrix effect to entire particles --this ensures splashes looks like a water or somethink like that(i think) end i = i + 1 for k,v in pairs(particles) do if v.y \> 500 then table.remove(particles, k) v:removeSelf() end end end Runtime:addEventListener( "enterFrame", onEveryFrame )