Reputation: 3
I'm working on my brick breaker and to make a proper colliding system so as to make the ball switch direction logically, I have to detect with which side of the brick the ball collided. Here is my current script :
int sprite_collide(const Sprite item1, const Sprite item2)
{
if(item1.sShow == 0 || item2.sShow == 0) // return if any item is invisible
return 0;
if(item1.sCollide == 0 || item2.sCollide == 0)
return 0;
if((item2.sPos.x >= item1.sPos.x + item1.sImg->w) // too right
|| (item2.sPos.x + item2.sImg->w <= item1.sPos.x) // too left
|| (item2.sPos.y >= item1.sPos.y + item1.sImg->h) // too up
|| (item2.sPos.y + item2.sImg->h <= item1.sPos.y)) // too down
return 0;
return 1;
}
The Sprite Struct :
typedef struct Sprite
{
SDL_Surface *sImg;
SDL_Rect sPos;
int sShow; // If sShow = 1, we show it.
int sCollide; // If sCollide = 1, then it's physic.
int sMoving; // If sMoving = 1, it'll be able to move
float sSpeed;
int sLife;
int sInit;
} Sprite;
Upvotes: 0
Views: 121
Reputation: 586
You are just looking for any collision and then you return an int value. I would suggest to make another method to identify which side of the brick the ball touched. Something like this:
int sprite_collide(const Sprite item1, const Sprite item2)
{
if(item2.sPos.x >= item1.sPos.x + item1.sImg->w) // trop à droite
doAction(1);
if(item2.sPos.x + item2.sImg->w <= item1.sPos.x) // trop à gauche
doAction(2);
...
}
And then the doAction()
method will do something when the brick touches the side like you mentioned in the comments.
Good luck.
Upvotes: 1