Hi all, I have posted a discussion before about my air hockey project but now I am trying to use the built in Codea physics rather than fake the physics. This is what I have so far, but I am stuck and cannot figure out the collision detection and how to make the ball bounce off the players when the puck hits them…
supportedOrientations(LANDSCAPE_ANY)--can only be used in landscape position
function setup()
displayMode(FULLSCREEN)--starts the game in full screen (for testing purposes)
game=0--initial setup variable to show homescreen
win=0--setting a nul value to bypass the variable test asking who wins
physics.gravity(vec2(0,0))--removing the built in physics
p_ball=physics.body(CIRCLE,20)--creating bodies for the built in variables(x y variables to save time when coding)
r_ball=physics.body(CIRCLE,50)
l_ball=physics.body(CIRCLE,50)
r_ball.type=STATIC
l_ball.type=STATIC
p_ball.type=DYNAMIC
p_ball.restituion=0.5
wall1=physics.body(EDGE,vec2(30,30),vec2(30,HEIGHT-30))
wall2=physics.body(EDGE,vec2(30,30),vec2(WIDTH-30,30))
wall3=physics.body(EDGE,vec2(WIDTH-30,30),vec2(WIDTH-30,HEIGHT-30))
wall4=physics.body(EDGE,vec2(WIDTH-30,HEIGHT-30),vec2(30,HEIGHT-30))
wall1.type=STATIC wall2.type=STATIC wall3.type=STATIC wall4.type=STATIC
wall1.restitution=0.1 wall2.restitution=0.1 wall3.restitution=0.1 wall4.restitution=0.1
p_ball.bullet=true
p_speedX=0 p_speedY=0
end
function touched (touch)
if game==0 and touch.state==BEGAN then
game=1 rightscore=0 leftscore=0--starting the game from the menu screen
end
if game==1 then--sensing the sides that are being touched and then setting the player positions if they touch on their side
if touch.x > WIDTH/2+50 then
r_ball.x=touch.x
r_ball.y=touch.y
end
if touch.x < WIDTH/2-50 then
l_ball.x=touch.x
l_ball.y=touch.y
end
end
end
function draw()
if game==0 and win==0 then--homescreen and other setup variables
p_speedX=0--setting initial speed to 0
p_speedY=0--setting initial speed to 0
p_ball.x=WIDTH/2--placing the puck at the center of the screen
p_ball.y=HEIGHT/2
background(249, 249, 249, 255)
font("ArialRoundedMTBold")
fill(84, 62, 45, 255)
fontSize(100)
text("Air Hockey",WIDTH/2, HEIGHT-200)
fontSize(40)
text("Tap to Play",WIDTH/2, HEIGHT-270)
text("First Player to 7 Wins",WIDTH/2, HEIGHT-370)
font("ArialRoundedMTBold")
fill(143, 131, 122, 255)
fontSize(100)
text("Air Hockey",WIDTH/2+2, HEIGHT-200)
fontSize(40)
text("Tap to Play",WIDTH/2+2, HEIGHT-270)
text("First Player to 7 Wins",WIDTH/2+2, HEIGHT-370)
end
if game==0 and win==1 then --left side won menu screen
background(249, 249, 249, 255)
p_speedX=0
p_speedY=0
p_ball.x=WIDTH/2
p_ball.y=HEIGHT/2
font("ArialRoundedMTBold")
fill(84, 62, 45, 255)
fontSize(100)
text("Left Side Won",WIDTH/2, HEIGHT-200)
fontSize(40)
text("Tap to Play Again",WIDTH/2, HEIGHT-270)
fill(0, 7, 255, 255)
fontSize(100)
text("Left Side Won",WIDTH/2+2, HEIGHT-200)
fontSize(40)
text("Tap to Play Again",WIDTH/2+2, HEIGHT-270)
end
if game==0 and win==2 then --right side won menu screen
background(249, 249, 249, 255)
p_speedX=0
p_speedY=0
p_ball.x=WIDTH/2
p_ball.y=HEIGHT/2
font("ArialRoundedMTBold")
fill(84, 62, 45, 255)
fontSize(100)
text("Right Side Won",WIDTH/2, HEIGHT-200)
fontSize(40)
text("Tap to Play Again",WIDTH/2, HEIGHT-270)
fill(255, 0, 0, 255)
fontSize(100)
text("Right Side Won",WIDTH/2+2, HEIGHT-200)
fontSize(40)
text("Tap to Play Again",WIDTH/2+2, HEIGHT-270)
end
if game==1 then
--drawing the background for gameplay
background(249, 249, 249, 255)
strokeWidth(5)
stroke(0, 0, 0, 255)
line(WIDTH/2,HEIGHT-30,WIDTH/2,30)
fill(249,249,249,255)
ellipse(WIDTH/2,HEIGHT/2,200)
--drawing edges
stroke(0, 0, 0, 255)
line(30,30,30,HEIGHT-30)
line(30,HEIGHT-30, WIDTH-30,HEIGHT-30)
line(WIDTH-30,HEIGHT-30,WIDTH-30,30)
line(30,30,WIDTH-30,30)
--drawing goals
stroke(0, 0, 0, 255)
line(30,(HEIGHT/2)+100, 50, (HEIGHT/2)+100)
line(30,(HEIGHT/2)-100,50,(HEIGHT/2)-100)
line(WIDTH-30,(HEIGHT/2)+100, WIDTH-50, (HEIGHT/2)+100)
line(WIDTH-30,(HEIGHT/2)-100,WIDTH-50,(HEIGHT/2)-100)
--changing speeds
p_ball.linearVelocity=vec2(p_speedX,p_speedY)
if p_speedX>0.01 or p_speedX<-0.01 then
p_speedX=p_speedX-0.01
end
if p_speedY>0.01 or p_speedY<-0.01 then
p_speedY=p_speedY-0.01
end
--drawing puck
fill(162, 162, 162, 255)
ellipse(p_ball.x,p_ball.y,p_ball.radius*2)
--drawing two users
if r_ball.x >(WIDTH/2)+50 then --right side of screen
fill(255, 0, 0, 255)
ellipse(r_ball.x,r_ball.y,r_ball.radius*2)
end
if l_ball.x <(WIDTH/2)-50 then --left side of screen
fill(0, 7, 255, 255)
ellipse(l_ball.x,l_ball.y,l_ball.radius*2)
end
if p_ball.x <50 and p_ball.y < HEIGHT/2+100 and p_ball.y >HEIGHT/2-100 then
rightscore=rightscore+1 p_ball.x=WIDTH/2 p_ball.y=HEIGHT/2
end
if p_ball.x >WIDTH-50 and p_ball.y < HEIGHT/2+100 and p_ball.y >HEIGHT/2-100 then
leftscore=leftscore+1 p_ball.x=WIDTH/2 p_ball.y=HEIGHT/2
end
--detecting the win
if leftscore==7 then game=0 win=1 end
if rightscore==7 then game=0 win=2 end
--printing score (on top of everything else)
font("ArialRoundedMTBold")
fill(136, 136, 136, 255)
fontSize(30)
text("Score:",WIDTH*0.25,HEIGHT-15)
text(leftscore,(WIDTH*0.25)+65,HEIGHT-15)
fill(0, 7, 255, 255)
text("Score:",(WIDTH*0.25)+2,HEIGHT-14)
text(leftscore,(WIDTH*0.25)+67,HEIGHT-14)
fill(136, 136, 136, 255)
fontSize(30)
text("Score:",WIDTH*0.75,HEIGHT-15)
text(rightscore,(WIDTH*0.75)+65,HEIGHT-15)
fill(255, 0, 0, 255)
text("Score:",(WIDTH*0.75)+2,HEIGHT-14)
text(rightscore,(WIDTH*0.75)+67,HEIGHT-14)
end
sprite("Documents:namelogo",WIDTH-25,HEIGHT/2500)--my initials in the bottom left corner
end
function collide(contact)
--when there is a collision do...
end