Skip to: Site menu | Main content


Welcome to PSP-Programming.com, a place for developers to get together.

Welcome to the forums. Here you can find other user tutorials as well as homebrew releases and the source code repository. You can also ask for help with your code here and post your own homebrew!

PSP-Programming.com Forums
February 10, 2012, 12:54:37 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length

News: Check out the Code Section!
Home Help Search Shop Login Register
Digg This!
Pages: [1]
Print
Author Topic: [Tutorial] Collision Detection - Bounding Boxes  (Read 8106 times)
SG57
Sr. Member
****

Karma: +7/-37
Offline Offline

Posts: 474
1140.80 points

View Inventory
Send Money to SG57


View Profile
« on: June 02, 2006, 11:59:55 PM »

Well, someone asked me for it so...  I made one, Im not requesting this as a tutorial in the section since it isn't long enough to match the others  Embarassed   sorry about that...

Anyway, this tutorial was made by me (SG57) and it will help you set up bounding boxes for collision... next up are bounding circles...

************************Collision Detection: *******************
  \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\Bounding Boxes/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\

_______________________C_R_E_A_T_E_D__B_Y_:_________________
-----------------------------------------~~ SG57 ~~-----------------------------
 
Since the PSP just draws pixels on the screen to make the images, it doesn’t know or care where they are in relation to each other, but as a programmer, you need to be able to tell if two or more objects collide.  This may seem like a simple concept but you will see that it is more complicated than you think.  In order to detect a collision you need to check if any part of one object is in the same position as any part of another object.  In order to find this, an if-statement will work, but it will be very complicated and long.  In the following code we will check to see if two pixels are colliding, the simplest of cases.
Code:

If( pixel1x == pixel2x && pixel1y == pixel2y )
// do something


Here we have used two pixels (pixel1 and pixel2) and we have simply checked to see if they are both in the same position.  Notice that we first test the x position and only if they are the same we test the y coordinates.  Only if the y coordinates are the same AND (indicated by the && operator) the x coordinates are the same is the if-statement true.
This is fine for pixels, but how many images are pixels? Not many.  The next simplest image to check for collisions is the rectangular images.
Code:

If( pixel1x >= rectangle1x && pixel1x <= rectangle1x + rectangle1width && pixel1y >= rectangle1y && pixel1y <= rectangle1y + rectangle1height )
// do something


This block of code tests to see if a pixel collides in a rectangle.  Now that we have a range to work in we must not only switch to > and < instead of ==, but we must also have more parts to the if-statement.  This is getting complicated now, but there is another situation we need to know about.  If two rectangles collide the statement gets even longer:
Code:

If( rectangle1x >= rectangle2x – rectangle1width && rectangle1x <= rectangle2x + rectangle2width && rectangle1y >= rectangle2y - rectangle1height && rectangle1y <= rectangle2y + rectangle2height)


Confused yet?  Just be glad we are only working within two dimensions.  This gets even longer in 3D, but we won’t go into that here.  In fact, this is as complicated as we are going to get for the purposes of this lesson, but it is possible to test collisions on lines, circles, ellipses, and any other regular shape using equations.  Notice, however, that we subtracted the width of rectangle1 from rectangle2x.  This works because as the right-edge of the rectangle collides with the left edge of the other rectangle, the x-coordinate of rectangle1 is at the x-position of rectangle2 minus the width of rectangle1.

And for all those lazy programmers out there who don't want to go through and calculate all this raw collision detection, I have written a prototype (Ravine too) that all you need to input are the parameters...  Here is my 'Home Made'  Doxygen and function:
Code:

/*
   1ST IMAGE FOR COLLISION
   
   x1 - X-Co-ordinate of 1st image
   y1 - Y-Co-ordinate of 1st image
   w1 - Width of 1st image
   h1 - Height of 1st image
   
   2ND IMAGE FOR COLLISION
   
   x2 - X-Co-ordinate of 2nd image
   y2 - Y-Co-ordinate of 2nd image
   w2 - Width of 2nd image
   h2 - Height of 2nd image
*/
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;

}

This function I havent used in about 6 months... It's from shear memory, so if there is something off, just let me know and ill fix it...  

Anway, this function will check all possible forms of collision when having all 8 values needed (4 per image: X co-ordinate, Y co-ordinate, Width of image, Height of image).  This function ,obviously, return a value... that value is: 1 = Collision Detected; !1 = No Collidion Detected

Here is an example I have just thrown togeather that will print "Collision Detected" to the screen when returning 1, and "No Collision Detected" when returning != 1.

Taken from my Kitten Cannon Teaser Demos (Ive abandoned the game, so if you want my source, feel free to ask)
Code:

int catx=0, caty = 0, w1 = 32, h1 = 32, explox = 430, exploy = 222, w2 = 50, h2 = 50, cat_collided = 0;
if(CollisionDetection(catx, caty, w1, h1, explox, exploy, w2, h2)) {
                    cat_collided = 1;
} else if(cat_collided==1) {
                    printTextCentered(134, "Collision Detected with Fireball");
} else {
                    printTextCentered(134, "Collision with Fireball isnt Detected yet");
}
Now of course, there are many ways to go about doing this...  I for one, like bools and such, so I set up a check to see if the 'cat' had made contact with the 'fireball', and if so, then print the string to the screen...  If not detected, it by defualt printed it wasnt detected...  I also had more things happening, but this is just an example again... (I had the score be manipulated and such...)


Here is the tutorial in .TXT form for anyone wanting it 'on-the-go':
http://www.psp-programming.com/downloads/files/Collision_Tutorial.txt

...Just trying to do all I can for the scene you guyz Embarassed ...
Logged


harleyg
Give miinaturvat Points!
Full Member
***

Karma: +11/-14
Offline Offline

Posts: 231
0.00 points

View Inventory
Send Money to harleyg

View Profile
« Reply #1 on: June 03, 2006, 01:41:24 AM »

thanks alot SG57, ill have a proper read over it later Wink
Logged
SG57
Sr. Member
****

Karma: +7/-37
Offline Offline

Posts: 474
1140.80 points

View Inventory
Send Money to SG57


View Profile
« Reply #2 on: June 03, 2006, 02:25:27 AM »

ok, i told you id make you guyz a tut...

What would you grade it?  1-10  Embarassed   its not long enough...
Logged
SG57
Sr. Member
****

Karma: +7/-37
Offline Offline

Posts: 474
1140.80 points

View Inventory
Send Money to SG57


View Profile
« Reply #3 on: June 03, 2006, 04:34:10 AM »

Do you think people will even read it?   Its been out for about 5 hours...
Logged
SG57
Sr. Member
****

Karma: +7/-37
Offline Offline

Posts: 474
1140.80 points

View Inventory
Send Money to SG57


View Profile
« Reply #4 on: June 03, 2006, 05:25:58 AM »

lol...  I kinda of do...  You basically said to re-enact this tutorial, and yes its possible and very easy, you can do it!

You need atleast 4 variables:
1st Images X Co-ordinate Integer Variable
1st Images Y Co-ordinate Integer Variable
2nd Images X Co-ordinate Integer Variable
2nd Images Y Co-ordinate Integer Variable

Then just place there Dimensions in the appropriate spots and you have collision detection between those 2 images.
Logged
soccerpmn
Newbie
*

Karma: +0/-0
Offline Offline

Posts: 33
0.00 points

View Inventory
Send Money to soccerpmn

View Profile WWW
« Reply #5 on: June 03, 2006, 07:29:36 AM »

I'd give it an... 7/10, I love how it is short and to the point, but I'd like it more if it had a small sample program source that you could compile, then test to see if you want to use the functions it does.  Also, I'd like the source to your Kitten Cannon SG57 because I know you have a lot of graphics stuff in there, and I've only recently gotten the libpng and started using it! Embarassed
Logged

DiabloTerrorGF
Full Member
***

Karma: +0/-11
Offline Offline

Posts: 101
637.80 points

View Inventory
Send Money to DiabloTerrorGF


View Profile WWW
« Reply #6 on: June 03, 2006, 08:11:29 AM »

heh, I'm making one that does it 9 points(think of a number pad) so odd shapes like a T can be used without making 2 entities of rectangles.

Rect1=T, Rect2=b

Collision(Rect1, Rect2, 1,1,1,0,1,0,0,1,0,1,0,0,1,1,1,1,1,1) //First 9 is for the "points" on Rect1, the second 9 is for Rect2.

1-1-1
0-1-0
0-1-0

Get the idea? It's early beta and needs a hell of alot more optimization(mostly the attributes for the Collision could be used a lot better, like an array...)
Logged

SG57
Sr. Member
****

Karma: +7/-37
Offline Offline

Posts: 474
1140.80 points

View Inventory
Send Money to SG57


View Profile
« Reply #7 on: June 03, 2006, 03:43:15 PM »

ok...  If you want an example, the Kitten Cannon Fireball Demo thing has this exact collision...  And if it returns true, then it says on the screen, collision detected...  but anyawy, my Kitten Cannon source is very old (to me) and un-commented and has alot of external libs and has alot of repeditive callings (pspDebugInit and stuffs) so Ill post it, but I doubt youll get any of it really...  It jsut has a menu, credits, options menu, and in the regular game, when fired, the cat starts bouncing, but doesnt shoot out before it does just yet,and in the Debug, i have no idea what is in there...  Oh but there is a really good MP3 Player example in there....  Oh well, i guess ill post it up in hope of someoneelse to complete it...

Oh and ill make an example for you guyz if you wish...
Logged
Pages: [1]
Print
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Page created in 0.217 seconds with 32 queries.
Sister Sites: Guitar Hero 4   BrokeniTouch.com