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:
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
[Solved] -Collision- I can't get it to work!
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
[Solved] -Collision- I can't get it to work!
- Attachments
-
- platform.love
- (116.57 KiB) Downloaded 213 times
Last edited by Ximici on Sat Nov 23, 2013 7:18 pm, edited 1 time in total.
- DaedalusYoung
- Party member
- Posts: 413
- Joined: Sun Jul 14, 2013 8:04 pm
Re: -Collision- I can't get it to work!
You only need one if statement:
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".
Code: Select all
if x1 > x2 and x1 < x2 + w2 and y1> y2 and y1 < y2 + h2 then
--collision
end
- 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!
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:
It doesn't work.
--Ximici
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
--Ximici
- 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!
First of all, in collision.lua, the following is enough.
Second, are you reffering to those blocks ?
If yes, then, there is definitely a problem at line 37 in player.lua.
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.
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
Code: Select all
print(checkcollision(0,488,32,32,player.x,player.y,player.width,player.height))
Judging from the position of those block, it cannot be 0. That's why checkcollision always returns false.
Re: -Collision- I can't get it to work!
Thanks for the response! But I don't understand, sorry.
I implemented the code you suggested,
I used the parameters you said:
But I still get the response "false".
What am I doing wrong?
--Ximici
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
Code: Select all
print(checkcollision(0,488,32,32,player.x,player.y,player.width,player.height))
What am I doing wrong?
--Ximici
- 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!
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:
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:
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.
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
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))
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.
Re: -Collision- I can't get it to work!
Thanks! I got it to work!
Who is online
Users browsing this forum: Bing [Bot] and 9 guests