Collision Point Problems [SORT OF SOLVED]

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Hoksy
Prole
Posts: 5
Joined: Fri Mar 22, 2013 9:19 pm

Collision Point Problems [SORT OF SOLVED]

Post by Hoksy »

I'm making a simple maze game as a test to learn how to make collisions using tables (for multiple entities).
To stop player movement, I theorized that by checking if the coordinates of where the player wanted to move are inside that of a wall, then it disables movement.

Code: Select all

function entCollidePoint(ent,cx,cy) --entity table name, point we're checking's x position, point we're checking's y position

for i,v in ipairs(ent) do

local xcheck, ycheck

	if cx>v.x and cx<v.x+v.w then
		xcheck=true
	else
		xcheck=false
	end

	if cy>v.y and cy<v.y+v.h then
		ycheck=true
	else
		ycheck=false
	end
	
	return xcheck and ycheck
	
end
My love.update is then as follows:

Code: Select all

if love.keyboard.isDown('up')==true then
	vk_up=1
else
	vk_up=0
end

if love.keyboard.isDown('down')==true then
	vk_down=1
else
	vk_down=0
end

if love.keyboard.isDown('left')==true then
	vk_left=1
else
	vk_left=0
end

if love.keyboard.isDown('right')==true then
	vk_right=1
else
	vk_right=0
end

--PLAYER CODE
for i,v in ipairs(player) do

if entCollidePoint(box,v.x+4,v.y)==false and entCollidePoint(box,v.x-4,v.y)==false then
	v.x=v.x+((vk_right-vk_left)*4)
end

if entCollidePoint(box,v.x,v.y+4)==false and entCollidePoint(box,v.x,v.y-4)==false then
	v.y=v.y+((vk_down-vk_up)*4)
end

end
After running this code, the player is still able to walk through walls. Where am I going wrong?
MazeTest.love
(1.45 KiB) Downloaded 334 times
Last edited by Hoksy on Mon Apr 01, 2013 1:04 am, edited 3 times in total.
User avatar
daviddoran
Prole
Posts: 30
Joined: Sun Mar 24, 2013 5:35 am

Re: Collision Point Problems

Post by daviddoran »

Can you give us the .love? Much easier to debug with. Just zip your folder and rename .zip to .love.
Hoksy
Prole
Posts: 5
Joined: Fri Mar 22, 2013 9:19 pm

Re: Collision Point Problems

Post by Hoksy »

daviddoran wrote:Can you give us the .love? Much easier to debug with. Just zip your folder and rename .zip to .love.
Sure :) No, problem.
Post updated with source.
User avatar
daviddoran
Prole
Posts: 30
Joined: Sun Mar 24, 2013 5:35 am

Re: Collision Point Problems [UNSOLVED]

Post by daviddoran »

Hi Hoksy,

I found a few problems:
  • The for loop in entCollidePoint always returned after checking the first box
  • The collision checking at right and bottom didn't take the player width/height into account
  • The collision checking uses less than and greater than, so it breaks when the player is on the very edge of a box
Personally, I'd probably use one of the many collision checking libs because it's not the kind of wheel you want to reinvent.
I've uploaded my edited .love which works except when the player is on a box edge (very easy to replicate).
Attachments
MazeTest2.love
MazeTest updated
(2.2 KiB) Downloaded 135 times
User avatar
veethree
Inner party member
Posts: 877
Joined: Sat Dec 10, 2011 7:18 pm

Re: Collision Point Problems [UNSOLVED]

Post by veethree »

Code: Select all

if love.keyboard.isDown('up')==true then
The "==true" part is unnecessary.
Hoksy
Prole
Posts: 5
Joined: Fri Mar 22, 2013 9:19 pm

Re: Collision Point Problems [UNSOLVED]

Post by Hoksy »

veethree wrote:

Code: Select all

if love.keyboard.isDown('up')==true then
The "==true" part is unnecessary.
Good to know, I wasn't sure.
daviddoran wrote:Hi Hoksy,

I found a few problems:
  • The for loop in entCollidePoint always returned after checking the first box
  • The collision checking at right and bottom didn't take the player width/height into account
  • The collision checking uses less than and greater than, so it breaks when the player is on the very edge of a box
Personally, I'd probably use one of the many collision checking libs because it's not the kind of wheel you want to reinvent.
I've uploaded my edited .love which works except when the player is on a box edge (very easy to replicate).
Thanks for the list, I'm not the best of coders and I usually forget to do things.

As far as using a library, I'd be fine with using one as long as it worked with LuaPlayer on PSP (Pure Lua code). I know this isn't the mindset of these developers so that's why I tried to make my own code first. (Just found HardonCollider it appears to do what I'm looking for)

Thanks!
User avatar
substitute541
Party member
Posts: 484
Joined: Fri Aug 24, 2012 9:04 am
Location: Southern Leyte, Visayas, Philippines
Contact:

Re: Collision Point Problems [SORT OF SOLVED]

Post by substitute541 »

You'd want to move your player against the wall when your collision statements are true so the player can't go through the walls.
Currently designing themes for WordPress.

Sometimes lurks around the forum.
User avatar
daviddoran
Prole
Posts: 30
Joined: Sun Mar 24, 2013 5:35 am

Re: Collision Point Problems [SORT OF SOLVED]

Post by daviddoran »

No problem Hoksy. I actually didn't find the code that bad, I just realised some things didn't have the intended effect.

RE: collision libraries there's also https://github.com/kikito/bump.lua which aims to be lightweight. Thought I'm sure you can roll your own for this use case if you don't really want collisions, but just want to avoid going through walls. So instead of checking a point (e.g. v.x-4, v.y) collides with a box you'd check whether a given move would make your box overlap with another box. If so, then don't allow the move. The simple code to check two boxes overlap is here: http://www.love2d.org/wiki/BoundingBox.lua
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 12 guests