basic box collision

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
User avatar
baconhawka7x
Party member
Posts: 484
Joined: Mon Nov 21, 2011 7:05 am
Contact:

basic box collision

Post by baconhawka7x » Thu Mar 08, 2012 4:37 am

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
Attachments
Archive.love
DOwnload!:DDDF:LKJDSF:lkajsd;flkj!!11!
(63.64 KiB) Downloaded 48 times
Last edited by baconhawka7x on Sat Mar 10, 2012 12:23 am, edited 2 times in total.

User avatar
timmeh42
Citizen
Posts: 90
Joined: Wed Mar 07, 2012 7:32 pm
Location: Cape Town, South Africa

Re: basic box collision

Post by timmeh42 » Thu Mar 08, 2012 4:46 am

One would have to know what's doing the colliding - what shape it is, or how accurate the collision has to be.

User avatar
baconhawka7x
Party member
Posts: 484
Joined: Mon Nov 21, 2011 7:05 am
Contact:

Re: basic box collision

Post by baconhawka7x » Thu Mar 08, 2012 4:55 am

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!

User avatar
veethree
Inner party member
Posts: 814
Joined: Sat Dec 10, 2011 7:18 pm

Re: basic box collision

Post by veethree » Thu Mar 08, 2012 5:24 am

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

User avatar
miko
Party member
Posts: 409
Joined: Fri Nov 26, 2010 2:25 pm
Location: PL

Re: basic box collision

Post by miko » Thu Mar 08, 2012 8:52 am

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
My lovely code lives at GitHub: http://github.com/miko/Love2d-samples

User avatar
Nixola
Inner party member
Posts: 1940
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: basic box collision

Post by Nixola » Thu Mar 08, 2012 5:36 pm

I simply check every frame if in the 'next' player.x and player.y variables there is a tile
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics

User avatar
baconhawka7x
Party member
Posts: 484
Joined: Mon Nov 21, 2011 7:05 am
Contact:

Re: basic box collision

Post by baconhawka7x » Fri Mar 09, 2012 2:53 pm

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?

User avatar
veethree
Inner party member
Posts: 814
Joined: Sat Dec 10, 2011 7:18 pm

Re: basic box collision

Post by veethree » Fri Mar 09, 2012 3:18 pm

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.

User avatar
baconhawka7x
Party member
Posts: 484
Joined: Mon Nov 21, 2011 7:05 am
Contact:

Re: basic box collision

Post by baconhawka7x » Fri Mar 09, 2012 3:50 pm

Alright, thanks!:D and sorry for all of the posts lately! ;P

User avatar
veethree
Inner party member
Posts: 814
Joined: Sat Dec 10, 2011 7:18 pm

Re: basic box collision

Post by veethree » Fri Mar 09, 2012 4:07 pm

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.

Post Reply

Who is online

Users browsing this forum: slime and 16 guests