-SOLVED- Paddle Passing Through Wall?

I recently finished making my first app by following a YouTube tutorial, but one thing bothered me. I want my paddle in my game to collide with the walls and stop moving, instead of passing through it like it currently does.

How could I do this?

main.lau
[lua]require(“physics”)

function main()
setUpPhysics()
createWalls()
createBricks()
createBall()
createPaddle()
startGame()
end

function setUpPhysics()
physics.start()
physics.setGravity(0,0)
end

function createWalls()
local wallThickness = 15
– Left Wall
local wall = display.newRect(0 , 0, wallThickness, display.contentHeight)
physics.addBody(wall, “static”, {friction = 0, bounce = 1})
wall:setFillColor(255, 255, 255)
– Right Wall
wall = display.newRect(display.contentWidth - wallThickness , 0, wallThickness, display.contentHeight)
physics.addBody(wall, “static”, {friction = 0, bounce = 1})
wall:setFillColor(255, 255, 255)
– Top Wall
wall = display.newRect(0 , 0, display.contentWidth, wallThickness)
physics.addBody(wall, “static”, {friction = 0, bounce = 1})
wall:setFillColor(255, 255, 255)
– Bottom Wall
wall = display.newRect(0 , display.contentHeight - wallThickness, display.contentWidth, wallThickness)
physics.addBody(wall, “static”, {friction = 0, bounce = 1})
wall:setFillColor(255, 255, 255)
wall.type = “Bottom”
end

function createBricks()
local brickWidth = 60
local brickHeight = 30
local numRows = 4
local numColumns = 6
local topLeft = {x = display.contentWidth / 2 - (brickWidth * numColumns) / 2, y = display.contentWidth / 8}
local row
local column

for row = 0, numRows - 1 do
for column = 0, numColumns - 1 do
local brick = display.newRect(topLeft.x + (column * brickWidth), topLeft.y + (row * brickHeight), brickWidth, brickHeight)
brick:setFillColor(math.random(50, 255), math.random(50, 255), math.random(50, 255))
brick.type = “Destructible”
physics.addBody(brick, “static”, {friction = 0, bounce = 1})
end
end
end

function createBall()
local ballRadius = 15
ball = display.newCircle(display.contentWidth / 2, display.contentHeight / 2, ballRadius)
ball:setFillColor(255, 255, 255)
physics.addBody(ball, “dynamic”, {friction = 0, bounce = 1, radius = ballRadius})
ball.collision = function(self, event)
if(event.phase == “ended”) then
if(event.other.type == “Destructible”) then
event.other:removeSelf()
end
if(event.other.type == “Bottom”) then
self:removeSelf()
local onTimerComplete = function(event)
createBall()
startGame()
end
timer.performWithDelay(0, onTimerComplete)
end
end
end
ball:addEventListener(“collision”, ball)
end

function createPaddle()
local paddleWidth = 200
local paddleHeight = 15
local paddle = display.newRect(display.contentWidth / 2 - paddleWidth / 2, display.contentHeight - display.contentHeight / 9.6, paddleWidth, paddleHeight)
physics.addBody(paddle, “static”, {friction = 0, bounce = 1})
local movePaddle = function(event)
paddle.x = event.x
end
Runtime:addEventListener(“touch”, movePaddle)
end

function startGame()
ball:setLinearVelocity(math.random(-150, 150), display.contentWidth / 1.25)
end

display.setStatusBar(display.HiddenStatusBar)
main()[/lua] [import]uid: 103624 topic_id: 17641 reply_id: 317641[/import]

You place the paddle in a non physics way by fixed position - maybe this overrides the collision. Try to use a physically way: move the paddle something like the puck in the multi puck example or better try to follow the instruction for touch joints:

http://developer.anscamobile.com/content/game-edition-physics-joints#Touch_joint

[import]uid: 70114 topic_id: 17641 reply_id: 67131[/import]

To avoid strange behavior you can “cut” the touch joint if the user touches out of your boundaries [import]uid: 70114 topic_id: 17641 reply_id: 67133[/import]

What else do you guys suggest? Is it possible to add a type and have the paddle not go through that type? [import]uid: 103624 topic_id: 17641 reply_id: 67150[/import]

I tried to do what is said above, but I didn’t have any luck :confused: [import]uid: 103624 topic_id: 17641 reply_id: 67210[/import]

Very basic and cobbled together, but try something like this;

[lua]require(“physics”)

function main()
setUpPhysics()
createWalls()
createBricks()
createBall()
createPaddle()
startGame()
end

function setUpPhysics()
physics.start()
physics.setGravity(0,0)
end

function createWalls()
local wallThickness = 15
– Left Wall
local wall = display.newRect(0 , 0, wallThickness, display.contentHeight)
physics.addBody(wall, “static”, {friction = 0, bounce = 1})
wall:setFillColor(255, 255, 255)
– Right Wall
wall = display.newRect(display.contentWidth - wallThickness , 0, wallThickness, display.contentHeight)
physics.addBody(wall, “static”, {friction = 0, bounce = 1})
wall:setFillColor(255, 255, 255)
– Top Wall
wall = display.newRect(0 , 0, display.contentWidth, wallThickness)
physics.addBody(wall, “static”, {friction = 0, bounce = 1})
wall:setFillColor(255, 255, 255)
– Bottom Wall
wall = display.newRect(0 , display.contentHeight - wallThickness, display.contentWidth, wallThickness)
physics.addBody(wall, “static”, {friction = 0, bounce = 1})
wall:setFillColor(255, 255, 255)
wall.type = “Bottom”
end

function createBricks()
local brickWidth = 60
local brickHeight = 30
local numRows = 4
local numColumns = 6
local topLeft = {x = display.contentWidth / 2 - (brickWidth * numColumns) / 2, y = display.contentWidth / 8}
local row
local column

for row = 0, numRows - 1 do
for column = 0, numColumns - 1 do
local brick = display.newRect(topLeft.x + (column * brickWidth), topLeft.y + (row * brickHeight), brickWidth, brickHeight)
brick:setFillColor(math.random(50, 255), math.random(50, 255), math.random(50, 255))
brick.type = “Destructible”
physics.addBody(brick, “static”, {friction = 0, bounce = 1})
end
end
end

function createBall()
local ballRadius = 15
ball = display.newCircle(display.contentWidth / 2, display.contentHeight / 2, ballRadius)
ball:setFillColor(255, 255, 255)
physics.addBody(ball, “dynamic”, {friction = 0, bounce = 1, radius = ballRadius})
ball.collision = function(self, event)
if(event.phase == “ended”) then
if(event.other.type == “Destructible”) then
event.other:removeSelf()
end
if(event.other.type == “Bottom”) then
self:removeSelf()
local onTimerComplete = function(event)
createBall()
startGame()
end
timer.performWithDelay(0, onTimerComplete)
end
end
end
ball:addEventListener(“collision”, ball)
end

function createPaddle()
local paddleWidth = 200
local paddleHeight = 15
local paddle = display.newRect(display.contentWidth / 2 - paddleWidth / 2, display.contentHeight - display.contentHeight / 9.6, paddleWidth, paddleHeight)
physics.addBody(paddle, “static”, {friction = 0, bounce = 1})
local movePaddle = function(event)
paddle.x = event.x
if paddle.x > 240 then paddle.x = 240
elseif paddle.x < 100 then paddle.x = 100
end
end
Runtime:addEventListener(“touch”, movePaddle)
end

function startGame()
ball:setLinearVelocity(math.random(-150, 150), display.contentWidth / 1.25)
end

display.setStatusBar(display.HiddenStatusBar)
main()[/lua] [import]uid: 52491 topic_id: 17641 reply_id: 67244[/import]

That worked perfectly after I changed some numbers. Thank you very much!

I have a lua question about the code, though.
[lua] local movePaddle = function(event)
paddle.x = event.x
if paddle.x > 365 then paddle.x = 365
elseif paddle.x < 115 then paddle.x = 115
end
end[/lua]
It wouldn’t let me put an end after the first if and then one after the second elseif. So, is that elseif inside the if? If it was, then I don’t think that it would ever be called correctly. So, my question is, why can’t I put an end after each like this?
[lua] local movePaddle = function(event)
paddle.x = event.x
if paddle.x > 365 then paddle.x = 365 end
elseif paddle.x < 115 then paddle.x = 115 end
end[/lua]
[import]uid: 103624 topic_id: 17641 reply_id: 67282[/import]

The If… Then should only go to the end of the line unless you enclose code in brackets.
The END after Elseif should conclude the IF statement. [import]uid: 10389 topic_id: 17641 reply_id: 67354[/import]

The if statement to the end of the line makes sense. For the elseif, though, would I not even need that end below it, assuming it follows the same rules as below? [import]uid: 103624 topic_id: 17641 reply_id: 67355[/import]

There’s one END for FUNCTION and one for IF. [import]uid: 10389 topic_id: 17641 reply_id: 67357[/import]