function CollRes(x1, y1, w1, h1, x2, y2, w2, h2, speed)
if x1 + w1 > x2 and y1 + h1 > y2 and y1 < x2 + h2 and x1 + w1 < x2 + (speed + 1) then
x1 = x2 - w1
end
if y1 + h1 > y2 and x1 + w1 > x2 and x1 < x2 + w2 and y1 + h1 < y2 + (speed + 1) then
y1 = y2 - h1
end
if x1 < x2 + w2 and y1 + h1 > x2 and yp < y2 + h2 and x1 > x2 + w2 - (speed + 1) then
x1 = x2 + w2
end
if y1 < y2 + h2 and x1 + w1 > x2 and x1 < x2 + w2 and y1 > y2 + h2 - (speed + 1) then
y1 = y2 + h2
end
return x1, y1
end
the x1 and y1 is the position of the first box, i am using the speed to prevent the first box overlap with the second box.
it will reposition the first box if it collides with the second box (x2, y2),
it works perfectly with 2 - 5 boxes, not working with like 20 boxes with different width and height, help ?
You have a typo on this line, there's a variable 'yp' that should be 'y1'. Your error message must have told you that you're comparing nil with a number, next time read the error message and post it.
Azhukar wrote: ↑Sat Oct 21, 2017 11:39 am
You have a typo on this line, there's a variable 'yp' that should be 'y1'. Your error message must have told you that you're comparing nil with a number, next time read the error message and post it.
PGUp wrote: ↑Sat Oct 21, 2017 12:12 pmthere was no error message, and fixed that typo already. the yp is the y position of the player
That can happen when you use global variables in such a way. It's much more maintainable to keep your variables as localized to their use as possible and avoid globals. You'll run into problems once your project gets bigger and a simple typo will be hard to track down because it will not result in an error, but rather it will access a global just like in this case and produce undefined behavior.
Azhukar wrote: ↑Sat Oct 21, 2017 11:39 am
You have a typo on this line, there's a variable 'yp' that should be 'y1'. Your error message must have told you that you're comparing nil with a number, next time read the error message and post it.
if x1 < x2 + w2 and y1 + h1 > x2 and yp < y2 + h2 and x1 > x2 + w2 - (speed + 1) then
there was no error message, and fixed that typo already. the yp is the y position of the player
1. I'm guessing there was no error message because you did have yp defined (which you didn't mention; kinda wasn't relevant to your issue then, but then again, it was, because of the typo; also see above, i got ninja'd)
2. saying you already fixed that typo while having the typo in your post doesn't tell us if you fixed the typo; this should be obvious...
3. doesn't matter what yp was, it had no place in your collision function.
Also, you forgot to tell us again, did that fix your code, or does it still not work with more than 5? Is the issue easily reproducable? as in, is there a hard line where it does start to fail consistently?
Me and my stuff True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Azhukar wrote: ↑Sat Oct 21, 2017 11:39 am
You have a typo on this line, there's a variable 'yp' that should be 'y1'. Your error message must have told you that you're comparing nil with a number, next time read the error message and post it.
if x1 < x2 + w2 and y1 + h1 > x2 and yp < y2 + h2 and x1 > x2 + w2 - (speed + 1) then
there was no error message, and fixed that typo already. the yp is the y position of the player
1. I'm guessing there was no error message because you did have yp defined (which you didn't mention; kinda wasn't relevant to your issue then, but then again, it was, because of the typo; also see above, i got ninja'd)
2. saying you already fixed that typo while having the typo in your post doesn't tell us if you fixed the typo; this should be obvious...
3. doesn't matter what yp was, it had no place in your collision function.
Also, you forgot to tell us again, did that fix your code, or does it still not work with more than 5? Is the issue easily reproducable? as in, is there a hard line where it does start to fail consistently?
function CollRes(x1, y1, w1, h1, x2, y2, w2, h2, speed)
if x1 + w1 > x2 and y1 + h1 > y2 and y1 < x2 + h2 and x1 + w1 < x2 + (speed + 1) then
x1 = x2 - w1
end
if y1 + h1 > y2 and x1 + w1 > x2 and x1 < x2 + w2 and y1 + h1 < y2 + (speed + 1) then
y1 = y2 - h1
end
if x1 < x2 + w2 and y1 + h1 > x2 and y1 < y2 + h2 and x1 > x2 + w2 - (speed + 1) then
x1 = x2 + w2
end
if y1 < y2 + h2 and x1 + w1 > x2 and x1 < x2 + w2 and y1 > y2 + h2 - (speed + 1) then
y1 = y2 + h2
end
return x1, y1
end
function love.load()
lg = love.graphics
lk = love.keyboard
xp = 50
yp = 50
speed = 3
end
function love.update()
if lk.isDown("right") then
xp = xp + speed
end
if lk.isDown("left") then
xp = xp - speed
end
if lk.isDown("up") then
yp = yp - speed
end
if lk.isDown("down") then
yp = yp + speed
end
xp, yp = CollRes(xp, yp, 50, 50, 101, 202, 50, 50, 3) -- collision with the "wall"
end
function love.draw()
lg.setColor(255,255,255)
lg.rectangle("fill", xp, yp, 50, 50)
lg.rectangle("fill", 101, 202, 50, 50) -- the "wall"
end
i also tried to change the collision checking from using speed to half of the collider's width and height, didnt work. it seems like it works when you try it, i move the player circling the "wall" counter clockwise and it doesnt work anymore
and it goes through the left side of the wall, weird..