Page 1 of 1

my collision is bugging out and i cant't figure it out

Posted: Thu Sep 26, 2013 4:24 pm
by henrik9864
i have worked on a collision detection system for a week now and cannot make it work properly. i have tried to search for help but none of the cases have worked for me. :cry:

it works fine with two objects but when it gets more than tree to handle it bugs out. I am quite new to lua and very new to love so try to keep it simple (not necessary can google it :P but it would help alot )

here is the code:
[function physics:Collided(obj1, obj2)
if obj1 == nil then
return false
end
if obj2 == nil then
return false
end
-- obj2 side
local height1 = obj1.texture:getHeight( )
local width1 = obj1.texture:getWidth( )
local height2 = obj2.texture:getHeight( )
local width2 = obj2.texture:getWidth( )
if obj1.pos.x > obj2.pos.x and obj1.pos.x < obj2.pos.x+width2 or obj1.pos.x+width1 > obj2.pos.x and obj1.pos.x+width1 < obj2.pos.x+width2 then
if obj1.pos.y > obj2.pos.y and obj1.pos.y < obj2.pos.y+height2 or obj1.pos.y+height1 > obj2.pos.y and obj1.pos.y+height1 < obj2.pos.y+height2 then
local left = obj2.pos.x+width2 < obj1.pos.x+1
local right = obj2.pos.x > obj1.pos.x+width1-1
local up = obj2.pos.y+height2 < obj1.pos.y+1
local down = obj2.pos.y > obj1.pos.y+height1-1
--if obj2.name == "lol1" then
print(left,right,up,down)
--end
if up then
return true, "up"
elseif down then
return true, "down"
elseif right then
return true, "right"
elseif left then
return true, "left"
end
end
end
return false
end][/code]

also tips is appreciated :awesome:

Re: my collision is bugging out and i cant't figure it out

Posted: Thu Sep 26, 2013 7:19 pm
by DaedalusYoung

Code: Select all

function physics:Collided(obj1, obj2)
    if obj1 == nil then
        return false
    end
    if obj2 == nil then
        return false
    end
	-- obj2 side 
	local height1 = obj1.texture:getHeight( )
	local width1 = obj1.texture:getWidth( )
	local height2 = obj2.texture:getHeight( )
	local width2 = obj2.texture:getWidth( )
	if obj1.pos.x > obj2.pos.x and obj1.pos.x < obj2.pos.x+width2 or obj1.pos.x+width1 > obj2.pos.x and obj1.pos.x+width1 < obj2.pos.x+width2 then
		if obj1.pos.y > obj2.pos.y and obj1.pos.y < obj2.pos.y+height2 or obj1.pos.y+height1 > obj2.pos.y and obj1.pos.y+height1 < obj2.pos.y+height2 then
			local left = obj2.pos.x+width2 < obj1.pos.x+1
			local right = obj2.pos.x > obj1.pos.x+width1-1
			local up = obj2.pos.y+height2 < obj1.pos.y+1
			local down = obj2.pos.y > obj1.pos.y+height1-1
			--if obj2.name == "lol1" then
			print(left,right,up,down)
			--end
			if up then
				return true, "up"
			elseif down then
				return true, "down"
			elseif right then
				return true, "right"
			elseif left then
				return true, "left"
			end
		end
	end
	return false
end
Fixed your forum code tags

Re: my collision is bugging out and i cant't figure it out

Posted: Thu Sep 26, 2013 8:19 pm
by henrik9864
thanks :3
im new to love

Re: my collision is bugging out and i cant't figure it out

Posted: Thu Sep 26, 2013 8:53 pm
by micha
Hi and welcome to the LÖVE community.

Here is a suggestion to improve your code.
Replace this:

Code: Select all

if obj1.pos.x > obj2.pos.x and obj1.pos.x < obj2.pos.x+width2 or obj1.pos.x+width1 > obj2.pos.x and obj1.pos.x+width1 < obj2.pos.x+width2 then
if obj1.pos.y > obj2.pos.y and obj1.pos.y < obj2.pos.y+height2 or obj1.pos.y+height1 > obj2.pos.y and obj1.pos.y+height1 < obj2.pos.y+height2 then
by this:

Code: Select all

if obj1.pos.x < obj2.pos.x + width2 and
  obj1.pos.x + width1 > obj2.pos.x and
  obj1.pos.y < obj2.pos.y + height2 and
  obj1.pos.y + height1 > obj2.pos.y then
That way you can save one if-condition (by joining the two condition with "and") and you can save 4 of the 8 checks. If this is not clear to you, why 4 conditions are enough, try drawing two rectangles on paper, that intersect.

For further help you can do the following things to help us help you:
- upload a .love file of what you have (sometimes the error is not in the snippet that people post)
- state exactly what happens and what you would like to happen (and how we can reproduce the error).

Re: my collision is bugging out and i cant't figure it out

Posted: Tue Oct 01, 2013 5:21 pm
by henrik9864
Thanks micha for helping out :awesome:

I did not know that 4 conditions was enough but when i tried drawing it on a piece of paper i saw it :D

I will try it out in my code later but i have hurt my finger so it is hard to type :cry:

And i have to say tha the love comunity is pretty good :D

Sorry for late reply.

Re: my collision is bugging out and i cant't figure it out

Posted: Tue Oct 01, 2013 5:52 pm
by Ranguna259
Doing collision is going to be hard, I've tried and I gave up but if you can code a collision detection and action bug free then, if you can, share your code :megagrin:

If you can't then take a look at love.physics module for collisions

Re: my collision is bugging out and i cant't figure it out

Posted: Tue Oct 01, 2013 8:33 pm
by henrik9864
Yes i will share the code and i was alost at the giveup point myselves :3

and the love.physics module is great but i would rather make my own but that is just personal phreferance :awesome:

Re: my collision is bugging out and i cant't figure it out

Posted: Tue Oct 01, 2013 8:35 pm
by Ranguna259
Best of luck :)

Re: my collision is bugging out and i cant't figure it out

Posted: Mon Oct 07, 2013 5:08 pm
by henrik9864
okay a quick upadte :awesome:

I have been sic for the last couple of days and have not been able to do much coding :x and i am very sorry for people that have been vaiting for this scrips but im still figuring some bugs out but it works...just

But if you want the script now just pm me and i will send it to as fast as i can :awesome:

Henrik out

how do i uplode a file?