typedef struct direction
{
char q; // quadrant (up_left, up_right, low_left, low_right)
double c; // coefficient in degrees (0 .. 90)
} bdir;
struct ball
{
int xpos;
int ypos;
bdir dir;
} mainball;
int ploc; // refers to paddle location
int jump; // refers to velocity
//ball_cycle is called somewhere from a real timer signal handler.
void ball_cycle(void)
{
int paddle_hit_resolution = 0;
if (mainball.dir.q == up_left)
{ //sin and cos from math.h
mainball.xpos-=jump * cos(mainball.dir.c * PI/180);
mainball.ypos-=jump * sin(mainball.dir.c * PI/180);
if(mainball.xpos < 0)
{
mainball.dir.q = up_right;
}
if(mainball.ypos < 0)
{
mainball.dir.q = low_left;
}
return;
}
if (mainball.dir.q == up_right)
{
mainball.xpos+=jump * cos(mainball.dir.c * PI/180);
mainball.ypos-=jump * sin(mainball.dir.c * PI/180);
if(mainball.xpos > window_x_size - BALL_WIDTH)
{
mainball.dir.q = up_left;
}
if(mainball.ypos < 0)
{
mainball.dir.q = low_right;
}
return;
}
if (mainball.dir.q == low_left)
{
mainball.xpos-=jump * cos(mainball.dir.c * PI/180);
mainball.ypos+=jump * sin(mainball.dir.c * PI/180);
if(mainball.xpos < 1)
{
mainball.dir.q = low_right;
}
if(mainball.ypos > window_y_size - (BALL_HEIGHT + PADDLE_HEIGHT)) // paddle hit?
{
mainball.dir.q = up_left;
if ((mainball.xpos >= ploc) && (mainball.xpos <= ploc + PADDLE_WIDTH))
{
score++;
/*
this is where the question is about.
*/
} else
{
lost();
}
}
return;
}
if (mainball.dir.q == low_right)
{
mainball.xpos+=jump * cos(mainball.dir.c * PI/180);
mainball.ypos+=jump * sin(mainball.dir.c * PI/180);
if(mainball.xpos > window_x_size - BALL_WIDTH)
{
mainball.dir.q = low_left;
}
if(mainball.ypos > window_y_size - (BALL_HEIGHT + PADDLE_HEIGHT) ) //paddle hit?
{
mainball.dir.q = up_right;
if ((mainball.xpos >= ploc ) && (mainball.xpos <= ploc + PADDLE_WIDTH))
{
score++;
/*
here too.
*/
} else
{
lost();
}
}
return;
}
return;
}
上面的代码是来自桨/球游戏的片段,为我的教育写作。我如何计算球/桨碰撞?
有两个地方(见评论)这个问题是关于。
当球朝向屏幕的下部(当mainball.dir.q被low_left或low_right)
球移动或者撞击挡板和移动到另一个象限(up_left或up_right),或者,它错过了桨,失败()被称为并且游戏结束。 (45度,所以当球击中屏幕墙时,它也会向另一个象限反弹45度)。
在我的情况下,PADDLE_WIDTH等于120(像素)。所以我可以坚决在(ex)180部分的桨。例如。
resolution = ((mainball.xpos - ploc) * 180)/PADDLE_WIDTH
(所以无论地方球击中桨,将由0和180之间的值,而不管桨宽度的指示,对吗?)
在两个地方我需要计算一个碰撞。 (桨有速度,定义为全局变量别的地方)
(球已经与桨相撞后,我需要有一个新的角度,让游戏开始寻找流畅。)
会是什么在球与桨碰撞后成为新的mainball.dir.c值?
我猜的分辨率值,甚至可能速度应通过计算新mainball.dir.c使用。
你能缩小你的问题和代码吗?看起来太长了。 – 2014-09-10 12:33:15
[gamedev SE](http://gamedev.stackexchange.com/)可能会更好。 – Quentin 2014-09-10 12:51:51