Wrap moving object to opposite side of screen.

Hi,

I know there’s been (much) mention around these parts that you shouldn’t directly adjust a dynamic objects x & y coordinates as it can mess up the physics engine.

So if I have an object that is moved using applyForce how do I get it to wrap around the screen?

In some cases you can’t avoid it.  Screen wrapping is one of those cases.

actions.gif
 
SSKhas code for this: https://roaminggamer.github.io/RGDocs/pages/SSK2/libraries/actions2/#scene-wrapping-scene
 
It is documented, but you can also see how it is used here:
 

  1. Download validation suite: https://github.com/roaminggamer/SSK2/raw/master/validation.zip

  2. Run example: ‘Actions Screen Wrapping Rectangle’

  3. See the code in file:  validation/tests/actions/007_rect_wrapping.lua
     
    This is the CORE of the code

    local actions = ssk.actions – Create a ‘wrapRect’ as a proxy for wrapping calculations – local wrapRect = newRect( group, centerX, centerY, { w = fullh - 100, h = fullh - 100, fill = {0,1,1,0.1}, stroke = {1,1,0,0.5}, strokeWidth = 2 } ) local player = newImageRect( group, centerX, centerY, “images/up.png”, { size = 40 }, { radius = 20 } ) player.enterFrame = function( self ) actions.scene.rectWrap( self, wrapRect ) end; listen( “enterFrame”, player )

The code itself is very short so you can just extract it and modify it as needed:

scene.rectWrap = function( objectToWrap, wrapRectangle ) local right = wrapRectangle.x + wrapRectangle.contentWidth / 2 local left = wrapRectangle.x - wrapRectangle.contentWidth / 2 local top = wrapRectangle.y - wrapRectangle.contentHeight / 2 local bot = wrapRectangle.y + wrapRectangle.contentHeight / 2 if(objectToWrap.x \>= right) then objectToWrap.x = left + objectToWrap.x - right elseif(objectToWrap.x \<= left) then objectToWrap.x = right + objectToWrap.x - left end if(objectToWrap.y \>= bot) then objectToWrap.y = top + objectToWrap.y - bot elseif(objectToWrap.y \<= top) then objectToWrap.y = bot + objectToWrap.y - top end end

Which is what I thought.  I’ve done it no end of times without any problems at all but last night I was having some really weird issues.

Anyway, after a lot of hair pulling and maybe just a little bit of swearing and vowing to throw the computer out of the window I went out for a walk.

When I got back I looked at it with fresh eyes and realised that the issue was due to another (invisible) physics object that is welded to the players ship but I hadn’t set it to isSensor=true.  Once I did that it was problem solved.  It also had the added benefit of solving a couple of other issues that I had been putting off looking into as they weren’t so important.

Happy days.

In some cases you can’t avoid it.  Screen wrapping is one of those cases.

actions.gif
 
SSKhas code for this: https://roaminggamer.github.io/RGDocs/pages/SSK2/libraries/actions2/#scene-wrapping-scene
 
It is documented, but you can also see how it is used here:
 

  1. Download validation suite: https://github.com/roaminggamer/SSK2/raw/master/validation.zip

  2. Run example: ‘Actions Screen Wrapping Rectangle’

  3. See the code in file:  validation/tests/actions/007_rect_wrapping.lua
     
    This is the CORE of the code

    local actions = ssk.actions – Create a ‘wrapRect’ as a proxy for wrapping calculations – local wrapRect = newRect( group, centerX, centerY, { w = fullh - 100, h = fullh - 100, fill = {0,1,1,0.1}, stroke = {1,1,0,0.5}, strokeWidth = 2 } ) local player = newImageRect( group, centerX, centerY, “images/up.png”, { size = 40 }, { radius = 20 } ) player.enterFrame = function( self ) actions.scene.rectWrap( self, wrapRect ) end; listen( “enterFrame”, player )

The code itself is very short so you can just extract it and modify it as needed:

scene.rectWrap = function( objectToWrap, wrapRectangle ) local right = wrapRectangle.x + wrapRectangle.contentWidth / 2 local left = wrapRectangle.x - wrapRectangle.contentWidth / 2 local top = wrapRectangle.y - wrapRectangle.contentHeight / 2 local bot = wrapRectangle.y + wrapRectangle.contentHeight / 2 if(objectToWrap.x \>= right) then objectToWrap.x = left + objectToWrap.x - right elseif(objectToWrap.x \<= left) then objectToWrap.x = right + objectToWrap.x - left end if(objectToWrap.y \>= bot) then objectToWrap.y = top + objectToWrap.y - bot elseif(objectToWrap.y \<= top) then objectToWrap.y = bot + objectToWrap.y - top end end

Which is what I thought.  I’ve done it no end of times without any problems at all but last night I was having some really weird issues.

Anyway, after a lot of hair pulling and maybe just a little bit of swearing and vowing to throw the computer out of the window I went out for a walk.

When I got back I looked at it with fresh eyes and realised that the issue was due to another (invisible) physics object that is welded to the players ship but I hadn’t set it to isSensor=true.  Once I did that it was problem solved.  It also had the added benefit of solving a couple of other issues that I had been putting off looking into as they weren’t so important.

Happy days.