keeping objects within screen bounds

At the moment I am just using invisible 1px rectangles to keep things on screen.
This does cause a problem as they interact with the sprites and other physics on screen.

Is there a way to keep all sprites and objects within the screen bounds without coding the bounds to each object.
I suppose a global set bounds option?

I have looked at a lot of game source etc and know there is a way, but an example of the code alone would be cool.

thanks
[import]uid: 127675 topic_id: 30295 reply_id: 330295[/import]

Hi!

Try this,

–Create global screen boundaries
local leftWall = display.newRect(0,0,1, display.contentHeight )
local rightWall = display.newRect (display.contentWidth, 0, 1, display.contentHeight)
local topWall = display.newRect (0, 0, display.contentWidth, 1)

physics.addBody (leftWall, “static”, { bounce = 0.1} )
physics.addBody (rightWall, “static”, { bounce = 0.1} )
physics.addBody (topWall, “static”, { bounce = 0.1} )

Cheers! [import]uid: 157993 topic_id: 30295 reply_id: 121343[/import]

thanks for that,
this is what I have or very similar at the moment.

Basically I have collision objects transitioning across the screen.
I have " a player", which collides with the objects.
If I have the bounds of the screen set like this, then the objects collide with it .

Ideal scenerio is this:

player cannot move out of screen bounds but objects can, I am sure I can reference the x,y, on the player to keep it so? but my knowledge is letting me down. :frowning:

[import]uid: 127675 topic_id: 30295 reply_id: 121362[/import]

I ran into a problem that I think is similar to this. For some reason when you do transitions until the transition is complete collision detection will not be picked up. I am not sure what caused this. Some collisions would pick up and some wouldn’t. [import]uid: 49863 topic_id: 30295 reply_id: 121396[/import]

I’m not sure if I understand correctly. Are you using transition.to to move physical objects ?
You shouldnt do this.
Physics engine gives you appropriate methods to move objects (apply force, setLinearVelocity etc)

bubblebobble what does it mean that player cannot move outside the screen? Do you want the player to be bounced of the wall, or just stick to it ?(I’m wondering if you realy needs player collision on the edge of the screen) [import]uid: 145499 topic_id: 30295 reply_id: 121397[/import]

hi,
thanks for the replies.

Ok, I will try better explain.

I have a bird that flys, “without” any 1px boundaries set as static objects at rwall,lwall,ceiling the bird can fly out of screen.
When I place 1px invisible rects at rwall.lwall,ceiling the bird cannot fly out of the screen.
However, there are objects that transition across the screen that the bird has to attempt a collision with; on collision the transition plays, removes itself and then an animated sprite effect plays. Similar to star burst effect.
All the collisions etc work good.
If the transition object is not interacted with it will collide with the rectangle 1px boundary and then play the sprite animation (no good).
I require a way to confine the bird to screen and only have the transitions interacting with the bird.

when I started this project(my first), I thought I would build it like I do my websites (modular), but figured a single main lua with an image folder would suffice; how wrong I was!

cheers
[import]uid: 127675 topic_id: 30295 reply_id: 121399[/import]

If you just wanna keep this player in the screen, remove this invisilbe wall and try to do sth like this (run this in function registered for enterFrame event):
But please remeber to remove objects that are out of the screen and not needed anymore.

if player.x \> display.contentWidth then player.x = display.contentWidth   
elseif player.x \< 0 then player.x = 0  
end  
-- similar thing should be done for Y axis.  
  

[import]uid: 145499 topic_id: 30295 reply_id: 121451[/import]

Hi @bubblebobble,

It sounds like you need to set up the proper collision filters, so the bird (and only the bird) react with your wall boundaries. And likewise, so that the bird reacts with everything… Walls and moving objects.

Here is a guide to help you with this:
http://developer.coronalabs.com/forum/2010/10/25/collision-filters-helper-chart

You will, of course need to clear (remove) your moving objects when they exit the screen, to prevent them piling up and chewing up memory.

NOTE: as was mentioned above, you should avoid transitions in this case and use physical methods to move your objects across the screen.

Hope this helps!
Brent [import]uid: 9747 topic_id: 30295 reply_id: 121464[/import]

yes, I have to admit, I am using a transition to move a physical body.
So I guess thats a no no, I will use physics to propel the objects; thank you for the pointers.

@brent,
yes I already downloaded your physics sheets earlier in the week.
The explanation seems easy enough to understand.

I will first change the transitions to physics objects only, then have a play with collision filters.
Always seems to be one step forward and two backwards at the moment ! :frowning:

I will now have to look at spawning my leaves (objects) and moving them using physics bodies.
I will probably (certainly) be back for some more pointers.

Rock on !
[import]uid: 127675 topic_id: 30295 reply_id: 121479[/import]

Hi!

Try this,

–Create global screen boundaries
local leftWall = display.newRect(0,0,1, display.contentHeight )
local rightWall = display.newRect (display.contentWidth, 0, 1, display.contentHeight)
local topWall = display.newRect (0, 0, display.contentWidth, 1)

physics.addBody (leftWall, “static”, { bounce = 0.1} )
physics.addBody (rightWall, “static”, { bounce = 0.1} )
physics.addBody (topWall, “static”, { bounce = 0.1} )

Cheers! [import]uid: 157993 topic_id: 30295 reply_id: 121343[/import]

thanks for that,
this is what I have or very similar at the moment.

Basically I have collision objects transitioning across the screen.
I have " a player", which collides with the objects.
If I have the bounds of the screen set like this, then the objects collide with it .

Ideal scenerio is this:

player cannot move out of screen bounds but objects can, I am sure I can reference the x,y, on the player to keep it so? but my knowledge is letting me down. :frowning:

[import]uid: 127675 topic_id: 30295 reply_id: 121362[/import]

I ran into a problem that I think is similar to this. For some reason when you do transitions until the transition is complete collision detection will not be picked up. I am not sure what caused this. Some collisions would pick up and some wouldn’t. [import]uid: 49863 topic_id: 30295 reply_id: 121396[/import]

I’m not sure if I understand correctly. Are you using transition.to to move physical objects ?
You shouldnt do this.
Physics engine gives you appropriate methods to move objects (apply force, setLinearVelocity etc)

bubblebobble what does it mean that player cannot move outside the screen? Do you want the player to be bounced of the wall, or just stick to it ?(I’m wondering if you realy needs player collision on the edge of the screen) [import]uid: 145499 topic_id: 30295 reply_id: 121397[/import]

hi,
thanks for the replies.

Ok, I will try better explain.

I have a bird that flys, “without” any 1px boundaries set as static objects at rwall,lwall,ceiling the bird can fly out of screen.
When I place 1px invisible rects at rwall.lwall,ceiling the bird cannot fly out of the screen.
However, there are objects that transition across the screen that the bird has to attempt a collision with; on collision the transition plays, removes itself and then an animated sprite effect plays. Similar to star burst effect.
All the collisions etc work good.
If the transition object is not interacted with it will collide with the rectangle 1px boundary and then play the sprite animation (no good).
I require a way to confine the bird to screen and only have the transitions interacting with the bird.

when I started this project(my first), I thought I would build it like I do my websites (modular), but figured a single main lua with an image folder would suffice; how wrong I was!

cheers
[import]uid: 127675 topic_id: 30295 reply_id: 121399[/import]

If you just wanna keep this player in the screen, remove this invisilbe wall and try to do sth like this (run this in function registered for enterFrame event):
But please remeber to remove objects that are out of the screen and not needed anymore.

if player.x \> display.contentWidth then player.x = display.contentWidth   
elseif player.x \< 0 then player.x = 0  
end  
-- similar thing should be done for Y axis.  
  

[import]uid: 145499 topic_id: 30295 reply_id: 121451[/import]

Hi @bubblebobble,

It sounds like you need to set up the proper collision filters, so the bird (and only the bird) react with your wall boundaries. And likewise, so that the bird reacts with everything… Walls and moving objects.

Here is a guide to help you with this:
http://developer.coronalabs.com/forum/2010/10/25/collision-filters-helper-chart

You will, of course need to clear (remove) your moving objects when they exit the screen, to prevent them piling up and chewing up memory.

NOTE: as was mentioned above, you should avoid transitions in this case and use physical methods to move your objects across the screen.

Hope this helps!
Brent [import]uid: 9747 topic_id: 30295 reply_id: 121464[/import]

yes, I have to admit, I am using a transition to move a physical body.
So I guess thats a no no, I will use physics to propel the objects; thank you for the pointers.

@brent,
yes I already downloaded your physics sheets earlier in the week.
The explanation seems easy enough to understand.

I will first change the transitions to physics objects only, then have a play with collision filters.
Always seems to be one step forward and two backwards at the moment ! :frowning:

I will now have to look at spawning my leaves (objects) and moving them using physics bodies.
I will probably (certainly) be back for some more pointers.

Rock on !
[import]uid: 127675 topic_id: 30295 reply_id: 121479[/import]