here’s an example to do something like this.
but honestly, the performance on an actual device is not very satisfying…
This works great with particle counts up to 100, but then it gets sloppy. I created this with much bigger objects (bouncing balls) and quickly changed it to “sand mode”. There has to be a better solution, but you should get the idea:
-- Project: PhysicsTest
-- Description: particle physics test
--
-- 2011 finefin
local physics = require("physics")
physics.start()
physics.setGravity( 0, 40 )
--physics.setDrawMode( "hybrid" )
-- A general function for dragging physics bodies
local function dragBody( event )
local t = event.target
local phase = event.phase
if "began" == phase then
display.getCurrentStage():setFocus( t )
t.isFocus = true
-- Store initial position
t.x0 = event.x - t.x
t.y0 = event.y - t.y
if event.x \> t.x then
t.rotation = t.rotation - 10
elseif event.x \< t.x then
t.rotation = t.rotation + 10
end
-- Make body type temporarily "kinematic" (to avoid gravitional forces)
event.target.bodyType = "kinematic"
-- Stop current motion, if any
event.target:setLinearVelocity( 0, 0 )
event.target.angularVelocity = 0
elseif t.isFocus then
if "moved" == phase then
t.x = event.x - t.x0
t.y = event.y - t.y0
elseif "ended" == phase or "cancelled" == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false
-- Switch body type back to "dynamic", unless we've marked this sprite as a platform
if ( not event.target.isPlatform ) then
event.target.bodyType = "dynamic"
end
end
end
-- Stop further propagation of touch event!
return true
end
-- create world
local wallLeft = display.newRect( 0, -50, 10, 530 )
physics.addBody( wallLeft, "static", { friction=0.5 } )
local wallRight = display.newRect( 310, -50, 10, 530 )
physics.addBody( wallRight, "static", { friction=0.5 } )
local platform1 = display.newRect( 0, 50, 200, 10 )
physics.addBody( platform1, "static", { friction=0.5 } )
platform1.rotation = 10
platform1:addEventListener( "touch", dragBody )
platform1.isPlatform = true
local platform2 = display.newRect( 120, 150, 200, 10 )
physics.addBody( platform2, "static", { friction=0.5 } )
platform2.rotation = -10
platform2:addEventListener( "touch", dragBody )
platform2.isPlatform = true
local platform3 = display.newRect( 0, 250, 200, 10 )
physics.addBody( platform3, "static", { friction=0.5 } )
platform3.rotation = 10
platform3:addEventListener( "touch", dragBody )
platform3.isPlatform = true
-- create table for all particles
local balls = {}
-- remove particles that are out of bound
local function ifOutOfBounds (event)
for i,val in pairs(balls) do
if(val.y \> 480) or (val.y \< -50) then
val:removeSelf();
balls[i] = nil;
end
end
end
local randomBall = function()
-- create ball
local ball
ball = display.newRect( 160, -20, 1, 1 )
ball.x = 160; ball.y = -20
physics.addBody( ball, { density=1, friction=0.6, bounce=0.1, radius=1 } )
ball.angularVelocity = math.random(800) - 400
-- add ball to table
balls[#balls + 1] = ball
end
Runtime:addEventListener( "enterFrame", ifOutOfBounds )
timer.performWithDelay( 10, randomBall, -1 )
cheers
-finefin
[import]uid: 70635 topic_id: 14839 reply_id: 54827[/import]