Public Group

Archived

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

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.

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);

//collision with front surface

if (ball.state == BALL_STATE_LEFT)
{
if (Collision_Test(ball.x, ball.y, ball.width, ball.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,
{
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 on other sites
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 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 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,
{
ball.xv = 20;
}

}

u mean this in the part of Move_Ball() ?

Share on other sites
wow, it worked!

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

1. 1
Rutin
29
2. 2
3. 3
4. 4
5. 5

• 13
• 13
• 11
• 10
• 14
• Forum Statistics

• Total Topics
632961
• Total Posts
3009493
• Who's Online (See full list)

There are no registered users currently online

×