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.
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 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
my collision is bugging out and i cant't figure it out
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
-
- Prole
- Posts: 11
- Joined: Mon Sep 02, 2013 3:58 pm
- 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
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
-
- Prole
- Posts: 11
- Joined: Mon Sep 02, 2013 3:58 pm
Re: my collision is bugging out and i cant't figure it out
thanks
im new to love
im new to love
Re: my collision is bugging out and i cant't figure it out
Hi and welcome to the LÖVE community.
Here is a suggestion to improve your code.
Replace this:
by this:
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).
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
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
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).
Check out my blog on gamedev
-
- Prole
- Posts: 11
- Joined: Mon Sep 02, 2013 3:58 pm
Re: my collision is bugging out and i cant't figure it out
Thanks micha for helping out
I did not know that 4 conditions was enough but when i tried drawing it on a piece of paper i saw it
I will try it out in my code later but i have hurt my finger so it is hard to type
And i have to say tha the love comunity is pretty good
Sorry for late reply.
I did not know that 4 conditions was enough but when i tried drawing it on a piece of paper i saw it
I will try it out in my code later but i have hurt my finger so it is hard to type
And i have to say tha the love comunity is pretty good
Sorry for late reply.
- 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
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
If you can't then take a look at love.physics module for collisions
If you can't then take a look at love.physics module for collisions
-
- Prole
- Posts: 11
- Joined: Mon Sep 02, 2013 3:58 pm
Re: my collision is bugging out and i cant't figure it out
Yes i will share the code and i was alost at the giveup point myselves
and the love.physics module is great but i would rather make my own but that is just personal phreferance
and the love.physics module is great but i would rather make my own but that is just personal phreferance
- Ranguna259
- Party member
- Posts: 911
- Joined: Tue Jun 18, 2013 10:58 pm
- Location: I'm right next to you
-
- Prole
- Posts: 11
- Joined: Mon Sep 02, 2013 3:58 pm
Re: my collision is bugging out and i cant't figure it out
okay a quick upadte
I have been sic for the last couple of days and have not been able to do much coding 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
Henrik out
how do i uplode a file?
I have been sic for the last couple of days and have not been able to do much coding 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
Henrik out
how do i uplode a file?
Who is online
Users browsing this forum: Bing [Bot] and 1 guest