Advertisement

Direction>_< Left? Right?

Started by April 18, 2002 08:18 AM
3 comments, last by edwinnie 22 years, 6 months ago
i am still using the Dummy book and trying to make a Pong clone out of the provided functions. my problem is that whenever 1) the ball tried to hit the left paddle(redpaddle), heading towards the left direction ( <- ) it moves through it. (which is wrong) 2) the ball tried to hit the left paddle(redpaddle), heading towards the right direction ( -> ), reflection occurs. (which is correct) the code is as below>
  
void Move_Ball(void)
{
// this function sets the ball moving


Move_BOB(&ball);

//test for collision with redpaddle

//collision with front surface

if (ball.state == BALL_STATE_LEFT)
{
 if (Collision_Test(ball.x, ball.y, ball.width, ball.height,
 	  		        redpaddle.x, redpaddle.y, redpaddle.width, redpaddle.height))
 { 
     ball.state = BALL_STATE_RIGHT;
	 Start_Ball(20);
 }
}

//collision with back surface

if (ball.state == BALL_STATE_RIGHT)
{
 if (Collision_Test(ball.x, ball.y, ball.width, ball.height,
 	  		        redpaddle.x, redpaddle.y, redpaddle.width, redpaddle.height))
 { 
     ball.state = BALL_STATE_LEFT;
	 Start_Ball(20);
 }
}
   
}//end Move_Ball  
and the ball starts towards the left direction ( <- ), using this function>
  
void Start_Ball(int vel)
{
// this functions generates the ball and redpaddle starts first


if (ball.state == BALL_STATE_LEFT)
{   ball.xv = -vel;
} 

if (ball.state == BALL_STATE_RIGHT)
{   ball.xv = vel;
}

} // end Start_Ball  
the defines> #define BALL_STATE_LEFT 0 #define BALL_STATE_RIGHT 1 the initial state(in game_init()> ball.state = BALL_STATE_LEFT
Just a note: You could shrink the start_ball function by doing this

void Start_Ball(int vel){
ball.xv = -vel;
} // end Start_Ball

no matter which direction you ball is moving, negating it will give you the opposite:

x = -1;

x = -x; // x == 1
x = -x; // x == -1 (again)

... Second
In both collision tests, your testing redpaddle, which means you would only need test for both directions. But! What happened to the other paddle?

So, play grasshopper, play.
I'm not the brightest something or other in a group or similar.~ me ~
Advertisement
Doh!.. Ok, I''m a moron. Didn''t look at your start_ball code close enough . If you always send 20, then why send? And if your calling start_ball from w/in a test that has already determined the direction of the ball, why not tell it negative or positive direction and save the direction test w/in the start_ball function?

Yeah. If I find anything else I messed up on, .
I'm not the brightest something or other in a group or similar.~ me ~
well...lemme guess(i tried as well)


  if (ball.state == BALL_STATE_LEFT){ if (Collision_Test(ball.x, ball.y, ball.width, ball.height,       redpaddle.x, redpaddle.y, redpaddle.width,     redpaddle.height)) {       ball.xv = 20;}}  


u mean this in the part of Move_Ball() ?
wow, it worked!

hmm...wonder what was i thinking of just now...

This topic is closed to new replies.

Advertisement