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]