Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm currently working in an Angry Bird-esque game, where the user flings a bomb at buildings and the bomb explodes.

How do I create explosions in Corona? Essentially some sort of area of effect explosion should occur on collision, which pushes all the elements within the area out of the way (this could probably be done with applyForce)

I've tried the following, but left me with lackluster results:

function setExplosion(self, event)
    event.other:applyForce(300, 300, circle.x + (circle.width/2), circle.y + (circle.height/2))
end

Note that the circle is the explosion radius.

Anybody have any tips on creating explosions?

EDIT:

function setExplosion(self, event)
    event.other:applyForce(500, 500, circle.x + (circle.width/2), circle.y + (circle.height/2))
end



function drawExplosion(eventX, eventY)
    return function()
        circle = display.newCircle( eventX +20, eventY +20, 150 )  
            circle.myName = "circle"  
            game:insert(circle)
       circle:setFillColor(100,100,100, 100)  
     physics.addBody( circle, "static", {isSensor = true} )  
        circle.collision = setExplosion  
        circle:addEventListener( "collision", circle ) 
end
end
share|improve this question

closed as too localized by Anko, msell, bummzack, Byte56, Josh Petrie May 10 '13 at 19:11

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

    
Could you please provide some description (or better yet, a YouTube clip or something) describing what is happening that is undesirable? My assumption is that you are not setting the explosion force reference to the proper location, or not scaling down the force the further out you go, but I don't know what is happening that you don't like. –  Shotgun Ninja Mar 29 '13 at 16:20
    
On second thought, are you sure you're using the proper arguments to applyForce? According to docs.coronalabs.com/api/type/Body/applyForce.html, the first two arguments should be xForce and yForce, which you are setting to 300, 300. This means the body will experience a force pointing +300 X and +300 Y, or in the same direction every time. –  Shotgun Ninja Mar 29 '13 at 16:23
    
I voted to close this because you're asking for "tips" on "creating explosions" which is broad and discussion oriented. You'd get better quality answers, probably, if you focused more on describing the specifics of the result you want, why your current efforts are not good enough, and asking how you can bridge that gap. –  Josh Petrie May 10 '13 at 19:13

1 Answer 1

From my limited knowledge and research, you seem to be incorrect in application of the force from the explosion. My above comments discuss this further, but put simply, you should not be applying a uniform force of {300, 300} to all bodies, unless you want to hurl them at a 45-degree angle with the same amount of force. If you want a realistic explosion, you should be applying a somewhat normalized force outwards from the center of your explosion to each body, and scale it down based on distance from the explosion's center.

The general procedure I follow for processing explosions is this:

  • Locate the area of influence, and enumerate all bodies within it. (This is the part you seem to have down already.)
  • For each body, calculate two points:

    • The "target point", which is a point (or multiple points; you can get creative here) on the body at which the force is applied.
    • The center of the explosion (which you have already done).
  • Get the direction of the explosion's force upon the body, by subtracting the center point from the target point.
  • Normalize the vector to get the core directional vector.
  • Based on the original (non-normalized) vector's magnitude, apply some distance scaling function to the normalized vector to get your resultant force vector.

    • One possible way would be to multiply the normalized vector by some large "explosion power" value, then divide it by the squared magnitude of the original vector to scale it down.
  • Use the resultant force vector's X and Y components as the first two arguments to applyForce, and the target point's X and Y components as the last two.
share|improve this answer
    
I had an inkling that this might be the case. I'm not quite sure how I'm supposed to determine where the objects are, related from each other. I've added some more information to the first post, showing what I have so far. –  Cleverbird Apr 2 '13 at 7:15

Not the answer you're looking for? Browse other questions tagged or ask your own question.