Collision Help?? Pong Game

Hello everyone… I’m new to programming in general know some basic C++ and moved on to Corona using Lua as it’s much easier to start on and get results. I’m starting with a simple pong type game but just adding 1 Player for now… no AI. I’ve been researching doing my own and pasting snippets to get my game where it is now. So far I have about everything laid out and working but one thing. I can’t find a way to get any collision detection between my paddle and the ball. I’ve tried a few things and honestly don’t remember exactly what I used to try but had no success. What would i need to do to get this working? Is there a way I could make the two just use the physics I have set for them now as the force between them when they meet? Anyway here is my code let me know if you need more info… thanks.

[lua]-----------------------------------------------------------------------------------------

– main.lua


W = display.contentWidth
H = display.contentHeight
display.setStatusBar(display.HiddenStatusBar);

require(“physics”)
physics.start()
physics.setGravity(0,0)

–physics.setDrawMode(“hybrid”)

– Define Walls
local leftWall = display.newRect (0, 0, 1, H)
local rightWall = display.newRect (W - 1, 0, W, H)
local topWall = display.newRect (0, 0, W, 1)
local bottomWall = display.newRect (0, H - 1, W, H)

– Make Walls Physical
physics.addBody (leftWall, “static”, {bounce = .1})
physics.addBody (rightWall, “static”, {bounce = .1})
physics.addBody (topWall, “static”, {bounce = .1})
physics.addBody (bottomWall, “static”, {bounce = .1})

–Background Image
backImage = display.newImage (“background_image.png”);
backImage:toBack();

– Player Score
local cScore = 0
cScore = display.newText(cScore, 50, 0, nil, 14);
dScore = display.newText(“Score:”, 10, 3, nil, 12)
dScore:setTextColor(255,255,255)

– Drag Handler
local function onTouch(event)
local t = event.target
local phase = event.phase

if “began” == phase then
local parent = t.parent
parent:insert(t)
display.getCurrentStage():setFocus(t)
–t.x0 = event.x - t.x
t.y0 = event.y - t.y

elseif “moved” == phase then
–t.x = event.x - t.x0
t.y = event.y - t.y0

elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus(nil)
t.isFocus = false

end
return true
end

– Create Paddle
local paddleP1 = display.newRect(15, 125, 10, 75);
physics.addBody(paddleP1, “static”, {bounce = .5, friction = 1, density = 5})
paddleP1:addEventListener(“touch”, onTouch)

– Create Ball
–local aBall = display.newCircle(175, 190, 7.5)
–physics.addBody (aBall, {bounce = 1, friction = -1, density = 1})
local xdirection,ydirection = 5,5
local xpos,ypos = display.contentWidth*0.5,display.contentHeight*0.5
ball = display.newCircle( xpos, ypos, 10 );
ball:setFillColor(255,255,255,255);
physics.addBody (ball, {bounce = 1, friction = -1, density = 1})

local function animate(event)
xpos = xpos + ( 2.8 * xdirection );
ypos = ypos + ( 2.2 * ydirection );

if (xpos > display.contentWidth - 20 or xpos < 20) then
xdirection = xdirection * -1;
end
if (ypos > display.contentHeight - 20 or ypos < 20) then
ydirection = ydirection * -1;
end

ball:translate( xpos - ball.x, ypos - ball.y)
end

Runtime:addEventListener( “enterFrame”, animate );

[lua] [import]uid: 20272 topic_id: 34724 reply_id: 334724[/import]

Lets say you want the paddle to print a statement when the ball hits the paddle. Put this at the end of your code…

[lua]ball.class = “ball” – just a name for the ball (nothing special about ‘class’)
paddle.class = “paddle” – again, it’s just a variable

function paddle:collision(e)
if (e.phase == “began”) then
print(e.target.class,“collided with”,e.other.class)
end
return true
end
paddle:addEventListener(“collision”) – attach event listener to the paddle[/lua] [import]uid: 8271 topic_id: 34724 reply_id: 137953[/import]

Thank you… I got that to work but one more question. What would I add as my if statement to get the two to interact. So the ball bounces off of it instead of collision print and goes through it? [import]uid: 20272 topic_id: 34724 reply_id: 137954[/import]

In that case you want to take a look at the ‘preCollision’ event and the Physics Contact object. It’s fairly new for Corona and there was a recent blog post. I also mentioned it in a forum post yesterday, so you should take a look at the links I put there:

https://developer.coronalabs.com/forum/2013/01/08/how-can-i-achieve-drag-and-drop-effect#comment-137904 [import]uid: 8271 topic_id: 34724 reply_id: 138030[/import]

Lets say you want the paddle to print a statement when the ball hits the paddle. Put this at the end of your code…

[lua]ball.class = “ball” – just a name for the ball (nothing special about ‘class’)
paddle.class = “paddle” – again, it’s just a variable

function paddle:collision(e)
if (e.phase == “began”) then
print(e.target.class,“collided with”,e.other.class)
end
return true
end
paddle:addEventListener(“collision”) – attach event listener to the paddle[/lua] [import]uid: 8271 topic_id: 34724 reply_id: 137953[/import]

Thank you… I got that to work but one more question. What would I add as my if statement to get the two to interact. So the ball bounces off of it instead of collision print and goes through it? [import]uid: 20272 topic_id: 34724 reply_id: 137954[/import]

In that case you want to take a look at the ‘preCollision’ event and the Physics Contact object. It’s fairly new for Corona and there was a recent blog post. I also mentioned it in a forum post yesterday, so you should take a look at the links I put there:

https://developer.coronalabs.com/forum/2013/01/08/how-can-i-achieve-drag-and-drop-effect#comment-137904 [import]uid: 8271 topic_id: 34724 reply_id: 138030[/import]

I’m about to publish a complete Pong game tutorial/sample code that doesn’t use physics and has a CPU - it should be quite informative. I found physics isn’t really needed for something that simple.

Caleb [import]uid: 147322 topic_id: 34724 reply_id: 138817[/import]

I’m about to publish a complete Pong game tutorial/sample code that doesn’t use physics and has a CPU - it should be quite informative. I found physics isn’t really needed for something that simple.

Caleb [import]uid: 147322 topic_id: 34724 reply_id: 138817[/import]

Just published it yesterday:

http://developer.coronalabs.com/code/neonpong-complete-pong-sample-gametemplate

Caleb [import]uid: 147322 topic_id: 34724 reply_id: 139456[/import]

Just published it yesterday:

http://developer.coronalabs.com/code/neonpong-complete-pong-sample-gametemplate

Caleb [import]uid: 147322 topic_id: 34724 reply_id: 139456[/import]