I need to add static walls so that bodies couldn’t get out through the sides of the screen.
[import]uid: 46567 topic_id: 10365 reply_id: 310365[/import]
What about putting 4 long rectangles with static bodies around the 4 edges of the screen? [import]uid: 31262 topic_id: 10365 reply_id: 37829[/import]
What aaaron said is correct; just make sure the rectangles are as thin as can be - or if not, position them just off the edge of the screen so that objects don’t appear to be bouncing off nothing.
Peach
[import]uid: 52491 topic_id: 10365 reply_id: 37906[/import]
Take this code and paste it into a new file and name it physicsBorders.lua
Portrait;
module(..., package.seeall)
function makeBorders()
local borderGroup = display.newGroup()
-- change these values to fit your needs.
borderBodyElement = { density = 1.0, friction = 0.5, bounce = 0.2 }
-- (x, y, width, height)
local borderTop = display.newRect( 0, 0, display.contentWidth, 0 )
borderTop:setFillColor( 0, 0, 0)
physics.addBody( borderTop, "static", borderBodyElement )
-- (x, y, width, height)
local borderBottom = display.newRect( 0, display.contentHeight, display.contentWidth, 0 )
borderBottom:setFillColor( 0, 0, 0)
physics.addBody( borderBottom, "static", borderBodyElement )
-- (x, y, width, height)
local borderLeft = display.newRect( 0, 0, 0, display.contentHeight )
borderLeft:setFillColor( 0, 0, 0)
physics.addBody( borderLeft, "static", borderBodyElement )
-- (x, y, width, height)
local borderRight = display.newRect( display.contentWidth, 0, 0, display.contentHeight )
borderRight:setFillColor( 0, 0, 0)
physics.addBody( borderRight, "static", borderBodyElement )
borderGroup:insert(borderTop);
borderGroup:insert(borderBottom);
borderGroup:insert(borderLeft);
borderGroup:insert(borderRight);
borderGroup.isVisible = false;
return borderGroup
end
The in your game where you want the borders;
require("physicsBorders")
local walls = physicsBorders.makeBorders()
[import]uid: 13560 topic_id: 10365 reply_id: 37911[/import]
lano78’s code doesn’t work for me. This is what terminal window says to me:
[blockcode]
Runtime error
…o\Programming Projects\devBalloon\physicsBorders.lua:21: ERROR:
table expected. If this is a function call, you might have used ‘.’ instead of
‘:’ stack traceback:
[C]: ?
[C]: in function ‘insert’
…o\Programming Projects\devBalloon\Runtime error:
…o\Programming Projects\devBalloon\physicsBorders.lua:21: ERROR:
table expected. If this is a function call, you might have used ‘.’ instead of
‘:’ stack traceback:
[C]: ?
[C]: in function ‘insert’
…o\Programming Projects\devBalloon\physicsBorders.
[/blockcode]
Aaaron’s and Peach Pellen’s suggestion works for me, but other objects (balloons) can’t pass walls normally. Because there are a lot of balloon at once on the screen and one balloon stuck between wall and another balloon.
I think the problem might be that my balloons are square-formed. I mean they aren’t, but when I turn from normal view into physics debugging view, it looks like square-formed.
What can you suggest? Should I take care of making my balloons circle-formed or there’s another problem? [import]uid: 46567 topic_id: 10365 reply_id: 38289[/import]
Maybe try lowering the friction on your walls? [import]uid: 31262 topic_id: 10365 reply_id: 38294[/import]
I have no problem with the code, it works for me.
Did you insert the walls into the group where you have the balloons?
There is a post on the forum where a user mentioned that physics bodies that need to interact must be in the same group.
Did you define what radius the balloons have inside the physics properties? [import]uid: 13560 topic_id: 10365 reply_id: 38304[/import]
aaaron > friction of balloons and walls are zero
labo78 > I tried to set radius and the situation go better, but it still doesn’t look perfectly, visually it looks like lagging a bit or something.
But I haven’t inserted walls and balloons into the same group, so I guess it might be the problem.
P.S. I just looked to the code that you have posted here and I see my mistake now
so I’ll fix my code and then I’ll post here if everything gone right. [import]uid: 46567 topic_id: 10365 reply_id: 38382[/import]
Do you use Physics Editor?
It’s awesome and makes defining physics shapes easy as it could be, I highly recommend it.
Texture Packer is also really good. [import]uid: 13560 topic_id: 10365 reply_id: 38398[/import]
Hi lano78, thanks for suggestions, I’ll probably need these tools in the future. Yet I haven’t used neither Physics Editor nor Texture Packer.
I fixed my mistakes in the code and it works perfectly now, so thanks a lot. [import]uid: 46567 topic_id: 10365 reply_id: 38607[/import]