Ball passes through instead of bouncing

I’m trying to create 4 walls with a ball that bounces around inside the walls. When I use the code below, the ball passes through a wall instead of a third bounce. I thought maybe the walls were sleeping but even with physics.start(true) the ball falls through.

I hope I am doing something wrong.

[code]
local windowWidth = display.contentWidth
local windowHeight = display.contentHeight

local physics = require “physics”
physics.start()
physics.setScale(30)
physics.setGravity(0, 0)
physics.setDrawMode(“normal”) – debug, hybrid

local wallProperties = { friction = 0.0, bounce = 0.5}

local leftWall = display.newLine(50,0,50,windowHeight)
leftWall:setColor(0,255,0)
leftWall.width = 20
physics.addBody(leftWall, “static”, wallProperties)

local rightWall = display.newLine(windowWidth-50,0,windowWidth-50,windowHeight)
rightWall:setColor(0,255,0)
rightWall.width = 20
physics.addBody(rightWall, “static”, wallProperties)

local upWall = display.newLine(0,0,windowWidth,0)
upWall:setColor(0,255,0)
upWall.width = 20
physics.addBody(upWall, “static”, wallProperties)

local downWall = display.newLine(0,windowHeight,windowWidth,windowHeight)
downWall:setColor(0,255,0)
downWall.width = 20
physics.addBody(downWall, “static”, wallProperties)

local ballShape = {-5, -5, 5, -5, 5, 5, -5, 5}
local ball = display.newImage(“ball.png”)
ball.x = 160
ball.y = 350
physics.addBody(ball, { density = 3.0, friction = 0.0, bounce = 0.3, shape = ballShape})
ball:applyForce(-100,100,ball.x,ball.y)
[/code] [import]uid: 9562 topic_id: 3074 reply_id: 303074[/import]

I’m not sure if lines are supposed to work as physics bodies, but in any case they don’t. :wink: Use rectangles instead–for example, swap the the following lines in your code:

– local leftWall = display.newLine(50,0,50,windowHeight)
local leftWall = display.newRect(50,0,50,windowHeight)

You’ll have to play with the positioning since the rect’s registration point will be different than the line’s.

Tim [import]uid: 8196 topic_id: 3074 reply_id: 9015[/import]

Ok thanks Tim, I’ll switch to rect’s instead of lines.

On a side note, I can crash corona simulator if I want to. :>

The problem below is that my wall shape’s third point is invalid. I would expect an error, instead the simulator crashes and the terminal closes.

local windowWidth = display.contentWidth  
local windowHeight = display.contentHeight  
  
local physics = require "physics"  
physics.start()  
physics.setScale(30)  
physics.setGravity(0, 0)  
physics.setDrawMode("hybrid") -- normal, debug, hybrid  
  
local wallProperties = { friction = 0.0, bounce = 0.5 }  
  
local x1, y1, x2, y2, wallWidth = 50, 0, 50, windowHeight, 20  
local leftWall = display.newLine(x1, y1, x2, y2)  
leftWall:setColor(0,255,0)  
leftWall.width = wallWidth  
  
print(string.format("x1: %d, shapex1: %d",x1, x1 - wallWidth/2 - 5))  
wallProperties.shape = { -(wallWidth/2), 0,  
 wallWidth/2, 0,  
 -(wallWidth/2), 400,  
 20, 400 }  
physics.addBody(leftWall, "static", wallProperties)  

[import]uid: 9562 topic_id: 3074 reply_id: 9019[/import]

Im doing some very complex setup of collision bodies based of a 2D view of a 3D object. I can tell you any errors in defining the boundary will cause the simulator to crash. The error message briefly mentions Box2D and the whole thing quits.

Its a bit annoying [import]uid: 5354 topic_id: 3074 reply_id: 9022[/import]

Thanks for pointing this out, that is pretty annoying. I filed case #1672 in our bug base.

Tim [import]uid: 8196 topic_id: 3074 reply_id: 9070[/import]