How to apply force from explosion...

I’m hoping this has been done before, but does someone care to share their equation for applying force to objects on a screen from a bomb? In other words if I set a bomb off in the middle of bouncing balls (with a limited amount of gravity, they should all be blown away from the explosion.

This tutorial got me to where I have it working:
http://mobile.tutsplus.com/tutorials/corona/corona-sdk_physics-engine_explosions/

but it’s not a very realistic explosion, and the force is off, as is the direction it send the balls in.

This seems more like what I need:
http://physics.stackexchange.com/questions/8788/basic-explosion-physics-determining-force

but just reading it made me cry a little bit, so I’m hoping someone has already tackled this and doesn’t mind sharing. [import]uid: 19193 topic_id: 18344 reply_id: 318344[/import]

Here is some plug and play code to test what I was working with. If you click on the screen to set off a fake bomb, you’ll see it doesn’t work:

[lua]local physics = require(“physics”)

physics.start()
physics.setDrawMode( “hybrid” ) – overlays collision outlines on normal Corona objects

physics.setScale( 30 )
physics.setGravity( 0, 0 )

local localGroup = display.newGroup()
local popCircles = {};
local background = display.newRect(0, 0, display.viewableContentWidth, display.viewableContentHeight)
background:setFillColor(255, 255, 255)
localGroup:insert(background)
local boundaryBox = display.newGroup();
borderBodyElement = { friction=.2, bounce=1 }

local borderBottom = display.newRect(localGroup, 0, (display.contentHeight -1), 768, 1 )
borderBottom:setFillColor( 255,255,255) – make invisible
physics.addBody( borderBottom, “static”, borderBodyElement )
boundaryBox:insert(borderBottom)

local borderTop = display.newRect(localGroup, 0, 0, display.contentWidth, 1 )
borderTop:setFillColor( 255,255,255) – make invisible
physics.addBody( borderTop, “static”, borderBodyElement )
boundaryBox:insert(borderTop)
local borderLeft = display.newRect(localGroup, 0, 0, 1, display.contentHeight )
borderLeft:setFillColor( 255,255,255) – make invisible
physics.addBody( borderLeft, “static”, borderBodyElement )
boundaryBox:insert(borderLeft)

local borderRight = display.newRect(localGroup, (display.contentWidth -1), 0, 1, display.contentHeight )
borderRight:setFillColor( 255,255,255) – make invisible
physics.addBody( borderRight, “static”, borderBodyElement )
boundaryBox:insert(borderRight)
localGroup:insert(boundaryBox)

–loop over balls.
for i=1,10 do
local group = display.newGroup()
local redBody = { density=0.3, friction=.1, bounce=.6, radius=50.0}

popCircles[i] = display.newCircle( 0, 0, 50 )
popCircles[i]:setFillColor(255,255,255)

popCircles[i].id = i

popCircles[i]:setReferencePoint(display.CenterReferencePoint)

group:insert(popCircles[i])

local label = display.newText(group,i,0,0,native.systemFontBold,24)
label:setTextColor(0,0,0)
label.x = popCircles[i].x
label.y = popCircles[i].y
label:toFront();

group:setReferencePoint(display.CenterReferencePoint)
group.x = math.random(0,760) ;
group.y = math.random(0,1024) ;
group.id = i
physics.addBody( group, redBody )
group.isFixedRotation = true
group.myName = “bouncingNumber”;

localGroup:insert(group)

end

local function onLocalCollision( self, event )
if ( event.phase == “began” and self.myName == “circle” and event.other.myName == “bouncingNumber” ) then
–local forcex = math.abs(event.other.x-self.x)
–local forcey = math.abs(event.other.y-self.y)
local pi = math.pi
local cos = math.cos
local sin = math.sin

local x = self.x
print(’----------------------------’)
print('ID: '…event.other.id)

print('X: '…x)
print('EventX: '…event.other.x)

local y = self.y
local deltaX = x - event.other.x
print('DeltaX: '…deltaX)
local deltaY = y - event.other.y
print('DeltaY: '…deltaY)
local angle = math.deg( math.atan( deltaY / deltaX ) )
print('Angle: '…angle)
local force = 10 – change this to whatever value you need
local xForce = ( sin( angle * pi / 180 ) * force )
local yForce = ( cos( angle * pi / 180 ) * force )
print('xForce: '…xForce)
print('yForce: '…yForce)

–[[
local xForce = force - (event.other.x - self.x)
local yForce = force - (event.other.y - self.y)
forcex = 100 - forcex

if(forcex < 0) then
forcex = 0-(80 + forcex) - 12
else
forcex = 80 - forcex + 12
end
–]]
event.other:applyLinearImpulse( xForce, yForce, event.other.x, event.other.y )
print('ForceX: '…xForce);
print('ForceY: '…yForce);
–[[
if(math.abs(forcex) > 180 or math.abs(forcey) > 180) then
local explosion = display.newImage( “explosion.png”, event.other.x, event.other.y )
event.other:removeSelf()
local function removeExplosion( event )
explosion:removeSelf()
end

timer.performWithDelay( 50, removeExplosion)
end
–]]
end
end

local function setBomb ( event )
if(event.phase == “began”) then

local circle = “”

circle = display.newCircle( event.x, event.y, 180 )
circle:setFillColor(255,255,255)
physics.addBody( circle, “static”, {isSensor = true} )
circle:setReferencePoint(display.CenterReferencePoint)

circle.myName = “circle”
circle.collision = onLocalCollision
circle:addEventListener( “collision”, circle )

local function removeStuff( event )
circle:removeSelf()
end
timer.performWithDelay(2100, removeStuff)
end
end
background:addEventListener(“touch”,setBomb)
[/lua] [import]uid: 19193 topic_id: 18344 reply_id: 70313[/import]