Need Help for Collision
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
-
- Citizen
- Posts: 73
- Joined: Thu May 25, 2017 1:43 pm
Need Help for Collision
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).
-
- Citizen
- Posts: 73
- Joined: Thu May 25, 2017 1:43 pm
Re: Need Help for Collision
Oh I forgot the other wall is currently commented.
-
- Citizen
- Posts: 73
- Joined: Thu May 25, 2017 1:43 pm
Re: Need Help for Collision
My code is messy sorry------
This is the new file and the other wall is now uncommented and the right side of the wall is not working. Please helpRe: Need Help for Collision
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:
This code is extremely difficult to read. If you indent the code, it's much more obvious what's going on:
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.
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
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
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
-
- Citizen
- Posts: 73
- Joined: Thu May 25, 2017 1:43 pm
Re: Need Help for Collision
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.
Re: Need Help for Collision
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
-
- Citizen
- Posts: 73
- Joined: Thu May 25, 2017 1:43 pm
Re: Need Help for Collision
Can you please send the code? Im having errors when I change it.
Re: Need Help for Collision
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
Who is online
Users browsing this forum: Google [Bot] and 50 guests