Collisions don't work right

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
katd
Prole
Posts: 3
Joined: Thu Jan 05, 2017 5:00 pm

Collisions don't work right

Post by katd »

Hello Guys! I have been trying to make a Pong clone. My progress stopped since the collisions on the top, bottom and paddles of the game didn't work. So it would be nice to help me out of this problem. And could you also suggest changes to improve me code?
Thanks.

Code: Select all

function love.load()

	lplayer = {}
	lplayer.x = 0
	lplayer.y = love.graphics.getHeight() / 2
	lplayer.len = 10
	lplayer.height = 70
	lplayer.pixels = 1200

	rplayer = {}
	rplayer.x = love.graphics.getWidth() - 10
	rplayer.y = love.graphics.getHeight() / 2
	rplayer.len = 10
	rplayer.height = 70
	rplayer.pixels = 1200

	ball = {}
	ball.x = love.graphics.getWidth() / 2
	ball.y = love.graphics.getHeight() / 2
	ball.len = 10
	ball.height = 10
	ball.dx = -300
	ball.dy = 150


end

function love.update(dt)

	-- INPUT FROM PLAYER 1 --

	if (love.keyboard.isDown('d')) then
		if ((lplayer.y + lplayer.height) < love.graphics.getHeight()) then
			lplayer.y = lplayer.y + (lplayer.pixels * dt)
		end
	end
	if (love.keyboard.isDown('a')) then
		if (lplayer.y > 0) then
			lplayer.y = lplayer.y - (lplayer.pixels * dt)
		end
	end

	-- INPUT FROM PLAYER 2 --

	if (love.keyboard.isDown('h')) then
		if ((rplayer.y + rplayer.height) < love.graphics.getHeight()) then
			rplayer.y = rplayer.y + (rplayer.pixels * dt)
		end
	end
	if (love.keyboard.isDown('k')) then
		if (rplayer.y > 0) then
			rplayer.y = rplayer.y - (rplayer.pixels * dt)
		end


	end

	-- UP and DOWN COLLISSION  --
	if (ball.y == 0) then
		ball.dy = -ball.dy
	elseif(ball.y == love.graphics.getHeight()) then
		ball.dy = -ball.dy
	end

	-- BALL MOVEMENT --
	ball.x = ball.x + (ball.dx * dt)
	ball.y = ball.y + (ball.dy * dt)

	-- BALL PADDLECOLISSION --
	if (ball.x == lplayer.len and ball.y <= lplayer.y and ball.y >=(lplayer.y-lplayer.height)) then
		ball.dx = -ball.dx
	end
	if (ball.x == (love.graphics.getWidth() - rplayer.len) and ball.y <= rplayer.y and ball.y >= (rplayer.y - rplayer.height)) then
		ball.dx = -ball.dx
	end


end


function love.draw()
	love.graphics.setColor(255, 255, 255)
	love.graphics.rectangle("fill", lplayer.x, lplayer.y, lplayer.len, lplayer.height)
	love.graphics.rectangle("fill", rplayer.x, rplayer.y, rplayer.len, rplayer.height)
	love.graphics.rectangle("fill", ball.x, ball.y, ball.len, ball.height)
end
nyenye
Citizen
Posts: 62
Joined: Fri Dec 02, 2016 1:44 pm

Re: Collisions don't work right

Post by nyenye »

Hii, first of all keep in mind that the coordinate system of Love is top-left to bottom-right. So normally if you have a rectangle, it's X and Y position will be the top-left corner, and then you will have a width (maybe it's your len?), and a height.
With my mad paint skills I've made you a little scheme of how it should be.
exmaple.png
exmaple.png (4.71 KiB) Viewed 4065 times
Having said this, if you try to add the height instead of subtracting it, does it work?
katd
Prole
Posts: 3
Joined: Thu Jan 05, 2017 5:00 pm

Re: Collisions don't work right

Post by katd »

No! I forgot about it. But when i change the operators and subtract the height, it still doesn't work.
And the collision with the top and the bottom either.
User avatar
peterrust
Prole
Posts: 42
Joined: Thu Dec 29, 2016 8:49 pm
Location: Bellingham, WA, USA
Contact:

Re: Collisions don't work right

Post by peterrust »

katd, you're using an equality check for the up and down collision and for part of the paddle collision. Typically collision code can't rely on positions being exactly equal (i.e. touching), since the updates don't happen at every single possible instant of time. Instead, collision code usually has to check for positions being overlapping.

There are some images on kikito's bump library page that demonstrate this (https://github.com/kikito/bump.lua). You'll notice that each of the images has a goalX and goalY -- these are the coordinates that the moving object was attempting to go to on the update and in each picture it overlaps the obstacle, so if the code is going to be precise, it has to compensate for this overlap by undoing the overlap in some way (sliding or bouncing or rewinding to the touch point). Pong is a simple enough game and your update() rate should be high enough that you may be able to get away with ignoring the overlap-compensation, but at the least, you'll have to check for an overlap instead of checking for equality.

For example in the up and down collision checking, instead of checking if y == 0, you'll need to check if y <= 0. Likewise instead of checking if y == love.graphics.getHeight(), you'll need to check if y >= love.graphics.getHeight().
User avatar
peterrust
Prole
Posts: 42
Joined: Thu Dec 29, 2016 8:49 pm
Location: Bellingham, WA, USA
Contact:

Re: Collisions don't work right

Post by peterrust »

katd,

Quick follow-up: if you don't deal with the overlap then it can result in bugs with the code written the way that it is. For example, if y is -5 and dy is -4, then your y <= 0 code would detect that the ball needs to bounce against the screen and would reverse the dy to 4. The problem with this is if, for instance, the delta between updates is 1 then on the next update the y would still be negative (-1), even though it's moving in the right direction, and your y <= 0 code would detect that and reverse the direction *again*, which isn't what you want. So you could easily account for the overlap by setting y = 0 when this condition is detected (and, likewise, setting y = love.graphics.getHeight() in the opposite condition).
katd
Prole
Posts: 3
Joined: Thu Jan 05, 2017 5:00 pm

Re: Collisions don't work right

Post by katd »

Thank you for the quick reply. I will keep in mind what you said. Now it works well and i can move on. This community is great!
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 2 guests