collision problem

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
PGUp
Party member
Posts: 105
Joined: Fri Apr 21, 2017 9:17 am

collision problem

Post by PGUp »

Code: Select all

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 ?
-
hamberge
Prole
Posts: 25
Joined: Wed Aug 16, 2017 2:55 pm

Re: collision problem

Post by hamberge »

Can you post the code that uses this? I.e., a love file? Also it'd be easier to read w proper indentation.
User avatar
Azhukar
Party member
Posts: 478
Joined: Fri Oct 26, 2012 11:54 am

Re: collision problem

Post by Azhukar »

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.

Code: Select all

if x1 < x2 + w2 and y1 + h1 > x2 and yp < y2 + h2 and x1 > x2 + w2 - (speed + 1) then
PGUp
Party member
Posts: 105
Joined: Fri Apr 21, 2017 9:17 am

Re: collision problem

Post by PGUp »

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.

Code: Select all

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
-
User avatar
Azhukar
Party member
Posts: 478
Joined: Fri Oct 26, 2012 11:54 am

Re: collision problem

Post by Azhukar »

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.
User avatar
zorg
Party member
Posts: 3436
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: collision problem

Post by zorg »

PGUp wrote: Sat Oct 21, 2017 12:12 pm
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.

Code: Select all

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 :3True 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.
PGUp
Party member
Posts: 105
Joined: Fri Apr 21, 2017 9:17 am

Re: collision problem

Post by PGUp »

zorg wrote: Sat Oct 21, 2017 12:47 pm
PGUp wrote: Sat Oct 21, 2017 12:12 pm
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.

Code: Select all

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?

Code: Select all


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

Who is online

Users browsing this forum: No registered users and 5 guests