Page 1 of 1
Collision! It ain't working.
Posted: Fri Sep 12, 2014 3:29 am
by Efbrefus
A while ago, I started playing with Love2d a bit, eventually left it. Came back, tried using the collision code I used in my previous experience, and it doesn't work anymore.
My collision code:
Code: Select all
function world.collide()
if player.x < 0 then
player.x = 0
elseif player.y < 0 then
player.y = 0
elseif player.y + player.image:getHeight() > 700 then
player.y = 600 - player.image:getHeight()
elseif player.x - player.image:getWidth() then
player.x = 800 - player.image:getWidth()
end
end
Cheers~
Re: Collision! It ain't working.
Posted: Fri Sep 12, 2014 4:46 am
by ivan
Code: Select all
elseif player.y + player.image:getHeight() > 600 then -- > 600
player.y = 600 - player.image:getHeight()
elseif player.x + player.image:getWidth() > 800 then -- > 800
player.x = 800 - player.image:getWidth()
end
This is not really collision but "clamping".
Alternative implementation:
Code: Select all
player.y = math.max(player.y, 0)
player.y = math.min(player.y, 600 - player.image:getHeight())
player.x = math.max(player.x, 0)
player.x = math.min(player.x, 800 - player.images:getWidth())
Yet antoher implementation:
Code: Select all
function clamp(value, low, high)
if value < low then
value = low
elseif value > high then
value = high
end
return value
end
player.x = clamp(player.x, 0, 800 - player.images:getWidth())
player.y = clamp(player.y, 0, 600 - player.images:getHeight())
Re: Collision! It ain't working.
Posted: Fri Sep 12, 2014 10:40 pm
by Efbrefus
ivan wrote:Code: Select all
elseif player.y + player.image:getHeight() > 600 then -- > 600
player.y = 600 - player.image:getHeight()
elseif player.x + player.image:getWidth() > 800 then -- > 800
player.x = 800 - player.image:getWidth()
end
This is not really collision but "clamping".
Alternative implementation:
Code: Select all
player.y = math.max(player.y, 0)
player.y = math.min(player.y, 600 - player.image:getHeight())
player.x = math.max(player.x, 0)
player.x = math.min(player.x, 800 - player.images:getWidth())
Yet antoher implementation:
Code: Select all
function clamp(value, low, high)
if value < low then
value = low
elseif value > high then
value = high
end
return value
end
player.x = clamp(player.x, 0, 800 - player.images:getWidth())
player.y = clamp(player.y, 0, 600 - player.images:getHeight())
Thanks mate, I'll see if that works.
Re: Collision! It ain't working.
Posted: Sat Sep 13, 2014 2:47 am
by Jasoco
Here's an even better one:
Code: Select all
function clamp(n, low, high)
return math.min(math.max(n, low), high)
end
Re: Collision! It ain't working.
Posted: Sat Sep 13, 2014 3:39 am
by Zilarrezko
Or a more confusing one.
EDIT: and apparently an even better one! Although, to be honest, Unless you're calling this every frame, multiple times, you won't see a difference.
Code: Select all
function clamp(val, high, low)
return (val > high and high) or (val < low and low) or val
end
Forgot where I saw the "(conditional and value) or (conditional and value)" where it only returns the value and not the conditions boolean. Though I don't understand it, I implemented it into clamp. Infact I think I might ask a question about that in the questions that don't deserve their own thread.