Physics Bodies & Sprites/sequences (A Realistic Spring)

I have the need for an animated graphic (using a sprite sheet) that represents a “spring”.  The spring constantly expands and contracts – the underlying sprite sheet/sequence is performed through a series of images in which the spring is expanding (getting bigger) or contracting.  I am able to add the the sprite sheet as a physics body; however, it seems to only add to the engine as the fully “expanded” image or the fully “contracted” image.  I would like for the spring to “punch” a character.  So, what I’m trying to achieve is for the physics body to actually get smaller or larger along with the changing frames within sprite sheet/animation.   I’ve gone so far as to try and add another “hidden” physic object that constant moves back and forth – but it is next to imposible to synch it with the sprite sheet.  Any thoughts or recommendations?

 

Example:

local elementIcon                  = display.newGroup()

 

local springSpriteSheet          = graphics.newImageSheet(“images/sprites/spring/spring.png”, SpringSpriteSheet:getSheet() )

local springSequence            = 

{

    { name=“punch”, frames= { 1, 2, 3, 4, 5, 6, 7, 6, 5, 4, 3, 2 }, time = 700, loopCount = 0 }, 

}

        

elementImage                    = display.newSprite(springSpriteSheet, springSequence )

elementIcon:insert(elementImage)

elementImage:play()

 

Physics.addBody( elementIcon,  

        {

            density     = (element.density or 1.0), 

            friction    = (element.friction or 1.0), 

            bounce      = (element.bounce or 1.0),

            isSensor    = (element.isSensor or false),

            shape       = (element.shape or nil),

            radius      = (element.radius or nil),

            filter      = (element.filter or nil),

            shape       = (element.shape or nil),

        } )

 

Hello @petebalsavias,

It’s a bit complex to set up initially, but you might consider creating separate bodies that match the “frames” of the spring, then use the PhysicsContact feature with a pre-collision listener to “turn off” and “turn on” the body parts that shouldn’t be sensing collision (because, by the animation, the spring is not occupying that space at the moment).

This tutorial explains the general principle behind it:

http://www.coronalabs.com/blog/2013/01/08/working-with-multi-element-physics-bodies/

Another option would be to set up a per-frame sprite listener that moves a separate physical body in accordance with the spring animation. You could move the separate body with the “next” animation frame listener described in this tutorial:

http://www.coronalabs.com/blog/2012/10/02/animated-sprites-and-methods/

Best regards,

Brent Sorrentino

Brent, option 2 worked perfectly (was easier to implement than option 1 given the state of my code).  Thanks a bunch for your help!

Hello @petebalsavias,

It’s a bit complex to set up initially, but you might consider creating separate bodies that match the “frames” of the spring, then use the PhysicsContact feature with a pre-collision listener to “turn off” and “turn on” the body parts that shouldn’t be sensing collision (because, by the animation, the spring is not occupying that space at the moment).

This tutorial explains the general principle behind it:

http://www.coronalabs.com/blog/2013/01/08/working-with-multi-element-physics-bodies/

Another option would be to set up a per-frame sprite listener that moves a separate physical body in accordance with the spring animation. You could move the separate body with the “next” animation frame listener described in this tutorial:

http://www.coronalabs.com/blog/2012/10/02/animated-sprites-and-methods/

Best regards,

Brent Sorrentino

Brent, option 2 worked perfectly (was easier to implement than option 1 given the state of my code).  Thanks a bunch for your help!

Hey @petebalsavias I have a quick question about this topic. Your solution was to change the physical body each time you get the “next” animation frame event. What I meant is you have a different physical body for each frame of your animation, and you were switching the current physical body according to the current frame, using the “next” animation frame listener?

Thanks in advance

Hey @petebalsavias I have a quick question about this topic. Your solution was to change the physical body each time you get the “next” animation frame event. What I meant is you have a different physical body for each frame of your animation, and you were switching the current physical body according to the current frame, using the “next” animation frame listener?

Thanks in advance