Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Wrap moving object to opposite side of screen.
Started by Appletreeman Aug 04 2018 12:32 PM

3 replies to this topic

Best Answer roaminggamer , 04 August 2018 - 03:15 PM

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

actions.gif
 
SSK has 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 )


[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

Appletreeman

[GLOBAL: userInfoPane.html]
Appletreeman
  • Contributor

  • 403 posts
  • Corona SDK

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?



[TOPIC: post.html]
#2

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,363 posts
  • Corona SDK

  Best Answer

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

actions.gif
 
SSK has 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 )



  • Appletreeman likes this

[TOPIC: post.html]
#3

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,363 posts
  • Corona SDK

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


  • Appletreeman likes this

[TOPIC: post.html]
#4

Appletreeman

[GLOBAL: userInfoPane.html]
Appletreeman
  • Contributor

  • 403 posts
  • Corona SDK

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

 

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.




[topic_controls]
[/topic_controls]