need some help please *beginer*

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
cruisx
Posts: 8
Joined: Tue Mar 25, 2008 8:26 am

need some help please *beginer*

Post by cruisx »

ok i need some help guys. I am trying to lean how to do collision detection with 2 objects on the screen but i am kinda stuck, im sure the problem is an easy one, but for a beginner i am having some trouble.

Code: Select all

//Oslib header file
#include <oslib/oslib.h>

//This is needed for eboot
PSP_MODULE_INFO&#40;"OSLib Sample", 0, 1, 1&#41;;
PSP_MAIN_THREAD_ATTR&#40;THREAD_ATTR_USER | THREAD_ATTR_VFPU&#41;;

//this creates pointers for our images.
OSL_IMAGE *sprite, *sprite2, *sprite3;

//definations
#define DOWN 0
#define UP 35
#define RIGHT 70
#define LEFT 105

//variables
int sprite_position;
int sprite_march;

//declare the function like buttons and spriteAnimation
void Buttons&#40;&#41;;
void SpriteAnimate&#40;&#41;;
int collision&#40;OSL_IMAGE *img1,float img1posX, float img1posY, OSL_IMAGE *img2, float img2posX, float img2posY,OSL_IMAGE *img3, float img3posX, float img3posY &#41;;

int main&#40;&#41;
&#123;

//start the Oslib library
	oslInit&#40;0&#41;;
//start the graphixs mode
	oslInitGfx&#40;OSL_PF_8888, 1&#41;;

//sets the transparency color
	oslSetTransparentColor&#40;RGB&#40;255,0,255&#41;&#41;;

//loads the images into memory
	sprite = oslLoadImageFile&#40;"sprite.png", OSL_IN_RAM, OSL_PF_5551&#41;;
	sprite2 = oslLoadImageFile&#40;"sprite2.png", OSL_IN_RAM, OSL_PF_5551&#41;;
	sprite3 = oslLoadImageFile&#40;"sprite3.png", OSL_IN_RAM, OSL_PF_5551&#41;;

//Disables the transparent color so it cannot be used again
	oslDisableTransparentColor&#40;&#41;;

//sees if all files are avaiable
	if &#40;!sprite || !sprite2||!sprite3&#41;
		oslDebug&#40;"! one or more of the files were missing, the program cannot run"&#41;;
		
		
//Sets the sprites original position, lets see if i can get 2 sprites on da bloody screen
	sprite->x = 155;
	sprite->y = 95;
	sprite_position = DOWN;

//sets second sprite position
	sprite2->x = 230;
	sprite2->y = 130;
	
//sets thrid sprite pos
	sprite3->x = 270;
	sprite3->y = 130;


//main while loop
	while &#40;!osl_quit&#41;
	&#123;
		//to be able ot draw on the screen
			oslStartDrawing&#40;&#41;;
			
		//Buttons 
			Buttons&#40;&#41;;
			
		//draws a green background
			oslDrawGradientRect&#40;0,0,480,272,RGB&#40;0,128,0&#41;,RGB&#40;0,128,0&#41;, RGB&#40;128,255,128&#41;, RGB&#40;128,255,128&#41;&#41;; 
			
		//this draws the images to the screen
			oslDrawImage&#40;sprite&#41;;
			oslDrawImage&#40;sprite2&#41;;
			oslDrawImage&#40;sprite3&#41;;
			
		//stops the drawing mode
			oslEndDrawing&#40;&#41;;
			
		//sync the screen 
			oslSyncFrame&#40;&#41;;
		&#125;
		
		//terminate the program
		oslEndGfx&#40;&#41;;
		oslQuit&#40;&#41;;
		return 0;
	&#125;
	
		void Buttons&#40;&#41;
		&#123;
		//starts up the psp buttons
			oslReadKeys&#40;&#41;;
			
		if &#40;osl_keys->held.down&#41;
		&#123;
			if&#40;!collision&#40;sprite, sprite->x, sprite->y + 2, sprite2, sprite2->x, sprite2->y, sprite3, sprite3->x, sprite3->y &#41;&#41;
			&#123;
			//sprite movement
			sprite->y +=2;
			
			//sets sprite position on sprite sheet
				sprite_position = DOWN;
			
			//calls sprie animate
				SpriteAnimate&#40;&#41;;
		&#125;
		&#125;
				
			if &#40;osl_keys->held.up&#41;
		&#123;
			if&#40;!collision&#40;sprite, sprite->x, sprite->y - 2, sprite2, sprite2->x, sprite2->y,sprite3, sprite3->x, sprite3->y &#41;&#41;
			&#123;
				sprite->y-= 2;
				sprite_position = UP;
				SpriteAnimate&#40;&#41;;
			
		&#125;
		&#125;
		
	   if &#40;osl_keys->held.right&#41;
    &#123;
			if&#40;!collision&#40;sprite, sprite->x + 2, sprite->y, sprite2, sprite2->x, sprite2->y,sprite3, sprite3->x, sprite3->y  &#41;&#41;
			&#123;
        sprite->x += 2;
        sprite_position = RIGHT;
        SpriteAnimate&#40;&#41;;
    &#125;
	&#125;
			
		        if &#40;osl_keys->held.left&#41;
    &#123;
		if&#40;!collision&#40;sprite, sprite->x - 2, sprite->y, sprite2, sprite2->x, sprite2->y,sprite3, sprite3->x, sprite3->y  &#41;&#41;
		&#123;
        sprite->x -= 2;
        sprite_position = LEFT;
        SpriteAnimate&#40;&#41;;
    &#125;
	&#125;
			
			   //If a button is not pressed
    if &#40;!osl_keys->held.value&#41; 
    &#123;
    //Start the variable over for when a button is pressed again
        sprite_march = 0; 

        //Sets the sprite's direction
        oslSetImageTileSize&#40;sprite,0,sprite_position,22,35&#41;;
    &#125;
&#125;


void SpriteAnimate&#40;&#41;
   &#123;    
        //Moves the sprite in the row that it is in
        sprite_march++;

        //Moves the sprite constantly
        oslSetImageTileSize&#40;sprite,&#40;sprite_march * 22&#41;,sprite_position,22,35&#41;;

        //resets the sprite movement in that row
        if &#40;sprite_march == 6&#41; sprite_march = 0;
    
&#125;
			
		
		int collision&#40;OSL_IMAGE *img1,float img1posX, float img1posY, OSL_IMAGE *img2, float img2posX, float img2posY, OSL_IMAGE *img3, float img3posX, float img3posY &#41; 
&#123;
   int collision;
   collision = 0;
   float img1width  = img1->stretchX;
   float img1height = img1->stretchY;
   float img2width  = img2->stretchX;
   float img2height = img2->stretchY;
   float img3width = img3->stretchX;
   float img3height = img3->stretchY;
  
   
   if &#40;&#40;img1posX + img1width > img2posX&#41; &&
       &#40;img1posX < img2posX + img2width&#41; &&
	   &#40;img1posX + img1width > img3posX&#41; &&
       &#40;img1posX < img2posX + img3width&#41; &&
       &#40;img1posY + img1height > img2posY&#41; &&
       &#40;img1posY < img2posY + img2height&#41; &#41; 
&#123;
         collision = 1;               
   &#125;     
   return collision;
&#125;
I know where the problem is but i dont know what im doing wrong

Code: Select all

if &#40;&#40;img1posX + img1width > img2posX&#41; &&
       &#40;img1posX < img2posX + img2width&#41; &&
	  &#91;B&#93; &#40;img1posX + img1width > img3posX&#41; &&
       &#40;img1posX < img2posX + img3width&#41; &&&#91;/B&#93;
       &#40;img1posY + img1height > img2posY&#41; &&
       &#40;img1posY < img2posY + img2height&#41; &#41; 
&#123;

if i put that in, none of the sprites have collision but if i take out the Bolded parts the first sprite has collision but the second one dose not.(second sprite as in the second object, not the character that is moving). How do i modify this correctly for every new object on the screen? help will be greatly appreciated.
User avatar
jean
Posts: 489
Joined: Sat Jan 05, 2008 2:44 am

Post by jean »

Just a few hints:
1) Collision is to be performed between two entities at a time.
2) Collision is generally not a commutative task because speed and acceleration are taken into account *BUT* In your case it's simple and commutative, so be sure not to check the same two times.
3) Write a single function that checks if a point is inside a shape and reuse it everytime you need to.

i.e.

Code: Select all

for each shape
   for each vertex of current shape
        check if the vertex is inside any other shape
              if so, append a "collision detected between a and b" flag into a list

  parse the list and for each collision detected, act accordingly
PosX100
Posts: 98
Joined: Wed Aug 15, 2007 1:02 am

Post by PosX100 »

So , you're not making a tile-based game?

Well , then , search on google how to perform bounding box collision.

Its really easy.


The calculation is simple , you just need to check if x1,x1+w1,y1,y1+h1 ,
x2,x2+w2,y2,y2+h2 collide....

w=width
h=height
x=x position
y=y position


In games , bounding box collision usually used before performing any other type collision(eg :pixel/and any other complex collision formula) to save up cpu , but for a very simple game(like a shooter for example) it should be enough..


[EDIT]
Hey, wait..you already have a collision detection function in your code

Code: Select all

    int collision&#40;OSL_IMAGE *img1,float img1posX, float img1posY, OSL_IMAGE *img2, float img2posX, float img2posY, OSL_IMAGE *img3, float img3posX, float img3posY &#41;
But you're doing a big mistake.

Why you're checking 3 objects?
You should check 2 objects at a time.

So , this should be enough:

Code: Select all

int collision&#40;OSL_IMAGE *img1,float img1posX, float img1posY, OSL_IMAGE *img2, float img2posX, float img2posY&#41;
&#123;
  //Its better to use a pointer to the actual struct/class member variable..
  //you can also use pointer reference for the images...
   float * img1width  = &img1->stretchX,img1height = &img1->stretchY,
   img2width  = &img2->stretchX,img2height = &img2->stretchY;

   return &#40;&#40;img1posX + img1width > img2posX&#41; &&
       &#40;img1posX < img2posX + img2width&#41; &&
       &#40;img1posY + img1height > img2posY&#41; &&
       &#40;img1posY < img2posY + img2height&#41; &#41;&#41;;
&#125;
cruisx
Posts: 8
Joined: Tue Mar 25, 2008 8:26 am

Post by cruisx »

PosX100 wrote:So , you're not making a tile-based game?

Well , then , search on google how to perform bounding box collision.

Its really easy.


The calculation is simple , you just need to check if x1,x1+w1,y1,y1+h1 ,
x2,x2+w2,y2,y2+h2 collide....

w=width
h=height
x=x position
y=y position


In games , bounding box collision usually used before performing any other type collision(eg :pixel/and any other complex collision formula) to save up cpu , but for a very simple game(like a shooter for example) it should be enough..


[EDIT]
Hey, wait..you already have a collision detection function in your code

Code: Select all

    int collision&#40;OSL_IMAGE *img1,float img1posX, float img1posY, OSL_IMAGE *img2, float img2posX, float img2posY, OSL_IMAGE *img3, float img3posX, float img3posY &#41;
But you're doing a big mistake.

Why you're checking 3 objects?
You should check 2 objects at a time.

So , this should be enough:

Code: Select all

int collision&#40;OSL_IMAGE *img1,float img1posX, float img1posY, OSL_IMAGE *img2, float img2posX, float img2posY&#41;
&#123;
  //Its better to use a pointer to the actual struct/class member variable..
  //you can also use pointer reference for the images...
   float * img1width  = &img1->stretchX,img1height = &img1->stretchY,
   img2width  = &img2->stretchX,img2height = &img2->stretchY;

   return &#40;&#40;img1posX + img1width > img2posX&#41; &&
       &#40;img1posX < img2posX + img2width&#41; &&
       &#40;img1posY + img1height > img2posY&#41; &&
       &#40;img1posY < img2posY + img2height&#41; &#41;&#41;;
&#125;
oh i did that for the second image becasue the first image is the moving sprite is it not? and also what part of my code should i replace with yours? Thanks once again though =) ill llook into the box collision
Post Reply