[help] with collision detection on a Pong remake.

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
nice
Party member
Posts: 191
Joined: Sun Sep 15, 2013 12:17 am
Location: Sweden

[help] with collision detection on a Pong remake.

Post by nice »

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. :P
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
Player file

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
Conf file

Code: Select all

function love.conf(t)
	t.title = "Pong"

	-- Apperently the original window size
	t.window.width = 512
	t.window.height = 256
end
:awesome: Have a good day! :ultraglee:
thedaemon
Prole
Posts: 1
Joined: Thu Jun 15, 2017 5:40 pm

Re: [help] with collision detection on a Pong remake.

Post by thedaemon »

https://love2d.org/wiki/BoundingBox.lua

This is what I am using for my Pong game. It's a good basic start.
User avatar
nice
Party member
Posts: 191
Joined: Sun Sep 15, 2013 12:17 am
Location: Sweden

Re: [help] with collision detection on a Pong remake.

Post by nice »

thedaemon wrote: Fri Jun 30, 2017 9:36 pm https://love2d.org/wiki/BoundingBox.lua

This is what I am using for my Pong game. It's a good basic start.
Thanks I'll take a look at it later, but how do I/does it determine the height and width of my object?
:awesome: Have a good day! :ultraglee:
User avatar
nice
Party member
Posts: 191
Joined: Sun Sep 15, 2013 12:17 am
Location: Sweden

Re: [help] with collision detection on a Pong remake.

Post by nice »

Nevermind this part of the post, I moved "return ball" to the bottom of the script and at least it runs now

Right now I'm attempting to use BoundingBox to try to collision and this is what I have right now:

Code: Select all

local Player = require("player")

function love.load()
	Player:setup()
end


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

function ball:CheckCollision(x1, x2, w1, h1, x2, y2, w2, h2)
	  

	return ball.x < p1.X + p1.scX and
        p1.X < ball.x + ball.scaleX and
        ball.y < p1.Y + p1.scY and
    	p1.Y < ball.y + ball.scaleY
end
But for some reason I get an error message saying there's something wrong on line 39 saying "Syntax error: ball.lua:39: '<eof>' expected near 'function'" and I don't really understand why, is it because I've defiend my "boundingbox" incorrectly?
Attachments
error ball.PNG
error ball.PNG (13.85 KiB) Viewed 12348 times
:awesome: Have a good day! :ultraglee:
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: [help] with collision detection on a Pong remake.

Post by davisdude »

I believe that error is from the "return ball" at line 34
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
User avatar
Le Codex
Prole
Posts: 31
Joined: Thu Feb 09, 2017 10:56 am
Location: France
Contact:

Re: [help] with collision detection on a Pong remake.

Post by Le Codex »

Your error is quite simple actually : you've added in the arguments -two- 'x2'. The first one is supposed to be 'y1'. Zerobrane Studio tells you that by crossing the first of the two variables and by underlining the second one ;) .

Also, you can just simply add height and width variable to your player and ball using:

Code: Select all

p1.width = img:getWidth()
p1.height = img:getHeight()

p2.width = img:getWidth()
p2.height = img:getHeight()

ball.width = img:getWidth()
ball.height = img:getHeight()
and then add this to your main.lua and use it to test collision between two objects:

Code: Select all

function checkCollision(obj, obj2)
  local left = obj.x
  local up = obj.y
  local right = obj.x + obj.width
  local down = obj.y + obj.height
  
  local left2 = obj2.x 
  local up2 = obj2.y 
  local right2 = obj2.x + obj2.width
  local down2 = obj2.y + obj2.height
  
  return up < down2 and down > up2 and left < right2 and right > left2
end
And if you create everything from scratch, Sheepolution's tutorials are pretty good also.

Tell me if it works out ^^ !

(A .love file would be appreciated, so we could test out your game more easily ;) )

Code: Select all

if your.timeSpeed > 0 then universe:update(dt) else universe:destroy() end
User avatar
OmegaMax
Prole
Posts: 12
Joined: Wed Jun 07, 2017 3:49 pm

Re: [help] with collision detection on a Pong remake.

Post by OmegaMax »

+1 for Sheepolution's tutorials,I've used them also,very good tutorial.
Post Reply

Who is online

Users browsing this forum: No registered users and 34 guests