The above video goes away if you are a member and logged in, so log in now!

 Would you like to get all the newest Gaming News fromQJ.NET in your email each day? Want to learn more about the team who brings you the QJ news?

## LUA - Collision Detection ~ Bounding Boxes

This is a discussion on LUA - Collision Detection ~ Bounding Boxes within the PSP Development Forum forums, part of the PSP Development, Hacks, and Homebrew category; Well, over at psp-programming.com, i put a collision detection function that would take 8 parameters and return whether or not ...

 Tweet
1. ## LUA - Collision Detection ~ Bounding Boxes

Well, over at psp-programming.com, i put a collision detection function that would take 8 parameters and return whether or not the 2 images that were basically created out of those parameters, collided...

So out of some requests, here it is converted to LUA from C:

In C:
Code:
```// Based of Ravines
extern int CollisionDetection(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2) {
int Collide = 0;
if ((y2 >= y1 && y1 + h1 >= y2) || (y2 + h2 >= y1 && y1 + h1 >= y2 + h2) || (y1 >= y2 && y2 + h2 >= y1) || (y1 + h1 >= y2 && y2 + h2 >= y1 + h1))
{
if (x2 >= x1 && x1 + w1 >= x2) {
Collide = 1;
}
if (x2 + w2 >= x1 && x1 + w1 >= x2 + w2) {
Collide = 1;
}
if (x1 >= x2 && x2 + w2 >= x1) {
Collide = 1;
}
if (x1 + w1 >= x2 && x2 + w2 >= x1 + w1) {
Collide = 1;
}
}
return Collide;
}```
In LUA:
Code:
```function CollisionDetection(x1, y1, w1, h1, x2, y2, w2, h2) --start function
Collide = 0
if (y2 >= y1 and y1 + h1 >= y2) or (y2 + h2 >= y1 and y1 + h1 >= y2 + h2) or (y1 >= y2 and y2 + h2 >= y1) or (y1 + h1 >= y2 and y2 + h2 >= y1 + h1) then
if x2 >= x1 and x1 + w1 >= x2 then					 -- corner 1
Collide = 1
end
if x2 + w2 >= x1 and x1 + w1 >= x2 + w2 then	 -- corner 2
Collide = 1
end
if x1 >= x2 and x2 + w2 >= x1 then					 -- corner 3
Collide = 1
end
if x1 + w1 >= x2 and x2 + w2 >= x1 + w1 then	 -- corner 4
Collide = 1
end
end
return Collide -- return whether or not collision is detected
end					 -- end function```
Ok... The above is C to LUA conversion...

The syntax of this function usage is:

1. x1 ~ X Co-ordinate of the 1st object/image
2. y1 ~ Y Co-ordinate of the 1st object/image
3. w1 ~ Width of the 1st object/image
4. h1 ~ Height of the 1st object/image
5. ---------------------------------------------
6. x2 ~ X Co-ordinate of the 2nd image/object
7. y2 ~ Y Co-ordinate of the 2nd image/object
8. w2 ~ Width of the 2nd image/object
9. h2 ~ Height of the 2nd image/object
10. ---------------------------------------------
11. Returns 1: Collision is detected between the 2 objects/images
12. Returns 0; Collision ISNT detected between the 2 objects/images
Well, there you have it... Now the part that some may or may not want to watch:

The Example:
Code:
```1string = "Collision Detected!"
2string = "No Collision Detected"

object1 = { x=0, y=0, w=string.len(1string)*7, h=10 } -- 7 = 1 character width in pixels

object2 = { x=240, y=0, w=string.len(2string)*7, h=10 }

while true do
object2.x = object2.x + 1
object1.x = object1.x + 2

if CollisionDetection(object1.x, object1.y, object1.w, object1.h, object2.x, object2.y, object2.w, object2.h) == 1 then
screen:print(object1.x, object1.y, 1string, Color.new(255,255,255))
screen:print(object2.x, object2.y, 1string, Color.new(255,255,255))
else
screen:print(object1.x, object1.y, 2string, Color.new(125,125,125))
screen:print(object2.x, object2.y, 2string, Color.new(125,125,125))
end
end
end```
Well.. There you have it... This here should print out whehter or not the strings have collided, via the strings...

Hope this has helped somebody?

~sg57

2. Thanks for the tutorial. My entire game was being held up because I couldn't make anything collide.

3. Exellent SG57. I needed the C collision. Didnt realise you posted it over at psp-programming.com

4. Yup, and the C collision is here now too... I also have showed some LUA users how LUA and C differ, but only by syntax...

How '}' = end
How '{' = then

Maybe some might convert on over, but oh well... As long as people can now have collision in there games on both C and LUA platforms of languages... (C++ I dont know how to convert to...)

5. Heh, thanks for the credit. Also, nice conversion to LUA.

6. I know, i said the C was/is based off you... About 5 months back or so was the last time i used that... Ive converted it to LUA before too, just not as its own thread though...

ohwell, onto bigger and better things then some bounding boxes...

7. Here's an easy way to find collision between circles, or a circle and a point.

Code:
```radius = circle1Radius + circle2Radius         -- circle2Radius is only needed with 2 circles
distance = SquareRootOfFunction( (x1 - x2)*(x1 - x2) + (y1 - y2)*(y1-y2) )

if (distance <= radius ) Then -- We have collision!```
That's not really aimed toward any specific language. The x1's and y1's are the center of the first circle and the x2's and y2's are either the center of the second circle, or the point.

8. Well.. Ill mess around with that, but i dont really see a use for bounding circles unless the imae you have is a circle (play button in my media app...)Ill mess around with it tomorrow

9. I think this would fit in this thread.
It is not totally mine, i just made it a little bit easier to detect the width and height of the two images.

Code:
```function collision(image1, x1, y1, image2, x2, y2)
w1 = image1:width()
h1 = image1:height()
w2 = image2:width()
h2 = image2:height()
if (y2 >= y1 and y1 + h1 >= y2) or (y2 + h2 >= y1 and y1 + h1 >= y2 + h2) or (y1 >= y2 and y2 + h2 >= y1) or (y1 + h1 >= y2 and y2 + h2 >= y1 + h1) then
if x2 >= x1 and x1 + w1 >= x2 then
return true
elseif x2 + w2 >= x1 and x1 + w1 >= x2 + w2 then
return true
elseif x1 >= x2 and x2 + w2 >= x1 then
return true
elseif x1 + w1 >= x2 and x2 + w2 >= x1 + w1 then
return true
end
end

return false
end```

10. this should be stickied ;) tthanks for code

11. ;) I wouldnt mind a sticky....

Like make 1 sticky that says: All LUA Material Here

or something then have links to all the LUA tutorials and information, thus clearing up the sticky area... Oh and have links via C programming...

Oh and that new ocllision function you created isnt really the best collision function since it IS only IMAGE collision related...

Look at my example, see how it uses collision for text, which can be classified as an object, along with an image, but your new function isn IMAGE specific, thus making it less effective for people who want to use the same function, with both objects and iamges...

Nice try by the way though.

13. Sweet, didnt know something like bounding boxes collision port from C to LUA is worth teh privelage of THE GUIDE ;)

14. this is good if everything in your game has 4 straight sides :)

15. Well, all images are saved as something with 4 sides/verticies, but if your image happens to be a circlular form more then that of a square.rectangle, then use Bounding Circles

16. I found this post by fchaos in the developers dungueon:
Lua Code Snippets

Its exactly your code (well not exactly since you only return Collide at the end) and its from 12-27-2005. I dont know if you knew this or not, just wanted to show people they should check in the LUA snippets a little more often.

17. I'm making a rather simple game in C and used your collision detection in it. it worked perfectly for the player collision. but now I'm trying to use it for enemy's colliding and just can't get it to work.

curwall & curenemy is the variable for which object it's checking and amountwall & amountenemy is the variable for how many enemys and walls there is on the level.

on the level that I'm making it's currenly 5 walls and 2 enemies.

but the enemy that collide with the wall just go right through the wall.

any help would be appriciated.
Code:
```               curwall = 0;
curenemy = 0;
while(curwall < amountwall) {
while(curenemy < amountenemy) {
if ((wally[curwall] >= enemyy[curenemy] && enemyy[curenemy] + 32 >= wally[curwall]) || (wally[curwall] + 32 >= enemyy[curenemy] && enemyy[curenemy] + 32 >= wally[curwall] + 32) || (enemyy[curenemy] >= wally[curwall] && wally[curwall] + 32 >= enemyy[curenemy]) || (enemyy[curenemy] + 32 >= wally[curwall] && wally[curwall] + 32 >= enemyy[curenemy] + 32))
{
if (wallx[curwall] >= enemyx[curenemy] && enemyx[curenemy] + 32 >= wallx[curwall]) {
if(enemydir[curenemy] == 0) { enemydir[curenemy] = 1; }
else if(enemydir[curenemy] == 1) { enemydir[curenemy] = 0; }
else if(enemydir[curenemy] == 2) { enemydir[curenemy] = 3; }
else if(enemydir[curenemy] == 3) { enemydir[curenemy] = 2; }
}
if (wallx[curwall] + 32 >= enemyx[curenemy] && enemyx[curenemy] + 32 >= wallx[curwall] + 32) {
if(enemydir[curenemy] == 0) { enemydir[curenemy] = 1; }
else if(enemydir[curenemy] == 1) { enemydir[curenemy] = 0; }
else if(enemydir[curenemy] == 2) { enemydir[curenemy] = 3; }
else if(enemydir[curenemy] == 3) { enemydir[curenemy] = 2; }
}
if (enemyx[curenemy] >= wallx[curwall] && wallx[curwall] + 32 >= enemyx[curenemy]) {
if(enemydir[curenemy] == 0) { enemydir[curenemy] = 1; }
else if(enemydir[curenemy] == 1) { enemydir[curenemy] = 0; }
else if(enemydir[curenemy] == 2) { enemydir[curenemy] = 3; }
else if(enemydir[curenemy] == 3) { enemydir[curenemy] = 2; }
}
if (enemyx[curenemy] + 32 >= wallx[curwall] && wallx[curwall] + 32 >= enemyx[curenemy] + 32) {
if(enemydir[curenemy] == 0) { enemydir[curenemy] = 1; }
else if(enemydir[curenemy] == 1) { enemydir[curenemy] = 0; }
else if(enemydir[curenemy] == 2) { enemydir[curenemy] = 3; }
else if(enemydir[curenemy] == 3) { enemydir[curenemy] = 2; }
}
}
curenemy++;
}
curwall++;
}```

18. I didnt even know that, but i do know ive converted this before back in July 2005 but not in its own thread, maybe he just publicized it or sumthin but i dunno...

Oh and its different because he didnt have to make true and false varaibales, you could easily remove the collision, replace with true and false.

19. Originally Posted by waterbottle
I'm making a rather simple game in C and used your collision detection in it. it worked perfectly for the player collision. but now I'm trying to use it for enemy's colliding and just can't get it to work.

curwall & curenemy is the variable for which object it's checking and amountwall & amountenemy is the variable for how many enemys and walls there is on the level.

on the level that I'm making it's currenly 5 walls and 2 enemies.

but the enemy that collide with the wall just go right through the wall.

any help would be appriciated.
Code:
```               curwall = 0;
curenemy = 0;
while(curwall < amountwall) {
while(curenemy < amountenemy) {
if ((wally[curwall] >= enemyy[curenemy] && enemyy[curenemy] + 32 >= wally[curwall]) || (wally[curwall] + 32 >= enemyy[curenemy] && enemyy[curenemy] + 32 >= wally[curwall] + 32) || (enemyy[curenemy] >= wally[curwall] && wally[curwall] + 32 >= enemyy[curenemy]) || (enemyy[curenemy] + 32 >= wally[curwall] && wally[curwall] + 32 >= enemyy[curenemy] + 32))
{
if (wallx[curwall] >= enemyx[curenemy] && enemyx[curenemy] + 32 >= wallx[curwall]) {
if(enemydir[curenemy] == 0) { enemydir[curenemy] = 1; }
else if(enemydir[curenemy] == 1) { enemydir[curenemy] = 0; }
else if(enemydir[curenemy] == 2) { enemydir[curenemy] = 3; }
else if(enemydir[curenemy] == 3) { enemydir[curenemy] = 2; }
}
if (wallx[curwall] + 32 >= enemyx[curenemy] && enemyx[curenemy] + 32 >= wallx[curwall] + 32) {
if(enemydir[curenemy] == 0) { enemydir[curenemy] = 1; }
else if(enemydir[curenemy] == 1) { enemydir[curenemy] = 0; }
else if(enemydir[curenemy] == 2) { enemydir[curenemy] = 3; }
else if(enemydir[curenemy] == 3) { enemydir[curenemy] = 2; }
}
if (enemyx[curenemy] >= wallx[curwall] && wallx[curwall] + 32 >= enemyx[curenemy]) {
if(enemydir[curenemy] == 0) { enemydir[curenemy] = 1; }
else if(enemydir[curenemy] == 1) { enemydir[curenemy] = 0; }
else if(enemydir[curenemy] == 2) { enemydir[curenemy] = 3; }
else if(enemydir[curenemy] == 3) { enemydir[curenemy] = 2; }
}
if (enemyx[curenemy] + 32 >= wallx[curwall] && wallx[curwall] + 32 >= enemyx[curenemy] + 32) {
if(enemydir[curenemy] == 0) { enemydir[curenemy] = 1; }
else if(enemydir[curenemy] == 1) { enemydir[curenemy] = 0; }
else if(enemydir[curenemy] == 2) { enemydir[curenemy] = 3; }
else if(enemydir[curenemy] == 3) { enemydir[curenemy] = 2; }
}
}
curenemy++;
}
curwall++;
}```
anyone?

I've been trying for hours to find out what's wrong with it

20. Originally Posted by SG57
Sweet, didnt know something like bounding boxes collision port from C to LUA is worth teh privelage of THE GUIDE ;)
Anything that's a guide is added, no matter the "worthiness". This way, I can go to programming questions
Originally Posted by What I can say
go to the GUIDE, under programming, there's like 30 guides. knock yourself out.

21. Originally Posted by waterbottle
anyone?

I've been trying for hours to find out what's wrong with it
Well... Your doing the function manually, just copy & paste my function code in your script somewhere, call CollisionDetection(enter both X, Y, width and height of the 2 images to check for collision here) It saves you time and helps alot... but make sure you say for if the collision IS true/collided with the 2 images, it retuns 1, so if(CollisionDetection(the X, Y, width and height of both images)==1 then Images have collided.)

22. can it detect from wich side it is hit

23. If you read my comments, it says corner 1, corner 2, etc. which is basically saying that if the 2 cornes touch at all,t hen collision is true...

24. Originally Posted by SG57
Well... Your doing the function manually, just copy & paste my function code in your script somewhere, call CollisionDetection(enter both X, Y, width and height of the 2 images to check for collision here) It saves you time and helps alot... but make sure you say for if the collision IS true/collided with the 2 images, it retuns 1, so if(CollisionDetection(the X, Y, width and height of both images)==1 then Images have collided.)
wow, that way is so much simpler :P

however it still dosn't work, could you (or somone else) take a look at the source and try to find the error? it's a bit messy (no notes frks.), but the solid part that dosn't work starts at line 304..

http://waterbottle.vworldv.com/main.c

Would be highly appriciated :)

25. hmmm... the collision function and parameters are right, maybe its just the codew inside of it.. Oh and might want to se your levelnum a value...

26. ## Quicker circle detect...

One quick note when detecting circle point (or circle-circle or circle-segment, circle-ray, etc.), you don't have to do the square root. Just square both sides.

I.e. test distance squared against radius squared.

Also, when doing collision, you almost always want a hierarchy of shapes, using quickest to compute first on down... I.e. if colliding two circles, first collide their bounding rects to see if you need to bother with the circle collision (only necessary if you are using the supa-fast bounding box detection which is computationally faster than the circle compare). If the box detection hits, then test circle, if circle hits then go down to pixel if you want.

Although, most of the time in 2d you can just get the possible collision area quickly (with something like the BB detection) and then do a bitwise mask calc for pixel perfect collision extremely cheaply... I'd quickly write it up, but it's been ages since I've done 2d only and I can no longer just bang it out from memory. :)

27. Originally Posted by Hardeyes
Also, when doing collision, you almost always want a hierarchy of shapes, using quickest to compute first on down... I.e. if colliding two circles, first collide their bounding rects to see if you need to bother with the circle collision (only necessary if you are using the supa-fast bounding box detection which is computationally faster than the circle compare). If the box detection hits, then test circle, if circle hits then go down to pixel if you want.
Are you sure? Wouldn't the four comparision checks cost more in branching then the one in circle collision checking?

Unless you are thinking of a different BB collision algorithm.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

All times are GMT -8. The time now is 11:14 PM.

Use of this Web site constitutes acceptance of the TERMS & CONDITIONS and PRIVACY POLICY