Collision! It ain't working.

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
Efbrefus
Prole
Posts: 8
Joined: Tue Jun 03, 2014 9:57 pm

Collision! It ain't working.

Post 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~
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Collision! It ain't working.

Post 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())
Efbrefus
Prole
Posts: 8
Joined: Tue Jun 03, 2014 9:57 pm

Re: Collision! It ain't working.

Post 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.
User avatar
Jasoco
Inner party member
Posts: 3726
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Collision! It ain't working.

Post 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
User avatar
Zilarrezko
Party member
Posts: 345
Joined: Mon Dec 10, 2012 5:50 am
Location: Oregon

Re: Collision! It ain't working.

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

Who is online

Users browsing this forum: No registered users and 1 guest