Page 1 of 1
Collision Help
Posted: Sat Sep 22, 2012 3:19 am
by Pyromaniac
Hi "Lovers", I'm Mainly creating a topic for help with my games collison. I've been having trouble with a few collision detection libs but can't figure out how to use BoundingBox.lua. Can someone give me a rundown on what to do with it?
Re: Collision Help
Posted: Sat Sep 22, 2012 8:14 am
by Petunien
I've this old collision demo on my disk. It's fully commented out. Hope it helps.
Move the player's rectangle with the arrow keys and collide with the white rectangle.
Please ask if you got questions.
Re: Collision Help
Posted: Sat Sep 22, 2012 11:59 pm
by Pyromaniac
I've encountered an error when adding the collision to my code.
Code: Select all
main.lua:52: attempt to call global 'CheckCollision' (a nil value)
This is lines 52 onward.
Code: Select all
if CheckCollision(char_x, player_y, 25, 25, ex_x, ex_y, 25, 25) then -- Checks if player and the white rectangle are colliding, if that true -> Print "Ouch!"
love.graphics.print("Collided", 10, 10)
end
function CheckCollision(ax1, ay1, aw, ah, bx1, by1, bw, bh)
local ax2,ay2,bx2,by2 = ax1 + aw, ay1 + ah, bx1 + bw, by1 + bh
return ax1 < bx2 and ax2 > bx1 and ay1 < by2 and ay2 > by1
end
end
Re: Collision Help
Posted: Sun Sep 23, 2012 6:25 am
by josefnpat
Please supply your .love so we can help you debug.
And next time, please post this in the
Support and Development sub-forum.
If possible, could a mod please move this thread?
Re: Collision Help
Posted: Sun Sep 23, 2012 3:52 pm
by Pyromaniac
Sorry for posting this in the wrong section. Attached is the .love file.
Re: Collision Help
Posted: Mon Sep 24, 2012 3:06 pm
by Petunien
We should have found the error already looking at your posted code.
The "if" that checks for collision doesn't have a "end". The CheckCollision-function does have two instead.
Just put the below one after the if.
Code: Select all
if CheckCollision(char_x, player_y, 25, 25, ex_x, ex_y, 25, 25) then
love.graphics.print("Collided", 10, 10)
end
function CheckCollision(ax1, ay1, aw, ah, bx1, by1, bw, bh)
local ax2,ay2,bx2,by2 = ax1 + aw, ay1 + ah, bx1 + bw, by1 + bh
return ax1 < bx2 and ax2 > bx1 and ay1 < by2 and ay2 > by1
end
And when that's done, attention! Your parameters on the Collision check are nil!