[Solved] Detecting if I'm hitting a wall, not in it.

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
User avatar
Sheepolution
Party member
Posts: 264
Joined: Mon Mar 04, 2013 9:31 am
Location: The Netherlands
Contact:

[Solved] Detecting if I'm hitting a wall, not in it.

Post by Sheepolution »

q26hCVH.png
q26hCVH.png (8.04 KiB) Viewed 125 times
This is what I have right now.
And what I want to achieve is that 1 also becomes true. And 2 actually should never happen, since you hit the wall.

So I'm trying to figure out how to do this. I thought of adding the movement speed to the function, but that didn't really work.

My checkCollision function

Code: Select all

function checkCollision(x1,y1,w1,h1,hspd,x2,y2,w2,h2,c)
	local bbleft1, bbright1, bbup1, bbdown1, bbleft2, bbright2, bbup2, bbdown2, check = x1 - w1-hspd, x1 + w1+hspd, y1 - h1, y1 + h1, x2 - w2, x2 + w2, y2 - h2, y2 + h2, c


	if (bbright1 >= bbleft2 and bbleft1 <= bbright2) and (bbup1 <= bbdown2 and bbdown1 >= bbup2) then
	if check == "bot" then
		if bbdown1 >= bbup2 and bbup1 <= bbup2 then

			return true
		else
			return false
		end
	end
		-----------
	if check == "top" then
		if bbup1 <= bbdown2 and bbdown1 >= bbdown2 then

			return true
		else
			return false

		end
	end
		-----------
	if check== "right" then
		if bbright1 >= bbleft2 and bbleft1 <= bbleft2 then
			return true
		else
			return false
		end
	end
		-----------
	if check== "left" then
		if bbleft1 <= bbright2 and bbright1 >= bbright2 then
			return true
		else
			return false
		end
	end
		-----------
	else
		return false
	end
end
So the exact thing I want to achieve is: Make the player hit a wall when there is no space between them, instead of when they are overlapping.
How can I do this?
Thank you in advance.
Attachments
collision problem.love
controls: a,d and w
(3.59 KiB) Downloaded 56 times
Last edited by Sheepolution on Tue Aug 13, 2013 10:35 am, edited 3 times in total.
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: My collision detects when I'm IN the wall, not against i

Post by micha »

The collision function is alright like this. What you need to get right, is the code around it.

In a time step, you move one block around. Instead of directly putting it to the new position, you handle the new position as a proposed position. You test if collision would occur in this position. If no, then accepd the proposed position and move the block. If the proposed position causes a collision, then you have to correct the proposed position until no collision happens. And then you move the block. In order to correct the proposed position it is helpfull if the collision function also returns the amount of overlap (e.g. in pixels). Then you can revert the overlap.

By the way: I cannot run your .love.
User avatar
Sheepolution
Party member
Posts: 264
Joined: Mon Mar 04, 2013 9:31 am
Location: The Netherlands
Contact:

Re: My collision detects when I'm IN the wall, not against i

Post by Sheepolution »

Fixed the file.

That sounds pretty logical actually. I'll try to play with the overlapping.

Okay so I tried to do something with the overlapping, but it's not really working. What I did was returning the overlapping value and make the player position with that value. I'm really confused about how to fix this.

I'll add the updated file in this post.
Attachments
collision problem2.love
Newer version
(7.08 KiB) Downloaded 61 times
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 4 guests