Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

edwinnie

Direction>_< Left? Right?

This topic is 6029 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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, .

Share this post


Link to post
Share on other sites
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() ?

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!