View Single Post
 
Old 04-09-2007, 07:14 PM
PSdonkey's Avatar
PSdonkey PSdonkey is offline
Squirrel Admin
PSP Titan
 
Join Date: Mar 2006
Posts: 9,117
PSdonkey has a reputation beyond reputePSdonkey has a reputation beyond reputePSdonkey has a reputation beyond reputePSdonkey has a reputation beyond reputePSdonkey has a reputation beyond reputePSdonkey has a reputation beyond reputePSdonkey has a reputation beyond reputePSdonkey has a reputation beyond reputePSdonkey has a reputation beyond reputePSdonkey has a reputation beyond reputePSdonkey has a reputation beyond repute
Default

I have seen a lot of people request a lesson in collisions and a good collision function. Just to let everyone know, collision detection is system independent. That means that you do not need OSlib or any other library to create collisions for you. it is simple C or C++ code that can make collision detection.
Since people still ask me for a collision function, I made this collision function using OSlib for everyone to use. This function will detect if 2 or more images collide with each other from any angle.

Code:
int collision(OSL_IMAGE *img1,float img1posX, float img1posY, OSL_IMAGE *img2, float img2posX, float img2posY ) 
{
   int collision;
   collision = 0;
   float img1width  = img1->stretchX;
   float img1height = img1->stretchY;
   float img2width  = img2->stretchX;
   float img2height = img2->stretchY;
   
   if ((img1posX + img1width > img2posX) &&
       (img1posX < img2posX + img2width) &&
       (img1posY + img1height > img2posY) &&
       (img1posY < img2posY + img2height) ) 
{
         collision = 1;               
   }     
   return collision;
}
These are the parameters of the function:

(OSL_IMAGE *img1,float img1posX, float img1posY, OSL_IMAGE *img2, float img2posX, float img2posY )

The function takes 2 images named img1 and img2 and takes their x and y coordinates of each image wherever they are on the screen.

Here is the most important part of the function:

if ((img1posX + img1width > img2posX) &&
(img1posX < img2posX + img2width) &&
(img1posY + img1height > img2posY) &&
(img1posY < img2posY + img2height) )

Those are the conditions of whether or not collision has occurred between the 2 images. Study the if statements and try to understand how all the conditions must be met for collision to happen. If collision occurs between the 2 images, the function will return a value of 1. If no collision is happening at the moment, the function will be returning a value of 0.
If you would like to add collision detection between more then 2 images, then just change the parameters of the function accordingly.
__________________
Want to become a PSP Dev the easy way? Check out my tutorial for the PSP here www.psp-hacks.com/forums/viewtopic.php?id=65403
Want to learn how to create your own games on the PSP? Check out my tutorial here http://www.psp-hacks.com/forums/view...693884#p693884
Want to learn how to program C++ ? Check out that tutorial here www.psp-hacks.com/forums/viewtopic.php?id=28694
Reply With Quote