[color=#0000FF]Hi guys
[color=#0000FF]this is my first time using allegro 5 and creating a game..
[color=#0000FF]my game is about a ball moving in the screen and it should change its direction when it hits the walls or when it hits the bar witch is moving by the user
[color=#0000FF]my problem is when the ball comes closer to the bar something goes wrong...
[color=#0000FF]so if someone can check my code and help me I will be thankful
Ball(int x, int y, int R, int r,int b , int g){
this->x=x;
this->y=y;
this->R= R;
this->r=r;
this->b=b;
this->g=g;
i=0;
dx=dy=4;
}
void draw(){
al_draw_filled_circle(x,y,R,al_map_rgb(r,b,g));
}
void move(){
x+=dx;
y+=dy;
}
int collid(int w, int h,line *l){
if(((y+R)-l->y1==0)||((x+R)>=l->x1&&(x+R)<=l->x2)){
dx*=-1;
dy*=-1;} // I know there is something wrong here....
else{
if ((x+R >= w)||(x-R<= 0)) dx*=-1;
if ((y-R<= 0)||(y+R >= h)) dy*=-1;
Well, something that immediately strikes me as a bit off is that you go back on both x and y directions when your ball hits the line. Since the line is vertical, you usually only want to reflect on the y velocity.
[color=#0000cd]I mean I don't know when the ball will hit the bar ,
[color=#0000cd]I took this (y+R)-l->y1==0)||((x+R)>=l->x1&&(x+R)<=l->x2 from my friend, but it doesn't give me the right position
To clarify with my suggestion, you should get rid of the dy*=-1 line from the main "line collision" test and possibly put in a secondary if block which tests to see if the ball hit the edge of the line.