How to add boundaries and avoid objects going off screen

Hello, I’m new to this forum. Hopefully someone will help me with this issue I’ve been having.
I’m creating a game where multiple objects move in random ways inside the screen. I need to avoid this objects going off screen. I’ve been trying to create walls close to the screen edges:

local leftWall = display.newLine (0, 480, 320, 1);
local rightWall = display.newLine (0, -1, 320, 1);
local ceiling = display.newLine (-1, 0, 1, 480);
local bottom = display.newLine (320, 0, 1, 480);
physics.addBody( leftWall, “static”, { density=1.0, friction=0.3, bounce=0.2 } )
physics.addBody( rightWall, “static”, {density=1.0, friction=0.3, bounce=0.2 } )
physics.addBody( ceiling, “static”, { density=1.0, friction=0.3, bounce=0.2 } )
physics.addBody( bottom, “static”, { density=1.0, friction=0.3, bounce=0.2 } )

Someone suggested me to use newLine instead of newRect since it’s easier to manage apparently… but it’s not working either way. With this particular version i only get 3 lines that basically divide the screen in half. I don’t really know why.

Does someone know any way to avoid my objects going off screen using some kind of boundaries or even functions?
I’m new at programming so please suggest possible solutions in the easiest way possible.

Thank you in advance!

Hi @anon77699500,

Try

    local DECLARED_SCREEN_WIDTH = display.contentWidth
	local DECLARED_SCREEN_WIDTH = display.contentHeight
	local TOTAL_SCREEN_WIDTH	= display.actualContentWidth
	local TOTAL_SCREEN_HEIGHT   = display.actualContentHeight
	local CENTER_X 		      	= display.contentCenterX
	local CENTER_Y		      	= display.contentCenterY
	local UNUSED_WIDTH	      	= display.actualContentWidth - display.contentWidth
	local UNUSED_HEIGHT	      	= display.actualContentHeight - display.contentHeight
	local LEFT		          	= display.contentCenterX - display.actualContentWidth * 0.5
	local TOP	                = display.contentCenterY - display.actualContentHeight * 0.5
	local RIGHT 		        = display.contentCenterX + display.actualContentWidth * 0.5
	local BOTTOM 		        = display.contentCenterY + display.actualContentHeight * 0.5

	local leftWall  = display.newLine ( LEFT, TOP, LEFT, BOTTOM )
	local rightWall = display.newLine ( RIGHT, TOP, RIGHT, BOTTOM )
	local ceiling = display.newLine ( LEFT, TOP, RIGHT, TOP )
	local bottom = display.newLine ( LEFT, BOTTOM, RIGHT, BOTTOM )	

	physics.addBody( leftWall, 'static', { density=1.0, friction=0.3, bounce=0.2 } )
	physics.addBody( rightWall, 'static', {density=1.0, friction=0.3, bounce=0.2 } )
	physics.addBody( ceiling, 'static', { density=1.0, friction=0.3, bounce=0.2 } )
	physics.addBody( bottom, 'static', { density=1.0, friction=0.3, bounce=0.2 } )

	local rect = display.newRect( sceneGroup, 100, 100, 50 ,50 )
	physics.addBody( rect, 'dynamic', { density=1.0, friction=0.3, bounce=0.2 } )
	rect:setLinearVelocity( 350, 0 )

Have a nice day:)
ldurniat

2 Likes

Thanks for your help! I tried it and it seems to display what i wanted but it gives me this error:

ERROR: Runtime error main.lua:70: bad argument #1 to ‘newRect’ (number expected, got nil) stack traceback: [C]: in function ‘newRect’ main.lua:70: in main chunk

Apparently the error is in this line:

local rect = display.newRect( sceneGroup, 100, 100, 50 ,50 )

It’s not the first time I get this error with “newRect”. Is it a bug?

Do you have a display group called sceneGroup?

No! Should I add it? How do I do it?

Is this happening in main.lua or no?

Yes, It’s happening in main.lua

SceneGroup is a group, in which you should insert your objects, so composer library can control your objects (composer is for scene management).

In a composer scene you would make it like that:
local sceneGroup = self.view

But, if you are in main.lua you can just create a normal group:
local sceneGroup = display.newGroup()
And when you create your object’s 1st parameter is parent(group):
local rect = display.newRect( sceneGroup, 100, 100, 50 ,50 )

More on that:

1 Like

Thank you! Very useful, I’ll check it out :slight_smile:

Just a quick heads up.

@ldurniat’s code looks like a great place to start, but I’d recommend replacing the lines with rectangles (which you may already be doing).

The problem with giving lines a physics body is that the bodies aren’t technically even 1 pixel thick. This means that a fast moving object, or a small enough object, may pass through the lines before the physics bodies wake up and react to the supposed collision.

Having thicker rects will make it more unlikely for objects to pass through other physics bodies unintentionally.

3 Likes

Thanks for the advice! :slight_smile: