Keeping static physics body on edge of screen at all times while "camera" moves

I’m having trouble keeping a static physics body on the screen at all times.  I am moving my “world” display group in order to follow my player.  I add the static physics body and it works until my player reaches past the edge of the screen.  then I will go through the bottom of the screen.  I’ve tried physics debug mode to see where the collisions are happening… but I do see my bottom physics body at all times.  I’m just travelling through it for some reason.

adding bottom (my player collides with this until i travel too far to the right)  

local bottom = display.newRect( 0, screenH - 2, screenW, 2 ) bottom.anchorX, bottom.anchorY = 0,0 sceneGroup:insert( bottom ) physics.addBody( bottom, 'static' )

 moving my player to the right

function screenTouched(e) if e.phase == 'began' then player.obj:setLinearVelocity( 50, -100 ) end end

moving my world according to the players position.  my player is within the world group

function update( ) local target = -player.obj.x + 100 world.x = world.x + (target - world.x) \* 0.05 end

Hi @tameem50,

Is the static “bottom” object NOT part of the “world” group? Do you want it to remain at the bottom regardless of how much the “world” has scrolled in whatever direction?

Brent

yes exactly.  its not part of the world group.   i want it to remain at the bottom no matter how far the world has scrolled.  i can post entire project code if necessary.

OK, understood. As you may have read/learned, physics objects must reside in the same display group (or overall “world” group) if you want to properly detect collisions. If you need to move your “world” group but keep that one static object on the bottom, you should insert that object into the world group, but then whenever you move the world, just move its position in direct opposite of how you’re moving the world group. For example, if you move the world group [X,Y] by [-5,-0.2] (just an example, it could be whatever), move that bottom object by [5,0.2] (so effectively, just multiply the world movement X,Y by -1.

Alternatively, if you’re only using that bottom object as some means to stop the player from moving off the bottom of the screen (that the player sees), you could detect the player’s position independently of the world group shift and, in your movement process, conditionally keep the player from moving below a certain Y threshold.

Hope this helps,

Brent

Thanks for the answer.   Collisions are being detected up until the point I travel too far. If I debug the screen i still see that “bottom” physics object there, but after traveling right too far I eventually fall through it.  Do you know what the cause for this is?  Seems like it should either work, or not work.

Yes, basically, when you move the “world” group around, but you don’t move the “bottom” object in sync, the debug/hybrid physics views will not accurately match where Box2D mathematically regards the collision bounds of objects.

Brent

Hi @tameem50,

Is the static “bottom” object NOT part of the “world” group? Do you want it to remain at the bottom regardless of how much the “world” has scrolled in whatever direction?

Brent

yes exactly.  its not part of the world group.   i want it to remain at the bottom no matter how far the world has scrolled.  i can post entire project code if necessary.

OK, understood. As you may have read/learned, physics objects must reside in the same display group (or overall “world” group) if you want to properly detect collisions. If you need to move your “world” group but keep that one static object on the bottom, you should insert that object into the world group, but then whenever you move the world, just move its position in direct opposite of how you’re moving the world group. For example, if you move the world group [X,Y] by [-5,-0.2] (just an example, it could be whatever), move that bottom object by [5,0.2] (so effectively, just multiply the world movement X,Y by -1.

Alternatively, if you’re only using that bottom object as some means to stop the player from moving off the bottom of the screen (that the player sees), you could detect the player’s position independently of the world group shift and, in your movement process, conditionally keep the player from moving below a certain Y threshold.

Hope this helps,

Brent

Thanks for the answer.   Collisions are being detected up until the point I travel too far. If I debug the screen i still see that “bottom” physics object there, but after traveling right too far I eventually fall through it.  Do you know what the cause for this is?  Seems like it should either work, or not work.

Yes, basically, when you move the “world” group around, but you don’t move the “bottom” object in sync, the debug/hybrid physics views will not accurately match where Box2D mathematically regards the collision bounds of objects.

Brent