[help] with collision detection on a Pong remake.
Posted: Fri Jun 30, 2017 8:49 pm
Hello everyone!
This might be a lengthy read but there's things that I want to explain thoroughly so I thank you for your patience.
I'm currently in the process of relearning/learning Löve2D/lua and to learn I'm remaking some older games and in this case: Pong.
What I have so far
So far I have two player "paddles" that can be controlled by 'W' and 'S' keys (Player 1) and up and down arrow keys (Player 2) and I also have a ball that travels in a straight line to the left.
What I want to learn/use in this project
For this project I wanted learn how to use multiple files in this project as previously when I were active in Löve2d which were around 4 years ago I created a game that were a little over 500 lines of code in one file. Which is why I wanted to use multiple files.
I also want to do as much coding as possible for this project so I don't want to use modules/libraries which is something that I want to learn at a later stage.
My problem
The problem that I have is that I have no idea where to start with the collision detection, I have taken a look at at noway's tutorial where they make an Arkanoid clone.
In my case I'm more specifically looking at the collision detection parts of the tutorial, in an attempt trying to translate it to my project which isn't going very well as I don't understand too much of it.
It's kinda boring to see my ball to just travel pretty much to infinity to the left so I want some collision detection and make this ball bounce on the player paddles and on the top and bottom of my game (but let's maybe start with player paddles first).
I would appreciate any help that gets me in the right direction as I have been struggling with this for a little while now.
You can find the scripts for this project below.
Main file
Player file
Ball file
Conf file
This might be a lengthy read but there's things that I want to explain thoroughly so I thank you for your patience.
I'm currently in the process of relearning/learning Löve2D/lua and to learn I'm remaking some older games and in this case: Pong.
What I have so far
So far I have two player "paddles" that can be controlled by 'W' and 'S' keys (Player 1) and up and down arrow keys (Player 2) and I also have a ball that travels in a straight line to the left.
What I want to learn/use in this project
For this project I wanted learn how to use multiple files in this project as previously when I were active in Löve2d which were around 4 years ago I created a game that were a little over 500 lines of code in one file. Which is why I wanted to use multiple files.
I also want to do as much coding as possible for this project so I don't want to use modules/libraries which is something that I want to learn at a later stage.
My problem
The problem that I have is that I have no idea where to start with the collision detection, I have taken a look at at noway's tutorial where they make an Arkanoid clone.
In my case I'm more specifically looking at the collision detection parts of the tutorial, in an attempt trying to translate it to my project which isn't going very well as I don't understand too much of it.
It's kinda boring to see my ball to just travel pretty much to infinity to the left so I want some collision detection and make this ball bounce on the player paddles and on the top and bottom of my game (but let's maybe start with player paddles first).
I would appreciate any help that gets me in the right direction as I have been struggling with this for a little while now.
You can find the scripts for this project below.
Main file
Code: Select all
local Player = require("player")
local Ball = require("ball")
function love.load()
Player:setup()
Ball:setup()
moveSpeed = 100
playerSpeed = 200
end
function love.update(dt)
-- Keyboard input --
-- Player 1 input
if love.keyboard.isDown("w") then
p1.Y = p1.Y - (playerSpeed * dt)
elseif love.keyboard.isDown("s") then
p1.Y = p1.Y + (playerSpeed * dt)
end
-- Player 2 input
if love.keyboard.isDown("up") then
p2.Y = p2.Y - (playerSpeed * dt)
elseif love.keyboard.isDown("down") then
p2.Y = p2.Y + (playerSpeed * dt)
end
-- Keep the Player 1 inside the screen --
if p1.Y < 0 then
p1.Y = 0
end
if p1.Y > 240 then
p1.Y = 240
end
-- Keep the Player 1 inside the screen --
if p2.Y < 0 then
p2.Y = 0
end
if p2.Y > 240 then
p2.Y = 240
end
-- Ball movement --
Ball.x = Ball.x - Ball.speed * dt
end
function love.draw()
-- Drawing Player 1
love.graphics.draw(p1.img, p1.X, p1.Y, p1.orien, p1.scX, p1.scY, origX, origY)
-- Drawing Player 2
love.graphics.draw(p2.img, p2.X, p2.Y, p2.orien, p2.scX, p2.scY, origX, origY)
-- Drawing the Ball
love.graphics.draw(Ball.img, Ball.x, Ball.y, Ball.orientation, Ball.scaleX, Ball.scaleY, Ball.originX, Ball.originY)
end
Code: Select all
local Player = {}
function Player:setup()
-- Player 1 graphics --
p1 =
{
-- Player graphics for the Paddle
img = love.graphics.newImage("playerOne.png"),
-- Player's X position
X = 12,
-- Player's Y position
Y = 128,
--Player's Orientation factor
orien = 0,
-- Player's Scale factor X-axis
scX = 1,
-- Player's Scale factor Y-axis
scY = 1,
-- Player's Origin X position
origX = 1,
-- Player's Origin Y position
origY = 8
}
p2 =
{
-- Player graphics for the Paddle
img = love.graphics.newImage("playerTwo.png"),
-- Player's X position
X = 500,
-- Player's Y position
Y = 128,
--Player's Orientation factor
orien = 0,
-- Player's Scale factor X-axis
scX = 1,
-- Player's Scale factor Y-axis
scY = 1,
-- Player's Origin X position
origX = 1,
-- Player's Origin X position
origY = 8
}
end
return Player
Ball file
Code: Select all
local ball = {}
function ball:setup()
-- Ball graphics --
ball.img = love.graphics.newImage("ball.png")
-- Ball's X position
ball.x = 256
-- Ball's Y position
ball.y = 128
-- Ball's Orientation factor
ball.orientation = 0
-- Ball's Scale factor X-axis
ball.scaleX = 1
-- Ball's Scale factor Y-axis
ball.scaleY = 1
-- Ball's Origin X position
ball.originX = 3
-- Ball's Origin Y position
ball.originY = 3
-- Ball Movement --
ball.speed = 100
ball.move = 0
end
return ball
Code: Select all
function love.conf(t)
t.title = "Pong"
-- Apperently the original window size
t.window.width = 512
t.window.height = 256
end