(Physics) Sprite animation doesn't fall down

Hi, I have written the code below to create an animation and a line that shifts from the right to the left. My problem is that if the line is no longer under the sprite, the sprite doesn’t fall down.

local sheet1 = graphics.newImageSheet( "hero.png", { width=135, height=259, numFrames=14 } ) local hero = display.newSprite( sheet1, { name="hero", start=1, count=14, time=450 } ) hero.timeScale = 1.0 hero.x = \_W/2-400 hero.y = \_H/2+180 hero.alpha = 1 hero.myName = "hero" hero:play() physics.addBody( hero, "dynamic", { friction=0.0, bounce=0 } ) hero.isFixedRotation=true local linea = display.newRect(\_H/2,math.random(\_H/2+290,\_H/2+400),1250,10) linea:setFillColor(255,0,0) linea.alpha = 0.5 linea.myName = "linea" physics.addBody(linea, "static", { friction = 0, bounce = 0 } ) local function muoviLinea() linea.x = linea.x - velocita linea2.x = linea2.x - velocita if(linea.x\<-712)then linea.x = \_W/2 linea.y = math.random(\_H/2+290,\_H/2+400) end end Runtime:addEventListener("enterFrame", muoviLinea)

If, instead of a sprite, I use

local hero = display.newRect(100,100,10,10) physics.addBody( hero, "dynamic", { friction=0.0, bounce=0 } )&nbsp;

Everything works as it should.

 

What am I doing wrong? Thank you.

Could you post the entire code? I was unable to simulate here, some variables missing… I think you posted twice the same code.

Yeah sorry! I’ve posted it twice!

Hi @estiennelorenzo,

I don’t see anything obviously wrong with your code. I use sprites with physics all the time, and they behave no differently than other objects.

Have you tried turning on “hybrid” physics draw mode to inspect what is happening with these objects?

http://docs.coronalabs.com/api/library/physics/setDrawMode.html

Thanks,

Brent

Hi Brent, thanks for your reply.

in the following picture you can see the result of my code. I’ve turned physics draw mode to “debug”.

dx1ron.png

In your actual project, how many second elapse before the platform moves out from under the hero? It’s possible that your sprite hero has gone to “sleep” because it sits in place for several seconds and, by default, Box2D allows objects to sleep after a few seconds of inactivity. So then, when the platform moves out from under, the object is still asleep and will not respond.

Does this seem to be what is occurring?

Brent

I thought that objects in the physics world only react to things that happen in the physics engine. Is it possible to manually move the line and expect the sprite to fall?

Hi @Vince,

It depends on whether the object is asleep or not. If it is asleep, and you simply move the platform from underneath it by non-physical means, it will likely remain asleep (what I suspect is occurring in the OP’s case).

If you want the object to fall, you can either prevent it from ever sleeping:

http://docs.coronalabs.com/api/type/Body/isSleepingAllowed.html

Or, when the platform moves (and you know that it will), you can forcibly wake up the object:

http://docs.coronalabs.com/api/type/Body/isAwake.html

Best regards,

Brent

@Brent, I see. Thanks for clarifying!

Hi Brent. A very short time elapses bebtween collision and the ending of the line. If i declare a rect object, place it on my sprite, and then with a Runtime function I set the sprite coordinates equal to the rect object (which falls down as it should), it all works great.

Hi @estiennelorenzo,

Based on the “hybrid” view color of the object in your screenshot (dark grey), this indicates that the object is asleep, so it won’t drop when the platform moves out from under it. Can you try to set its “isSleepingAllowed” property to “false” and report if it helps?

http://docs.coronalabs.com/api/type/Body/isSleepingAllowed.html

Thanks,

Brent

Hi @Brent, yes it worked! Thank you so much!

Could you post the entire code? I was unable to simulate here, some variables missing… I think you posted twice the same code.

Yeah sorry! I’ve posted it twice!

Hi @estiennelorenzo,

I don’t see anything obviously wrong with your code. I use sprites with physics all the time, and they behave no differently than other objects.

Have you tried turning on “hybrid” physics draw mode to inspect what is happening with these objects?

http://docs.coronalabs.com/api/library/physics/setDrawMode.html

Thanks,

Brent

Hi Brent, thanks for your reply.

in the following picture you can see the result of my code. I’ve turned physics draw mode to “debug”.

dx1ron.png

In your actual project, how many second elapse before the platform moves out from under the hero? It’s possible that your sprite hero has gone to “sleep” because it sits in place for several seconds and, by default, Box2D allows objects to sleep after a few seconds of inactivity. So then, when the platform moves out from under, the object is still asleep and will not respond.

Does this seem to be what is occurring?

Brent

I thought that objects in the physics world only react to things that happen in the physics engine. Is it possible to manually move the line and expect the sprite to fall?

Hi @Vince,

It depends on whether the object is asleep or not. If it is asleep, and you simply move the platform from underneath it by non-physical means, it will likely remain asleep (what I suspect is occurring in the OP’s case).

If you want the object to fall, you can either prevent it from ever sleeping:

http://docs.coronalabs.com/api/type/Body/isSleepingAllowed.html

Or, when the platform moves (and you know that it will), you can forcibly wake up the object:

http://docs.coronalabs.com/api/type/Body/isAwake.html

Best regards,

Brent

@Brent, I see. Thanks for clarifying!