Page 1 of 1

basic box collision

Posted: Thu Mar 08, 2012 4:37 am
by baconhawka7x
I was just wondering if i could have some excact syntax for a collision with a 16x16 tile,

Code: Select all

for i,v in ipairs(solid) do
     insert box collision/solution
end
I just want the tiles to be solid(hence the table name solid;) I would appreciate it a butt-load!:D

Thanks in advanced!:D

So I have some parts of it done so far..
But theres a part in my code where I'm not sure what to do. I left a blank space there, if you look under phy-troll.lua from lines 13-16. Idk what to put in there hah, I tried a couple of things but they all failed, so I'd love some help!:D

Re: basic box collision

Posted: Thu Mar 08, 2012 4:46 am
by timmeh42
One would have to know what's doing the colliding - what shape it is, or how accurate the collision has to be.

Re: basic box collision

Posted: Thu Mar 08, 2012 4:55 am
by baconhawka7x
the tile width and height is 16 pixels, and I just don't want the player to be able to move through it.
so like
if player.x > v.x then blah blah(that's obviously not exactly what you would say)
I don't want "player" walking through "solid".(some var's are player.x player.y!:D)
I know, it's a super annoying obnoxious question, but I've tried and failed too many times!

Re: basic box collision

Posted: Thu Mar 08, 2012 5:24 am
by veethree
I have a quite basic rectangle collision detection functon that i've been using for a while that you can possibly use

Code: Select all

function CheckCollision(x1, y1, w1, h1, x2, y2, w2, h2)
	if x1 > (x2 + w2) or (x1 + w1) < x2 then return false end
	if y1 > (y2 + h2) or (y1 + h1) < y2 then return false end
	return true
end
Usage:

Code: Select all

if CheckCollision(xpos1, ypos1, width1, height1, xpos2, ypos2, width2, height2) then
	--Whatever happens when collision is detected
end
You would replace the first 4 parameters with the player's position etc, and the other 4 with the tiles position etc or vice versa.


Simpler version for perfect squares only (you can use this if your player is also 16x16)

Code: Select all

function checkcollision(x1, y1, s1, x2, y2, s2)
	if x1 > (x2 + s2) or (x1 + s1) < x2 then return false end
	if y1 > (y2 + s2) or (y1 + s1) < y2 then return false end
	return true
end
Edit: I'm too lazy to do the exact code, But basically you just do a loop in which you check for collisions, If a collision is detected you stop the player from moving.

edit2: You also might wanna do a less accurate collision detection to see if the player is close the the solid tile, And only if the player is close enough run the collision detecting loop, You could possibly use a distance formula for that

Code: Select all

function getdistance(x1, y1, x2, y2)
	dx = x2 - x1
	dy = y2 - y1
	return = math.sqrt(math.pow(dx,2) + math.pow(dy,2))
end

Re: basic box collision

Posted: Thu Mar 08, 2012 8:52 am
by miko
baconhawka7x wrote:I was just wondering if i could have some excact syntax for a collision with a 16x16 tile,
Its not about tiles, but can be easily modified:
https://github.com/miko/Love2d-samples/ ... ionExample

Re: basic box collision

Posted: Thu Mar 08, 2012 5:36 pm
by Nixola
I simply check every frame if in the 'next' player.x and player.y variables there is a tile

Re: basic box collision

Posted: Fri Mar 09, 2012 2:53 pm
by baconhawka7x
Well, something I don't completely understand is, I obviously just don't want the player to walk through tiles(yes, the player is also 16x16) but there are functions that check collision for every side, so if I used that, how would I know what side hes on? so I can make sure he doesn't walk through that side?

Re: basic box collision

Posted: Fri Mar 09, 2012 3:18 pm
by veethree
baconhawka7x wrote:Well, something I don't completely understand is, I obviously just don't want the player to walk through tiles(yes, the player is also 16x16) but there are functions that check collision for every side, so if I used that, how would I know what side hes on? so I can make sure he doesn't walk through that side?
To find out which side he's on you can compare the position of the tile and the players, If the X of the player is less than the x of the tile at the time of collision you know the player must be on the left side of the tile. But i'm not too sure you'd need that information unless you want the player to bounce off the solid tile or something. otherwise you can just have a boolean for the player like "canMove" and if it's set to false you don't let the player move. (and you set it to false if the player is trying to walk into a solid tile)

Also, I might be supplying completely wrong information here since i don't know too much about the game. For example if the player is gridlocked you'd want to have a whole different approach. Just saying.

Re: basic box collision

Posted: Fri Mar 09, 2012 3:50 pm
by baconhawka7x
Alright, thanks!:D and sorry for all of the posts lately! ;P

Re: basic box collision

Posted: Fri Mar 09, 2012 4:07 pm
by veethree
baconhawka7x wrote:Alright, thanks!:D and sorry for all of the posts lately! ;P
Haha, I'm pretty sure just about everyone goes through a phase where they spam the forums with questions. It's a great way to learn though.