[Solved] -Collision- I can't get it to work!

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
Ximici
Prole
Posts: 13
Joined: Sun Apr 21, 2013 5:55 pm

[Solved] -Collision- I can't get it to work!

Post by Ximici »

Hello,

First of all, sorry for another collision question. I searched the forum for help but I could not figure it out. I know how collision detection should work and I wrote my own (messy?) code for (very basic) collision.
I made a huge if-statement for this to work so I could detect the collision, I worked in four steps:
ExixUuv.png
ExixUuv.png (7.83 KiB) Viewed 263 times
Why doesn't this work? Did I do something wrong?
And if it works, what must the response be? (to not let the player go trough blocks)
The collision code is in the collision.lua and the player code in the player.lua (obvious)
Also note that I am a beginner and the code is somewhat messy, sorry about that.
If you are going to say 'use a library', say one that works with the code I have written because I don't want to start all over again...

Thank you very much, I appreciate your help.
--Ximici
Attachments
platform.love
(116.57 KiB) Downloaded 186 times
Last edited by Ximici on Sat Nov 23, 2013 7:18 pm, edited 1 time in total.
User avatar
DaedalusYoung
Party member
Posts: 407
Joined: Sun Jul 14, 2013 8:04 pm

Re: -Collision- I can't get it to work!

Post by DaedalusYoung »

You only need one if statement:

Code: Select all

if x1 > x2 and x1 < x2 + w2 and y1> y2 and y1 < y2 + h2 then
    --collision
end
I've noticed Lua seems to prefer to process 'or's first and then 'and's, or the other way round, not sure, so for very long statements, often you think you're checking for "(a and b) or c", but Lua sees your code as "a and (b or c)". Or the other way round. I'm sure it has something to do with [this], but while I know how that works, I still can't fully comprehend it in large amounts. When in doubt, add parentheses. There's no ambiguity in "(a and b) or c", but it's not so clear in "a and b or c".
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: -Collision- I can't get it to work!

Post by Roland_Yonaba »

Ximici
Prole
Posts: 13
Joined: Sun Apr 21, 2013 5:55 pm

Re: -Collision- I can't get it to work!

Post by Ximici »

Hello,

I know about BoundingBox.lua. Doesn't work. I said I tested alot, that included BoundingBox.lua. Please, help, I don't know why it doesn't work.
I also tried:

Code: Select all

if x1 > x2 and x1 < x2 + w2 and y1> y2 and y1 < y2 + h2 then
    --collision
end
It doesn't work.

--Ximici
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: -Collision- I can't get it to work!

Post by Roland_Yonaba »

First of all, in collision.lua, the following is enough.

Code: Select all

function checkcollision(x1,y1,w1,h1,x2,y2,w2,h2)
  return x1 > x2 and x1 < x2 + w2 and y1> y2 and y1 < y2 + h2
end
Second, are you reffering to those blocks ?
0fhWTkXs.jpg
0fhWTkXs.jpg (1017 Bytes) Viewed 264 times
If yes, then, there is definitely a problem at line 37 in player.lua.

Code: Select all

print(checkcollision(0,488,32,32,player.x,player.y,player.width,player.height))
The first argument is supposed to be the x-coordinate of the upper-left BBox corner of the object the player must be colliding with (i.e, a block).
Judging from the position of those block, it cannot be 0. That's why checkcollision always returns false.
Ximici
Prole
Posts: 13
Joined: Sun Apr 21, 2013 5:55 pm

Re: -Collision- I can't get it to work!

Post by Ximici »

Thanks for the response! But I don't understand, sorry.
I implemented the code you suggested,

Code: Select all

function checkcollision(x1,y1,w1,h1,x2,y2,w2,h2)
  return x1 > x2 and x1 < x2 + w2 and y1> y2 and y1 < y2 + h2
end
I used the parameters you said:

Code: Select all

print(checkcollision(0,488,32,32,player.x,player.y,player.width,player.height))
But I still get the response "false".

What am I doing wrong?

--Ximici
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: -Collision- I can't get it to work!

Post by Roland_Yonaba »

Assuming that you have to objects. The first one is located at position x1,y1, and the second at position x2,y2 (upper-left corner coordinates).
w1, h1 are the width and height of the first object, and w2,h2 are the width and height of the second object.
The following code:

Code: Select all

function checkcollision(x1,y1,w1,h1,x2,y2,w2,h2)
  return x1 > x2 and x1 < x2 + w2 and y1> y2 and y1 < y2 + h2
end
checks if those objects overlaps.
So let's consider the player represents the second object. It means that player.x, player.y, player.width and player.height goes for x2, y2, w2, and h2.That's fine.

So, if you want the function to return the proper response, you should match x1,y1,w1 and h2 with the coordinates and with and height of the first object, which is supposed to be a block.

When writing:

Code: Select all

print(checkcollision(0,488,32,32,player.x,player.y,player.width,player.height))
It means you are testing a collision between a player and another object located at x1 = 0, y1 = 488, and having a width of 32 and a height of 32.
Question is, does the block really stand at that location? I do not think so, juding from its position on the screen.
So you might to fix those coordinates.
Ximici
Prole
Posts: 13
Joined: Sun Apr 21, 2013 5:55 pm

Re: -Collision- I can't get it to work!

Post by Ximici »

Thanks! I got it to work!
Post Reply

Who is online

Users browsing this forum: rigelloo and 0 guests