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

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
henrik9864
Prole
Posts: 11
Joined: Mon Sep 02, 2013 3:58 pm

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

Post 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:
User avatar
DaedalusYoung
Party member
Posts: 413
Joined: Sun Jul 14, 2013 8:04 pm

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

Post 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
henrik9864
Prole
Posts: 11
Joined: Mon Sep 02, 2013 3:58 pm

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

Post by henrik9864 »

thanks :3
im new to love
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

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

Post 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).
henrik9864
Prole
Posts: 11
Joined: Mon Sep 02, 2013 3:58 pm

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

Post 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.
User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

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

Post 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
LoveDebug- A library that will help you debug your game with an on-screen fully interactive lua console, you can even do code hotswapping :D

Check out my twitter.
henrik9864
Prole
Posts: 11
Joined: Mon Sep 02, 2013 3:58 pm

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

Post 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:
User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

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

Post by Ranguna259 »

Best of luck :)
LoveDebug- A library that will help you debug your game with an on-screen fully interactive lua console, you can even do code hotswapping :D

Check out my twitter.
henrik9864
Prole
Posts: 11
Joined: Mon Sep 02, 2013 3:58 pm

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

Post 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?
Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests