In Corona SDK, How do I keep a player from being dug into platforms?

I’m trying to make a small endless running game in which a player (a small box, basically) moves left and right in between platforms. I noticed that the player is dug into the platforms. I tried tweaking the density on both player physics body and platform physics body. But none of them worked. Is this related to anchorX,anchorY by any chance?

=================================================================================

Code

=================================================================================

local physics = require(‘physics’)
physics.start()

player = display.newImageRect(‘boxy.png’,30,30)
player.anchorX = 0
player.anchorY = 0
player.x = 200
physics.addBody(player,‘dynamic’, {density = 1, friction = 0, bounce = 0})

platformLeft = display.newRect(100,300,450,30)
platformLeft:setFillColor(0,0,0)
platformLeft.anchorX = 1
platformLeft.anchorY = 0.5
platformLeft.x = (width - 1) * 30
platformLeft.y = _H + 90
physics.addBody(platformLeft,‘static’, {density = 1, friction = 0, bounce = 0})
platgroup:insert(platformLeft)

platformRight = display.newRect(100,350,450,30)
platformRight:setFillColor(0,0,0)
platformRight.anchorX = 0
platformRight.anchorY = 0.5
platformRight.x = width * 30
platformRight.y = _H + 90
physics.addBody(platformRight,‘static’, {density = 1,friction = 0, bounce = 0})
platgroup:insert(platformRight)

Hi @danpreneur,

This shouldn’t be anything related to setting an off-center anchor. Somewhere in your code (that you haven’t shown here) are you changing the x/y position of the “platgroup” group? Are you moving it or transitioning it?

Brent

Brent,

I apologize for the lack of details in my question. The best way to illustrate my situation is using a picture of the game I’m trying to build. But, since I just signed up for the Corona account, I am unable to upload any photo. The best way I can show you is through a link to a StackOverFlow site where I have asked the same questions: http://stackoverflow.com/questions/36974486/in-corona-sdk-how-do-i-keep-a-player-from-being-dug-into-platforms.

Here’s the basic idea of the game: there’s a box that a player controls to move left and right. The player has to move the box through a gap in between platforms that elevate, and if the box touches the top of the screen, it’s game over. So, to provide clarity to your confusion, yes, the y position of the ‘platgroup’ is moving while the x position remain constant.

The problem I am currently experiencing is that you don’t see the bottom half of the box when it lands on top of the platform. I assumed this could be related to density and anchorX/Y. But, I have tweaked the code to almost every variation, but the problem has yet to be fixed.

Hi @danpreneur,

How are you moving the platform group (“platgroup”) upward? I assume you’re just transitioning or moving the entire group on a Runtime update? If so, this is the cause of the issue, because I see (in your code) that you haven’t placed “player” in the same group. When it comes to collision detection, Box2D wants to have potentially colliding objects in the same group so it can properly calculate the collision math.

I have a better suggestion for designing this game, if you’d like to consider it:

  1. Make each platform set into one physics object. These would actually be 2-element physics bodies (tutorial), with each “half” (on left and right of the gap) as one element. Because you are positioning the gap differently in X position, just make each half long enough so that, no matter the position of the gap, there will always be enough platform to span the screen (and the rest just runs off the screen with no physical effect on anything).

  2. Make these platforms of “kinematic” type (not “static”).

  3. Instead of moving the platforms with transitions (assuming you are), move them with physics. Set their direct linear velocity (:setLinearVelocity()) to go upwards. However you decide to do it, keep “spawning” new platforms somewhere off the bottom of the screen, in a consistent manner so you get the “endless runner” gameplay (or in your case, the “endless faller” :P).

  4. Keep gravity on, and let the player just naturally fall through the gaps as you move the platforms up.

Hope this helps,

Brent

Hi @danpreneur,

This shouldn’t be anything related to setting an off-center anchor. Somewhere in your code (that you haven’t shown here) are you changing the x/y position of the “platgroup” group? Are you moving it or transitioning it?

Brent

Brent,

I apologize for the lack of details in my question. The best way to illustrate my situation is using a picture of the game I’m trying to build. But, since I just signed up for the Corona account, I am unable to upload any photo. The best way I can show you is through a link to a StackOverFlow site where I have asked the same questions: http://stackoverflow.com/questions/36974486/in-corona-sdk-how-do-i-keep-a-player-from-being-dug-into-platforms.

Here’s the basic idea of the game: there’s a box that a player controls to move left and right. The player has to move the box through a gap in between platforms that elevate, and if the box touches the top of the screen, it’s game over. So, to provide clarity to your confusion, yes, the y position of the ‘platgroup’ is moving while the x position remain constant.

The problem I am currently experiencing is that you don’t see the bottom half of the box when it lands on top of the platform. I assumed this could be related to density and anchorX/Y. But, I have tweaked the code to almost every variation, but the problem has yet to be fixed.

Hi @danpreneur,

How are you moving the platform group (“platgroup”) upward? I assume you’re just transitioning or moving the entire group on a Runtime update? If so, this is the cause of the issue, because I see (in your code) that you haven’t placed “player” in the same group. When it comes to collision detection, Box2D wants to have potentially colliding objects in the same group so it can properly calculate the collision math.

I have a better suggestion for designing this game, if you’d like to consider it:

  1. Make each platform set into one physics object. These would actually be 2-element physics bodies (tutorial), with each “half” (on left and right of the gap) as one element. Because you are positioning the gap differently in X position, just make each half long enough so that, no matter the position of the gap, there will always be enough platform to span the screen (and the rest just runs off the screen with no physical effect on anything).

  2. Make these platforms of “kinematic” type (not “static”).

  3. Instead of moving the platforms with transitions (assuming you are), move them with physics. Set their direct linear velocity (:setLinearVelocity()) to go upwards. However you decide to do it, keep “spawning” new platforms somewhere off the bottom of the screen, in a consistent manner so you get the “endless runner” gameplay (or in your case, the “endless faller” :P).

  4. Keep gravity on, and let the player just naturally fall through the gaps as you move the platforms up.

Hope this helps,

Brent