So I am trying to get the computer controlled pong player to follow the pong, also i am trying to get the pong to move, and I would like to know how to make a pong collision without using the complicated box2d physics engine.
The file is included below and thanks for your help! ^-^
How to make simple A.I and collision in a pong clone
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
How to make simple A.I and collision in a pong clone
- Attachments
-
- main.love
- (413 Bytes) Downloaded 147 times
- mcjohnalds45
- Prole
- Posts: 18
- Joined: Sat Jun 02, 2012 12:08 pm
Re: How to make simple A.I and collision in a pong clone
First of all, to move the computer's paddle up and down, you can check if the pong is above the computers paddle and if it is move it up, then check if the pong is below the computers paddle and if it is move it down.
Getting the pong to move is a simple as two lines.
Then to check if the pong is colliding width one of the paddles you can use this function that checks if the one rectangle is intersecting another.
Example Usage.
Finally i'd suggest making the ball move a little faster than the computer at some point or the computer will never lose.
Code: Select all
if pong.y > computer.y + (computer.width / 2) then
computer.y = computer.y + (computer.speed * dt)
end
if pong.y < computer.y + (computer.width / 2) then
computer.y = computer.y - (computer.speed * dt)
end
-- the "+ (computer.width / 2)" is to check if the pong is above or below the centre of the paddle rather than the top
Code: Select all
pong.x = pong.x + (pong.speed * dt)
pong.y = pong.y + (pong.speed * dt)
Code: Select all
function collision(X1, Y1, W1, H1, X2, Y2, W2, H2)
if X1 > X2 + W2 then
return false
end
if X1 + W1 < X2 then
return false
end
if Y1 > Y2 + H2 then
return false
end
if Y1 + H1 < Y2 then
return false
end
return true
end
Code: Select all
if collision(player.x, player.y, player.width, player.height, pong.x, pong.y, pong.size, pong.size) then
print("bazinga")
end
Re: How to make simple A.I and collision in a pong clone
Thank you I never thought of that.
Who is online
Users browsing this forum: No registered users and 3 guests