Corona SDK: Jumping on moving platforms

Hi everyone,

I’ve just started using Corona SDK, and I’m having some problems applying physics on moving platforms.

Basically I have a platforms moving right to left, and when a object lands on the platform there is no friction, the object fails to move with the platform, so once the platform has moved from underneath the object, the object falls to the bottom of the screen. Has anybody else had this problem? I guess maybe a physics object loses some of its attributes but i don’t know which one.

I want that the object can move with the platform, same direction and velocity.

Here’s a code sample:

..... elements = display.newGroup() elements.anchorChildren = true elements.anchorX = 0 elements.anchorY = 1 elements.x = 0 elements.y = 0 screenGroup:insert(elements) player = display.newImageRect("player.png",30,50) player.anchorX = 50 player.anchorY = 50 player.x = 80 player.y = display.viewableContentHeight - 80 physics.addBody(player, "static", {density=.1, bounce=0.1, friction=1.0}) player:setLinearVelocity( 100, -600 ) screenGroup:insert(player) ..... .... local gameStarted = false function jumptoplatform(event) if event.phase == "began" then if gameStarted == false then player.bodyType = "dynamic" addplatformsTimer = timer.performWithDelay(1000, addplatforms, -1) moveplatformsTimer = timer.performWithDelay(2, moveplatforms, -1) gameStarted = true player:setLinearVelocity( 100, -600 ) else player:setLinearVelocity( 100, -600 ) end end end function moveplatforms() for a = elements.numChildren,1,-1 do if(elements[a].x \> -150) then elements[a].x = elements[a].x - 6 else elements:remove(elements[a]) end end end function addplatforms() platform1 = display.newImageRect("platform.png",200,80) platform1.anchorX = 0 platform1.anchorY = 1 platform1.x = 450 platform1.y = yPosition() physics.addBody(platform1, "static", {density=1, bounce=0.1, friction=1.0}) elements:insert(platform1) end .....

Maybe i have to add an onCollision function, to handle it?, or use joints? Any idea will be appreciated…

Thanks in advance

I don’t know if this might cause the problem, but you can’t change the bodyType of an object after it’s been created. Setting the player from “static” to “dynamic” doesn’t do anything in your code above, and the player object is going to remain as a “static” body.

Hi @renancar1090,

You’re thinking in the right direction. If you want an instant “player moves with platform”, then you should use a collision listener and, upon collision, set the player’s linear velocity to the same velocity as the platform (even if you have friction set to the maximum, it won’t be enough to create that instant velocity match). Alternatively, you could use a weld joint, but the first approach is probably easier.

Take care,

Brent

You can change the body type after creation ;}

…well maybe I should shut my mouth. 

I could have sworn that threw problems for me, but I haven’t tried it in a while, and the issues I experienced may have been related to scaling the physics bodies up and down.

Thanks for the catch G.I.!

Np :slight_smile: sometimes you need to use a little delay, depends where and when you use it

You’re both on the right track. :slight_smile: You can, in fact, change the body type after creation, but you can’t do it in the same time step as a collision event.

I’ve added a complete list of what you can and cannot do (on collision) in this guide:

http://docs.coronalabs.com/guide/physics/collisionDetection/index.html

Brent

I don’t know if this might cause the problem, but you can’t change the bodyType of an object after it’s been created. Setting the player from “static” to “dynamic” doesn’t do anything in your code above, and the player object is going to remain as a “static” body.

Hi @renancar1090,

You’re thinking in the right direction. If you want an instant “player moves with platform”, then you should use a collision listener and, upon collision, set the player’s linear velocity to the same velocity as the platform (even if you have friction set to the maximum, it won’t be enough to create that instant velocity match). Alternatively, you could use a weld joint, but the first approach is probably easier.

Take care,

Brent

You can change the body type after creation ;}

…well maybe I should shut my mouth. 

I could have sworn that threw problems for me, but I haven’t tried it in a while, and the issues I experienced may have been related to scaling the physics bodies up and down.

Thanks for the catch G.I.!

Np :slight_smile: sometimes you need to use a little delay, depends where and when you use it

You’re both on the right track. :slight_smile: You can, in fact, change the body type after creation, but you can’t do it in the same time step as a collision event.

I’ve added a complete list of what you can and cannot do (on collision) in this guide:

http://docs.coronalabs.com/guide/physics/collisionDetection/index.html

Brent