Need Help for Collision

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
KyleFlores1014
Citizen
Posts: 73
Joined: Thu May 25, 2017 1:43 pm

Need Help for Collision

Post by KyleFlores1014 »

Im trying to make a four side wall collision but when I add another wall all the functions goes into the newer wall but the older wall Ive added will be just a wall with no functions. Here is my code Im not using any libraries(I chose to).
Collision.love
(1.22 KiB) Downloaded 158 times
KyleFlores1014
Citizen
Posts: 73
Joined: Thu May 25, 2017 1:43 pm

Re: Need Help for Collision

Post by KyleFlores1014 »

Oh I forgot the other wall is currently commented.
KyleFlores1014
Citizen
Posts: 73
Joined: Thu May 25, 2017 1:43 pm

Re: Need Help for Collision

Post by KyleFlores1014 »

My code is messy sorry------
Collision(new).love
(2.3 KiB) Downloaded 154 times
This is the new file and the other wall is now uncommented and the right side of the wall is not working. Please help
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Need Help for Collision

Post by davisdude »

Hi,

I'm not really sure exactly what you mean by "the older wall Ive added will be just a wall with no functions."

That being said, I'm assuming what you're going for is to keep the player from going through the wall on the right side. If that is the case, you're very close. The problem is that your if-statement labeled "right side moving to the left" should be at the same level of indentation as the other statement. The reason you probably missed this is because you have almost no indentation, so it's really hard to tell what corresponds to what.

Let's take a look at your love.update like you have it right now:

Code: Select all

function love.update(dt)
Player:Move(dt)
    for i,p in ipairs(Player) do 
    p.r = 4
    p.l = 4
        for i,w in ipairs(Wall) do --for collision
            --left side moving to the right
            if CheckCol(p.x,p.y,p.w,p.h,w.x-4,w.y,10,w.h) then
                p.r = 0
            --right side moving to the left
            if CheckCol(p.x,p.y,p.w,p.h,w.x+w.w,w.y,4,w.h) then
                p.l = 0
            end
            end
        end
    end
end
This code is extremely difficult to read. If you indent the code, it's much more obvious what's going on:

Code: Select all

function love.update(dt)
	Player:Move(dt)
	for i,p in ipairs(Player) do 
		p.r = 4
		p.l = 4
		for i,w in ipairs(Wall) do --for collision
			--left side moving to the right
			if CheckCol(p.x,p.y,p.w,p.h,w.x-4,w.y,10,w.h) then
				p.r = 0
				--right side moving to the left
				if CheckCol(p.x,p.y,p.w,p.h,w.x+w.w,w.y,4,w.h) then
					p.l = 0
				end
			end
		end
	end
end
Now it's very obvious that the reason your function isn't working is because you aren't colliding with both the right and left walls at once.
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
KyleFlores1014
Citizen
Posts: 73
Joined: Thu May 25, 2017 1:43 pm

Re: Need Help for Collision

Post by KyleFlores1014 »

Ive fixed that problem already but I have another issue which is just minor. The minor problem is the right side and bottom part of the box does work but it overlaps unlike the left side and top part.
Collsion.love
(2.35 KiB) Downloaded 154 times
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Need Help for Collision

Post by davisdude »

If you change the less than signs in the CheckCol function with less than or equal to signs, it works for me
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
KyleFlores1014
Citizen
Posts: 73
Joined: Thu May 25, 2017 1:43 pm

Re: Need Help for Collision

Post by KyleFlores1014 »

Can you please send the code? Im having errors when I change it.
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Need Help for Collision

Post by davisdude »

This works for me

Code: Select all

function CheckCol(x1,y1,w1,h1,x2,y2,w2,h2) --bounding box
	return x1 <= x2+w2 and
	x2 <= x1+w1 and
	y1 <= y2+h2 and
	y2 <= y1+h1

end
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
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 50 guests