views:

215

answers:

1

Im using oslib with the pspsdk toolchain and for some reason this doesnt work the way I think it would

float spritewidth  = sprite->stretchX;
float spriteheight = sprite->stretchY;
float bushwidth  = bush->stretchX;
float bushheight = bush->stretchY;

//Basic border collision
if (sprite->x <= 0)
 sprite->x = 0;
if (sprite->y <= 0)
 sprite->y = 0;
if (sprite->x >= 455)
 sprite->x = 455;
if (sprite->y >= 237)
 sprite->y = 237;

 //Bush
if ( (sprite->x + spritewidth > bush->x) && 
    (sprite->x < bush->x + bushwidth) && 
    (sprite->y + spriteheight > bush->y) && 
    (sprite->y < bush->y + bushheight) ) 
{
  bushcol = 1;               
}
else
{
  bushcol = 0;      
}

 if (osl_keys->held.down)
 {
   if (bushcol == 0)
   {
     sprite->y += 4;
     sprite_position = DOWN;
     SpriteAnimate();
   }
   else
   { 
     sprite->y -= 6;
     bushcol = 0;
   }
 }

 if (osl_keys->held.up)    
 {
   if (bushcol == 0)
   {
     sprite->y -= 4;
     sprite_position = UP;
     SpriteAnimate();
   }
   else
   { 
     sprite->y += 6;
     bushcol = 0;
   }
 }

 if (osl_keys->held.right)
 {
   if (bushcol == 0)
   {
     sprite->x += 4;
     sprite_position = RIGHT;
     SpriteAnimate();
   }
   else
   { 
     sprite->x -= 6;
     bushcol = 0;
   }
 }

 if (osl_keys->held.left)
 {
   if (bushcol == 0)
   {
     sprite->x -= 4;
     sprite_position = LEFT;
     SpriteAnimate();
   }
   else
   { 
     sprite->x += 6;
     bushcol = 0;
   }
 }

The sprite starts moving in the opposite direction from the bush when I try to move away but does fall free eventually

any better collision methods or suggestions

I even tried this for each button and still no luck

if (osl_keys->held.down)
{
  if ( (sprite->x + spritewidth > bush->x) &&
       (sprite->x < bush->x + bushwidth) && 
       (sprite->y + spriteheight > bush->y) &&
       (sprite->y < bush->y + bushheight) ) 
  {
    sprite->y -= 4; 
  }
  else
  {
    sprite->y += 2;
    sprite_position = DOWN;
    SpriteAnimate();
  }
}
A: 

One thing you can do, is instead of having the character "move backwards" when he hits the bush, you can have his position changed.

What I mean is something like this: (Using only up for the example).

 if (osl_keys->held.up)    
 {
   if (bushcol == 0)
   {
     sprite->y -= 4;
     sprite_position = UP;
     SpriteAnimate();
   }
   else
   { 
     sprite->y = bush->y + 2;
     bushcol = 0;
   }
 }

That way, whenever the sprite collides, it just sets the position instead getting it to move backwards.

There are other methods for doing collision detection, but I'm way too tired right now to pull off an intelligent, much less readable, answer right now... A search on google will turn up many results.

DeadHead