Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualBaneTrapper

Posted 07 December 2012 - 09:56 AM

int main()
{
	Breakout bricks;
	bricks.draw_bricks();
	bricks.draw_paddle();
	bricks.draw_ball();
	bricks.move_ball();
	bricks.move_paddle();
	bricks.brick_collision();
	bricks.paddle_collision();
	return 0;
}

What i want to ask is, if you call (bricks.move_ball(), bricks.move_paddle()) and then check collision and it turns "Oh they collided" next loop comes, you draw that wrong looking frame.

With that code id put it like this
    Breakout bricks;
    bricks.move_ball();
    bricks.move_paddle();
    bricks.brick_collision();
    bricks.paddle_collision();
    bricks.draw_bricks();
    bricks.draw_paddle();
    bricks.draw_ball();
    return 0;

But it depends how your functions are made, in my code i make it this way
namespace en
{
    enum
    {
	    n_collision = 1,
	    y_collision = 2
    };
};

void check_for_valid_position()
{
//Setup position for next movement for ball
int ball_new_x = ball_position_x + ball_velocity_x,
    ball_new_y = ball_position_y + ball_velocity_y;

//Check if the balls new position is valid
if(check_collision(ball_new_x, ball_new_y) == en::y_collision)
// If the new position is invalid reverse ball_velocity x and y
    { /* bounce ball from the object */ }
else
// If the new position is valid, we apply it
    { ball_position_x = ball_new_x; ball_position_y = ball_new_y; }
}
The reason behind this is so i don't split ball_move() and ball_collision().
For exzample :
ball_move() moves ball for 5 pixels
ball_colision() detects that's invalid, and ball_collision now most do ball_move() job(move the ball - 5 pixels).

#2BaneTrapper

Posted 07 December 2012 - 09:51 AM

int main()
{
	Breakout bricks;
	bricks.draw_bricks();
	bricks.draw_paddle();
	bricks.draw_ball();
	bricks.move_ball();
	bricks.move_paddle();
	bricks.brick_collision();
	bricks.paddle_collision();
	return 0;
}

What i want to ask is, if you call (bricks.move_ball(), bricks.move_paddle()) and then check collision and it turns "Oh they collided" next loop comes, you draw that wrong looking frame.

With that code id put it like this
    Breakout bricks;
    bricks.move_ball();
    bricks.move_paddle();
    bricks.brick_collision();
    bricks.paddle_collision();
    bricks.draw_bricks();
    bricks.draw_paddle();
    bricks.draw_ball();
    return 0;

But it depends how your functions are made, in my code i make it this way
namespace en
{
    enum
    {
	    n_collision = 1,
	    y_collision = 2
    };
};

void check_for_valid_position()
{
//Setup position for next movement for ball
int ball_new_x = ball_position_x + ball_velocity_x,
    ball_new_y = ball_position_y + ball_velocity_y;

//Check if the balls new position is valid
if(check_collision(ball_new_x, ball_new_y) == en::y_collision)
// If the new position is invalid reverse ball_velocity x and y
    { /* bounce ball from the object */ }
else
// If the new position is valid, we apply it
    { ball_position_x = ball_new_x; ball_position_y = ball_new_y; }
}
The reason behind this is so i don't split ball_move() and ball_collision().
For exzample :
ball_move() moves ball for 5 pixels
ball_colision() detects that's invalid, and ball_collision now most do ball_move() job(move the ball - 5 pixels).

#1BaneTrapper

Posted 07 December 2012 - 09:48 AM

int main()
{
	Breakout bricks;
	bricks.draw_bricks();
	bricks.draw_paddle();
	bricks.draw_ball();
	bricks.move_ball();
	bricks.move_paddle();
	bricks.brick_collision();
	bricks.paddle_collision();
	return 0;
}

What i want to ask is, if you call (bricks.move_ball(), bricks.move_paddle()) and then check collision and it turns "Oh they collided" next loop comes, you draw that wrong looking frame.

With that code id put it like this
    Breakout bricks;
    bricks.move_ball();
    bricks.move_paddle();
    bricks.brick_collision();
    bricks.paddle_collision();
    bricks.draw_bricks();
    bricks.draw_paddle();
    bricks.draw_ball();
    return 0;

But it depends how your functions are made, in my code i make it this way
namespace en
{
    enum
    {
	    n_collision = 1,
	    y_collision = 2
    };
};

void check_for_valid_position()
{
//Setup position for next movement for ball
int ball_new_x = ball_position_x + ball_velocity_x,
    ball_new_y = ball_position_y + ball_velocity_y;

//Check if the balls new position is valid
if(check_collision(ball_new_x, ball_new_y) == en::y_collision)
// If the new position is invalid reverse ball_velocity x and y
    { /* bounce ball from the object */ }
else
// If the new position is valid, we apply it
    { ball_position_x = ball_new_x; ball_position_y = ball_new_y; }
}

PARTNERS