Paddle命中检测C ++

时间:2017-11-16 16:18:34

标签: c++ pong

在图像中,球从击球的顶部和底部(白线范围内的任何位置)的任何位置都会被击中。如何让球只在击中球拍时改变方向?

https://i.stack.imgur.com/CRYFN.png

int Paddle::checkBallCollision(Ball *b){
float paddle_topy, paddle_bottomy;
paddle_topy = this->position.y + length / 2;
paddle_bottomy = this->position.y - length / 2;

int PaddlePX = this->position.x;
int ballPX = b->position.x;
int ballPY = b->position.y;

if ((ballPY < paddle_bottomy) && (ballPY > paddle_topy) && (ballPX - b->width) > (PaddlePX - width / 2  )){
    DebugPrintf(" Paddle Hit ");

    return 1; // there is a collison 
}

else{
    return 0; // there is no collision
}

}

0 个答案:

没有答案